> I'm still a little unclear on how XPath and namespaces relate.
>  
> I'm working on a program which can take as input 2 things: any arbitrary
> xxml document, and any arbitrary xpath expression.  I need to run the
> expression on the document.  My initial guess is that this code will do the
> trick:
>  
>                     XPath xpath =
> (XPath)DocumentHelper.createXPath(expression);
>                     Node selectedNode =
> (Node)xpath.selectSingleNode(document);
>  
> I *think* this should work for every case (assuming the xpath is correctly
> entered by the user), but this code doesn't take namespaces into account.  I
> remember having to jump through some hoops for another part of my code which
> does use namespaces, and I also seem to remember some function which adds
> all the namespaces of a document to the xpath object.  Am I mistaken here?
> What is the correct way to go?

What you'll want to do:

a) Determine the namespace(s) of the element(s) that your XPath refers
to.

b) Bind directly into the XPath, irrespective of any target document,
the prefix->URI that *you* select for the context of your XPath.

c) Now, regardless of the prefix used in a target document, if the
URI resolves to the same URI that *your* prefixes (from (b) above),
then a match will occur. 

You may bind:

        cheese -> http://pizza.org/pizza-ml

While the document bings

        crust -> http://pizza.org/pizza-ml

If you XPath is "cheese:call-in-order", it'll match
a document containing:

        <call-in-order xmlns:crust="http://pizza.org/pizza-ml"/>

As far as actual API you need... beats me.  I typically use
Jaxen directly, and not through the dom4j api, for some reason.

        -bob


_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to