Search code examples
xmlfunctionxpathcountvtd-xml

VTD-XML 2.11 XPath expression count() fails with exception com.ximpleware.XPathEvalException: Function Expr can't eval to node set


Using the VTD-XML 2.11 (Java) API, when evaluating the XPath expression count(//b) on the XML document <a><b/><b/></a>, instead getting a result of 2.0, it fails with the following exception:

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)

Here is a very simple test case to reproduce the problem:

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();
    }
}

It looks like the count() function can't be used as the start of an XPath expression with VTD-XML, although it is a valid XPath 1.0 expression?

Any hint would be greatly appreciated.


Solution

  • Try to use use evalXPathToNumber instead of selectXPath.