Search code examples
javaxmldom4j

Dom4 1.6.1j: XPath that returns non-node type value possible?


e.g. I want to return the name of a node, i.e. a string, thus

/MxML/trades/trade/tradeBody/*[1]/local-name()

However when I try to evaluate it with doc.valueOf or doc.selectSingleNode, or whatever else I try I get this error:

org.dom4j.InvalidXPathException: Invalid XPath expression: /MxML/trades/trade/tradeBody/*[1]/local-name() Expected node-type

I know what its saying, I am returning a String and not a node, so how do I ask for this string?

Thanks.


Solution

  • Are you sure it works with JAXP? The expression does not look ok to me; local-name() is not a node-step.

    This is fine with dom4j:

        Document doc = DocumentHelper
                .parseText("<x:fish xmlns:x='42'>Goodbye, and thanks for all the fish</x:fish>");
        XPath xpath = new DefaultXPath("local-name(/*[1])");
        Object result = xpath.evaluate(doc);
        System.out.printf("Type: %s, Value: %s\n", result.getClass()
                .getSimpleName(), result);
    

    prints

    Type: String, Value: fish