我正在开发一个通过 JAR 文件分发的 Java 桌面,该 JAR 包含应用程序所需的一切。通过首先生成 XML 文件,然后使用 XSL 文件将 XML 转换为 HTML 来为各种事物生成 HTML 报告的应用程序。生成报告后,它需要是一个文件,因此我想使用的任何脚本都必须包含在文件中,并且我想使用 jQuery。我在资源文件夹中有一个 jQuery 文件,我可以解析它并将其添加到 XML 文件中,但是当需要通过 XSL 将 XML 转换为 HTML 时,我收到以下错误:
ERROR: 'The entity name must immediately follow the '&' in the entity reference.'
javax.xml.transform.TransformerException: javax.xml.transform.TransformerException: com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: The entity name must immediately follow the '&' in the entity reference.
ERROR: 'com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: The entity name must immediately follow the '&' in the entity reference.'
阅读错误我可以看到我需要编码特殊字符,但我不准备手动编码整个 jQuery 文件。
所以我的最终目标是:
1.) 需要在我的 HTML 报告中包含以下内容:
<script>
// the entire jQuery library
</script>
2.) 我的主应用程序必须完全包含在一个 JAR 文件中
3.) 我不想将 jQuery 库复制/粘贴到 XSL 文件中。这行得通,但看起来很草率。
更新#1:
我在上面的第 3 点上错了。我无法将 jQuery 库复制/粘贴到 XSL 文件中而不会出现错误:
java.io.UTFDataFormatException: encoded string too long: 239677 bytes
紧随其后的是更可笑的错误:
javax.xml.transform.TransformerConfigurationException: This Templates does not contain a class with the name 'GregorSamsa'.
变形记中的GregorSamsa
更新#2:
我的 XSL 样式表的开头是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
<xsl:output method="html"/>
我将版本号从 a 更改1.0
为 a2.0
但我收到一个错误消息:
ERROR: 'Unsupported XSL element 'http://www.w3.org/1999/XSL/Transform:sequence''
javax.xml.transform.TransformerException: java.lang.RuntimeException: Unsupported XSL element 'http://www.w3.org/1999/XSL/Transform:sequence'
此外,我的 jQuery 文件与我的 XSL 样式表位于同一位置,因此我认为以下内容应该有效(但它没有):
<xsl:sequence select="unparsed-text('jquery-1.8.3.min.js')" />
更新#3:
Downloaded Saxon for Java here and added the JAR file to my application. Change my call to transformer factory to this:
TransformerFactory f = new net.sf.saxon.TransformerFactoryImpl(); // was TransformerFactory.newInstance();
Transformer t = f.newTransformer( new StreamSource( Example.class.getResourceAsStream( "resource/report_style.xsl" ) ) );
Source s = new StreamSource( XMLFile );
Result r = new StreamResult( HTMLFile );
t.transform( s, r );
Using the suggested <xsl:sequence select="unparsed-text('jquery-1.8.3.min.js')" />
I still get the equivalent of my original error, though now with Saxon parser:
net.sf.saxon.trans.XPathException: org.xml.sax.SAXParseException: The entity name must immediately follow the '&' in the entity reference.
SXXP0003: Error reported by XML parser: The entity name must immediately follow the '&' in the entity reference.
UPDATE #4:
I wasn't getting anywhere with this so I ended up reading the HTML file back in after I had created it, added my JavaScript to it, then wrote it back out. This is an extra step, but any difference in report generation time is imperceivable and it works. I would still be interested to know why XSLT 2.0 wasn't solving this for me though.