我有一个Java的Maven项目,其中包含XSLT转换。我按照以下方式加载样式表:
TransformerFactory tFactory = TransformerFactory.newInstance();
DocumentBuilderFactory dFactory = DocumentBuilderFactory
.newInstance();
dFactory.setNamespaceAware(true);
DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
ClassLoader cl = this.getClass().getClassLoader();
java.io.InputStream in = cl.getResourceAsStream("xsl/stylesheet.xsl");
InputSource xslInputSource = new InputSource(in);
Document xslDoc = dBuilder.parse(xslInputSource);
DOMSource xslDomSource = new DOMSource(xslDoc);
Transformer transformer = tFactory.newTransformer(xslDomSource);
stylesheet.xsl中有许多语句。当我尝试运行我的单元测试时,这些语句似乎会引起问题,我会收到以下错误:
C:\Code\workspace\app\dummy.xsl; Line #0; Column #0; Had IO Exception with stylesheet file: footer.xsl
C:\Code\workspace\app\dummy.xsl; Line #0; Column #0; Had IO Exception with stylesheet file: topbar.xsl
XSLT中的include语句是相对链接。
xsl:include href="footer.xsl"
xsl:include href="topbar.xsl"
我尝试了实验并更改成以下内容,但仍然出现错误。
xsl:include href="xsl/footer.xsl"
xsl:include href="xsl/topbar.xsl"
任何想法?非常感谢您的任何帮助。
documentBuilderFactory.setNamespaceAware(true)
。如果没有这样做,可能会阻止父样式表加载子样式表中的任何模板,导致 ElemTemplateElement 错误。 - Xr.