commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Preissler, Z 1903, BN" <>
Subject JXPath: evaluating XPath against DOM Element?
Date Thu, 08 Dec 2005 13:20:12 GMT
Is there any way to use JXPath to evaluate XPath statements relative to a
DOM element node? In the following code fragment
everything works as expected if I create a JXPathContext with an
org.w3c.dom.Document as contextBean, but if I use the document's
root element, it returns <null> whenever I try to evaluate an XPath

        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(is);
        JXPathContext docContext = JXPathContext.newContext(doc);
        //this works
        Object val = null;
        Pointer ptr = docContext.createPath("/address");
        JXPathContext childContext = docContext.getRelativeContext(ptr);
        val = childContext.getValue("street");
        // val == "Orchard Road"
        //this doesn't
        Element root = doc.getDocumentElement();
        JXPathContext elementContext = JXPathContext.newContext(root);
        Object val2 = elementContext.getValue("/address/street");
        // val2 == null

Since I'd like to use an Element burried inside a larger document as
contextBean, first creating the childContext and removing any
leading slashes from xpath strings is a little cumbersome. Is there a better


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message