使用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()函数是有效的xpath 1.0表达式,但它似乎不能用作vtd-xml的xpath表达式的开头。
任何暗示都将不胜感激。

最佳答案

尝试使用useevalXPathToNumber而不是selectXPath

09-19 10:13