You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by "Preissler, Z 1903, BN" <G....@DeutschePost.de> on 2005/12/08 14:20:12 UTC

JXPath: evaluating XPath against DOM Element?

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
expression.

        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(is);
        JXPathContext docContext = JXPathContext.newContext(doc);
        docContext.setLenient(true);
        //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);
        elementContext.setLenient(true);
        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
way?

TIA
	Jerry

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org