我想使用撒克遜運行「不同值」的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」文件夾。這些樣本有助於查看差異。 –