我想重现this Java 示例应用程序中的示例:
http://www.w3schools.com/Xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog
这是我的示例应用程序:
-
input.xml
- 包含 w3schools 网站中显示的内容 -
transform.xslt
- 包含 w3schools 网站中显示的内容 -
output.xml
- 只是空文件。将由内容填充为xslt转换结果
所有文件都位于我的 /myproject/...resource/myclasspath
中目录。
[我用getResourceAsStream
])
这就是示例应用程序:
public class TestMain {
public static void main(String[] args) throws IOException, URISyntaxException, TransformerException {
TransformerFactory factory = TransformerFactory.newInstance();
InputStream xstlStream = TestMain.class.getResourceAsStream("transform.xslt");
Source xslt = new StreamSource(xstlStream);
Transformer transformer = factory.newTransformer(xslt);
InputStream inputStream = TestMain.class.getResourceAsStream("input.xml");
Source inputSource = new StreamSource(inputStream);
URL url = TestMain.class.getResource("output.xml");
transformer.transform(inputSource, new StreamResult(new File(url.toURI())));
}
}
当我启动这个应用程序时,它在 output.xml
中仍然没有任何内容。我对 w3schools 网站上显示的结果感到异常。
基本上,对于可能的特殊情况,我只想将属性添加到 <data></data>
标签,具有这样的输出 <data attr=""></data>
。但首先从这个示例应用程序开始。
问:为什么不起作用?
最佳答案
解决方案:只查看已编译的文件夹,但不查看源代码。有包含预期内容的输出文件。
关于java - 使用 XSLT。示例应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16500749/