我想使用撒克逊运行“不同值”的XPath。这里是我的代码:撒克逊不同值引发异常
@Test
public void testAttributeSelect() throws XPathFactoryConfigurationException {
System.setProperty("javax.xml.xpath.XPathFactory:"
+ NamespaceConstant.OBJECT_MODEL_SAXON,
"net.sf.saxon.xpath.XPathFactoryImpl");
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
"net.sf.saxon.dom.DocumentBuilderFactoryImpl");
String xpathString = "distinct-values(//id)";
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
try {
DocumentBuilder builder = domFactory.newDocumentBuilder();
System.out.println(builder.getClass());
Document doc =
builder.parse(this.getClass().getResourceAsStream("parametrizedId_feed.xml"));
System.out.println(doc.getClass());
XPath xpath =
XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON).newXPath();
NodeList s1 = (NodeList)
xpath.evaluate("/matches", doc, XPathConstants.NODESET);
NodeList s = (NodeList)
xpath.evaluate(xpathString, s1 , XPathConstants.NODESET);
我得到这个异常:
javax.xml.xpath.XPathExpressionException:无法定位类net.sf.saxon.dom.DOMNodeList节点的对象模型实现 at net.sf.saxon.xpath.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:300) at net.sf.saxon.xpath.XPathEvaluator.evaluate(XPathEvaluator.java:434) at ca.cbc.panacea.playground。 TestXpath.testAttributeSelect(TestXpath.java:43) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
撒克逊-DOM jar文件是在类路径。 另外,如果我尝试调用“不同的值”直接在文档对象,我得到一个:
net.sf.saxon.trans.XPathException:扩展功能所需的类org.w3c.dom.NodeList; net.sf.saxon.value.UntypedAtomicValue类的提供的值无法转换为 at net.sf.saxon.dom.DOMObjectModel.convertXPathValueToObject(DOMObjectModel.java:395) at net.sf.saxon.dom.DOMObjectModel.access在net.sf.saxon.dom.DOMObjectModel处使用$ 000(DOMObjectModel.java:42) $ 5.convert(DOMObjectModel.java:166) at net.sf.saxon.xpath.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:352) 在net.sf.saxon.xpath.XPathEvaluator.evaluate(XPathEvaluator.java:434)
我想不通这里发生了什么。而谷歌也不是!
@arash:要查看代码示例,一个使用Saxon的原生树和一个使用DOM ,转到http://sourceforge.net/projects/saxon/files/Saxon-HE/9.4/然后下载撒克逊资源文件。查看XPathExample.java和XPathExampleDOM.java的“samples/java”文件夹。这些样本有助于查看差异。 –