VTD-XML 2.11 XPath 表达式 count() 失败,出现异常 com.ximpleware.XPathEvalException: Function Expr 无法评估节点集



使用 VTD-XML 2.11 (Java) API,在 XML 文档<a><b/><b/></a>上计算 XPath 表达式count(//b)时,而不是得到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 表达式?

任何提示将不胜感激。

尝试使用 evalXPathToNumber 而不是 selectXPath

最新更新