我已经遇到了Saxon 9.5 HE的一个问题,我几天都找不到解决方案。 也许这是显而易见的事情,但我无法看到它。
我尝试做的是通过指定自定义URIResolver来对XSL进行XML转换。
如果我运行它,我会加载我的XSL文件并进行转换,产生有效的结果。
try {
//tfactory.setURIResolver(new DOTUriResolver());
Transformer transformer = tfactory.newTransformer(xslSource);
if (transformer!=null)
transformer.transform(xmlSource, new StreamResult(System.out));
}
catch (Exception e) {
System.err.println(e.getMessage());
}
如果我发表评论
tfactory.setURIResolver(new DOTUriResolver());
并提供一个非常简单的URIResolver,只打印出base和href的值:
@Override
public Source resolve(String href, String base) throws XPathException {
System.out.println( " resolve: " + base + " | " + href);
return;
}
创建变换器对象时出错。
javax.xml.transform.TransformerConfigurationException: XML-22000: (Fatal Error) Error while parsing XSL file
查看我的URIResolver打印的内容,我看到base参数始终相同,并且具有root(first)XSL文件的值。我希望基于当前包含xsl:import或xsl:include标记(或document())的XSL文件进行更改,但对于所有文件保持不变。
这是创建xslSource并设置systemId的代码:
StreamSource source = new StreamSource(file);
String systemID = file.toURI().toURL().toString();
source.setSystemId(systemID);
你有什么想法为什么会这样?过去曾使用以前版本的Saxon(9.1)。
非常感谢任何帮助。
编辑: 这是我的URI解析器(完整代码):
package com.mekon.xproc;
import javax.xml.transform.Source;
import javax.xml.transform.URIResolver;
import net.sf.saxon.trans.XPathException;
public class DOTUriResolver implements URIResolver{
@Override
public Source resolve(String href, String base) throws XPathException {
System.out.println( " resolve: " + base + " | " + href);
return null;
}
}
现在,如果我使用它,我会遇到上述问题。对于每个呼叫,Base将保持相同,与第一个源的值相同。如果我不打电话,那么URI就会毫无问题地解决。
答案 0 :(得分:0)
需要查看URIResolver的完整代码。
我不确定为什么要在Source对象上设置File和URI。构造函数的代码是:
public StreamSource(File f) {
144 //convert file to appropriate URI, f.toURI().toASCIIString()
145 //converts the URI to string as per rule specified in
146 //RFC 2396,
147 setSystemId(f.toURI().toASCIIString());
148 }
当Saxon从URIResolver提供的Source读取样式表模块时,它将使用该Source对象的SystemId属性作为该样式表模块的基URI(当然假设没有外部实体,xml:base属性,等混淆事情。
答案 1 :(得分:0)
如果我使用:
,问题就解决了TransformerFactory tFactory = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl",null);
显然,这是一个我需要调查的类路径问题,大概也会引用Xalan。
非常感谢你的时间。