gpt4 book ai didi

java - Java 中带有参数的高效 XSLT 管道

转载 作者:搜寻专家 更新时间:2023-10-31 19:58:40 25 4
gpt4 key购买 nike

这个问题的最佳答案描述了一种在 Java 中实现高效 XSLT 管道的技术:

Efficient XSLT pipeline in Java (or redirecting Results to Sources)

不幸的是,虽然 Transformer 似乎公开了一个用于设置 XSLT 参数的 API,但这似乎没有任何效果。例如,我有以下代码:

变形金刚.java

import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.Templates;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.Transformer;
import java.io.File;
public class MyTransformer {
public static void main(String[] args) throws javax.xml.transform.TransformerConfigurationException, javax.xml.transform.TransformerException{
SAXTransformerFactory stf = (SAXTransformerFactory)TransformerFactory.newInstance();

// These templates objects could be reused and obtained from elsewhere.
Templates templates1 = stf.newTemplates(new StreamSource( new File("MyStylesheet1.xslt")));
Templates templates2 = stf.newTemplates(new StreamSource(new File("MyStylesheet2.xslt")));

TransformerHandler th1 = stf.newTransformerHandler(templates1);
TransformerHandler th2 = stf.newTransformerHandler(templates2);

th1.setResult(new SAXResult(th2));
th2.setResult(new StreamResult(System.out));

Transformer t = stf.newTransformer();

//SETTING PARAMETERS HERE
t.setParameter("foo","this is from param 1");
t.setParameter("bar","this is from param 2");

t.transform(new StreamSource(new File("in.xml")), new SAXResult(th1));

// th1 feeds th2, which in turn feeds System.out.
}
}

MyStylesheet1.xslt

<?xml version="1.0"?>
<stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:foo="urn:foo" version="1.0">
<output method="xml"/>

<param name="foo"/>

<template match="@*|node()">
<copy>
<apply-templates select="@*|node()"/>
</copy>
</template>

<template match="foo:my/foo:hello">
<copy>
<foo:world>
foo is : <value-of select="$foo"/>
</foo:world>
</copy>

</template>
</stylesheet>

MyStylesheet2.xslt

<?xml version="1.0"?>
<stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:foo="urn:foo" version="1.0">
<output method="xml"/>

<param name="bar"/>

<template match="@*|node()">
<copy>
<apply-templates select="@*|node()"/>
</copy>
</template>

<template match="foo:my/foo:hello/foo:world">
<copy>
<apply-templates select="@*|node()"/>

<attribute name="attr">
<value-of select="$bar"/>
</attribute>
</copy>

</template>
</stylesheet>

in.xml

<my xmlns="urn:foo">
<hello/>
</my>

这给了我以下输出:

<?xml version="1.0" encoding="UTF-8"?><my xmlns="urn:foo">
<hello><foo:world xmlns:foo="urn:foo">foo is : </foo:world></hello>
</my>

如您所见,foo:world/@attr 是空的,foo:world 的文本内容是“foo is:”。预期的行为是它们应该已经填充了传递给 setParameter 方法的参数。

有没有办法使用这种技术设置 XSL 转换参数。如果没有,有人可以推荐一种在 Java 中有效转换样式表的替代技术,这样也可以设置 XSLT 参数吗?

最佳答案

问题是每个 TransformerHandler 都有一个单独的 Transformer 与之关联。你的第二个模板有问题,但因为这是一个例子,我想这无关紧要。你想要:

//SETTING PARAMETERS HERE
th1.getTransformer().setParameter("foo","this is from param 1");
th2.getTransformer().setParameter("bar","this is from param 2");

请注意,您也不需要创建第三个转换器,您只需在 th1.getTransformer() 上启动转换链即可

关于java - Java 中带有参数的高效 XSLT 管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3517352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com