首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >saxon distinct值抛出异常

saxon distinct值抛出异常
EN

Stack Overflow用户
提问于 2011-11-08 03:44:49
回答 3查看 2.3K关注 0票数 2

我正在尝试使用撒克逊运行一个“不同的值”XPath。这是我的代码:

代码语言:javascript
复制
 @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);

我明白这一例外:

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)的

javax.xml.xpath.XPathExpressionException:无法在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)上找到net.sf.saxon.dom.DOMNodeList类节点的对象模型实现

Saxon文件位于类路径中。另外,如果我试图在doc对象上直接调用“distinct-values”,我会得到一个:

net.sf.saxon.trans.XPathException:扩展函数需要类org.w3c.dom.NodeList;无法在net.sf.saxon.dom.DOMObjectModel.convertXPathValueToObject(DOMObjectModel.java:395) at net.sf.saxon.dom.DOMObjectModel.access$000(DOMObjectModel.java:42) at net.sf.saxon.dom.DOMObjectModel$5.convert(DOMObjectModel.java:166) at net.sf.saxon.xpath.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:352) at net.sf.saxon.xpath.XPathEvaluator.evaluate(XPathEvaluator.java:434)上转换net.sf.saxon.value.UntypedAtomicValue类的提供值

我搞不懂这是怎么回事。谷歌也没有!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-11-08 09:12:26

首先要指出的是,DOM和Saxon在一起并不特别好。如果要在树上使用Saxon来构造树,请优先使用Saxon的原生树模型,而不是DOM --它比DOM快十倍。

事实上,您提到了saxon-dom.jar,这意味着您必须使用一个相当旧的Saxon版本,可能该版本不再受支持。所以我的下一个建议是转向最近的版本。

我注意到的另一件事是,您要求XPath处理器处理Saxon对象模型,然后使用它来处理DOM对象模型。我不知道这是否可行。(如果您想确保加载的是Saxon,而不是其他XPath引擎,例如,因为您需要XPath 2.0,那么最好完全跳过JAXP工厂机制,直接实例化XPath实现。)

票数 4
EN

Stack Overflow用户

发布于 2014-03-27 17:48:52

我找到了一个从撒克逊检索NodeList的解决方案。在执行语句"List s= (List) xpath.evaluate(xpathString,doc1,XPathConstants.NODESET)“之后,您可以使用下面的代码从列表中读取节点和节点值:

getTagValue( "COMMODITYNAME“,NodeOverNodeInfo.wrap((NodeInfo) s.get(I)”COMMODITYNAME“是XML中的节点,您希望读取值,NodeOverNodeInfo.wrap((NodeInfo) s.get(i))是当前从"s”列表中指向的节点。

私有字符串getTagValue(String,NodeOverNodeInfo nodeInfo) {

代码语言:javascript
复制
    NodeList nodeList = nodeInfo.getChildNodes(); //list of  XML node
    Node nodeValue = null;
    String strReturn = null;
    for (int iItem=0;iItem<nodeList.getLength();iItem++)
    {
        nodeValue = nodeList.item(iItem).getFirstChild();   
        if (nodeValue != null && strag.equalsIgnoreCase(nodeValue.getParentNode().getNodeName()))
        {
            strReturn = nodeValue.getNodeValue();
            //punta la tag index
            //System.out.println(nodeValue.getParentNode().getNodeName()); //this is the node name
            //System.out.println(nodeValue.getNodeValue()); // this is the node value
        }
    }
return strReturn;
}

再见,瓦莱里奥

票数 1
EN

Stack Overflow用户

发布于 2011-11-08 15:59:49

这不是一个答案,我只是想评论迈克尔的反应,但评论是非常有限的。谢谢你的回复,迈克尔。我的依赖关系如下:

代码语言:javascript
复制
<dependency>
        <groupId>net.sourceforge.saxon</groupId>
        <artifactId>saxon</artifactId>
        <version>9.1.0.8</version>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.saxon</groupId>
        <artifactId>saxon</artifactId>
        <version>9.1.0.8</version>
        <classifier>xpath</classifier>
    </dependency>

    <dependency>
    <groupId>net.sourceforge.saxon</groupId>
        <artifactId>saxon</artifactId>
        <version>9.1.0.8</version>
        <classifier>dom</classifier>
    </dependency>

AFAIK这是maven回购的最新版本。如果我错过了什么,请告诉我。您对这种情况的解释是非常好的,除了我需要一个示例代码来了解如何做到这一点。我做了以下的改变,这是有效的!

代码语言:javascript
复制
InputSource is = new InputSource(this.getClass().getResourceAsStream("parametrizedId_feed.xml"));
         SAXSource ss = new SAXSource(is);

        XPath xpath =  XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON).newXPath();
        NodeInfo doc1 = ((XPathEvaluator)xpath).setSource(ss);
        System.out.println("Loaded XPath Provider " + xpath.getClass().getName());

       List s = (List) xpath.evaluate(xpathString, doc1 , XPathConstants.NODESET);
        for(int i = 0 ; i<s.size(); i++){
            String n = (String) s.get(i);
            System.out.println(n);

        }

你所说的撒克逊树模型是什么意思?唯一的问题是计算方法返回列表而不是NodeList。我想提到的是,我们之所以搬到Saxon,是因为它速度快,功能更好,所以代码库对JAXP有很多依赖,而JAXP是很难理解的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8045696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档