对于我的项目,我有一个包含xsl文件的软件包(xml)。当我需要一个时,我只需获取文件:
String xsl=getTextResource(this,"../xml/rob.xsl");
并且转换按预期工作
TransformerFactory tFactory=getTransformerFactory();
Transformer transformer=tFactory.newTransformer(xslSource);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
if(xslParameters !=null) {
for(Parameter p:xslParameters) {
transformer.setParameter(p.getName(), p.getStringValue());
}
}
result=XMLHelper.createDocument();
DOMResult resultStream=new DOMResult(result);
transformer.transform(xmlSource, resultStream);
但是,我的问题是以下陈述:
<xsl:param name="matcherPath">.</xsl:param>
<xsl:param name="parameterPath" select="concat($matcherPath,'/','rob_parameters.xml')" />
<xsl:variable name="zuordnungsTabelle" select="document(concat($matcherPath,'/','rob_matcher.xml'))" />
<xsl:variable name="parameterTabelle" select="document($parameterPath)" />
转换器在文件系统中搜索,但不在类文件中搜索。所以找不到文件。
转换器是否有可能在包中而不是在文件系统中查找其他文档?
谢谢马丁,这是解决这个问题的正确提示。
...
TransformerFactory tFactory=getTransformerFactory();
...
tFactory.setURIResolver(resolver);
解析器是一个实现 URIResolver 的类,该解析器实现函数解析。(有点快速和肮脏的解决方案...但它有效)
@Override
public Source resolve(String href, String base) throws TransformerException {
Source result=null;
// System.out.println("href="+href+" base="+base);
try {
String resultString=getTextResource(this,"../xml/"+href);
result=new StreamSource(new StringReader(resultString));
} catch (FileNotFoundException ex) {
Logger.getLogger(RoB_TransferGenerator.class.getName()).log(Level.SEVERE, null, ex);
throw new TransformerException("href="+href+" base="+base+ " not found");
}
return result;
}