VTD-XML 2.11 XPath表达式count()失败,异常com.ximpleware.XPathEvalException:函数Expr无法eval到节点集

时间:2013-01-29 22:18:50

标签: xml function xpath count vtd-xml

使用VTD-XML 2.11(Java)API,在评估XML文档count(//b)上的XPath表达式<a><b/><b/></a>时,而不是获得2.0的结果,它会失败并显示以下内容例外:

com.ximpleware.XPathEvalException:  Function Expr can't eval to node set
   at com.ximpleware.FuncExpr.evalNodeSet(FuncExpr.java:1033)
   at com.ximpleware.AutoPilot.evalXPath(AutoPilot.java:876)
   at ...testVTDXMLXPathFunctionCount(TestVTDXMLXPath.java:107)

这是一个非常简单的测试用例来重现问题:

public void testVTDXMLXPathFunctionCount() throws Exception {
    AutoPilot autoPilot = new AutoPilot();
    try {
        VTDGen document = new VTDGen();
        document.setDoc("<a><b/><b/></a>".getBytes());
        document.parse(true);
        VTDNav navigator = document.getNav();
        autoPilot.selectXPath("count(//b)");
        autoPilot.bind(navigator);
        int j;
        while ((j = autoPilot.evalXPath()) != -1) {
            System.out.println(navigator.toNormalizedXPathString(j));
        }
    } catch (XPathParseException e) {
        e.printStackTrace();
    } catch (XPathEvalException e) {
        e.printStackTrace();
    } catch (NavException e) {
        e.printStackTrace();
    } finally {
        autoPilot.resetXPath();
    }
}

看起来count()函数不能用作带VTD-XML的XPath表达式的开头,尽管它是一个有效的XPath 1.0表达式?

任何提示都将不胜感激。

1 个答案:

答案 0 :(得分:1)

尝试使用evalXPathToNumber代替selectXPath