我遇到一个问题,其中一个XSLT文件导入另一个,导致我的应用程序抛出MalformedURLException.import语句main.xsl如下所示:
<xsl:import href="transformCommon.xsl"/>
Run Code Online (Sandbox Code Playgroud)
该文件transformCommon.xsl与...相同main.xsl.限制加载它的代码如下所示:
private void loadXSLTFiles(String xsltFile)
{
TransformerFactory transformFactory = TransformerFactory.newInstance();
//tell the location of all of import file
transformFactory.setURIResolver(new ClassPathURIResolver());
Templates cache=null;
//cache XSLT source file for transformation reuse
InputStream is = this.getClass().getClassLoader().getResourceAsStream(xsltFile);
javax.xml.transform.Source xsltSource = new javax.xml.transform.stream.StreamSource(is);
try
{
cache = transformFactory.newTemplates(xsltSource);
}
catch (TransformerConfigurationException domException)
{
LOG.logError("XSLT initialization error has occurred: " + domException.getMessage());
}
...
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪是:
Caused by: java.net.MalformedURLException
at java.net.URL.(URL.java:602)
at java.net.URL.(URL.java:465)
at java.net.URL.(URL.java:414)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.apache.xalan.templates.StylesheetRootProxy.(Unknown Source)
... 59 more
我不确定为什么会出现这个错误.当我从中删除导入时main.xsl,一切正常.当然,删除它不是一种选择,因为这一点的重点是将常用函数移动到单独的XSLT.
有趣的是,只有我的工作站似乎有这个问题.最初编写此代码的开发人员说他没有遇到任何问题.我正在使用RAD 7.5.有没有人知道如何在逐个工作站的基础上出现这个问题?
为了能够解析样式表中的相对URL(包括导入)Source,您Templates需要从中创建"系统ID"(即.xsl文件的URL ).
代替
//tell the location of all of import file
transformFactory.setURIResolver(new ClassPathURIResolver());
//cache XSLT source file for transformation reuse
InputStream is = this.getClass().getClassLoader().getResourceAsStream(xsltFile);
javax.xml.transform.Source xsltSource = new javax.xml.transform.stream.StreamSource(is);
Run Code Online (Sandbox Code Playgroud)
试试这个:
URL xsltURL = this.getClass().getClassLoader().getResource(xsltFile);
Source xsltSource = new StreamSource(xsltURL.openStream(),
xsltURL.toExternalForm());
Run Code Online (Sandbox Code Playgroud)
(openStream可以抛出,IOException所以你需要将它添加到你的throws或在try/catch中包装整个东西).
| 归档时间: |
|
| 查看次数: |
2712 次 |
| 最近记录: |