Title: RE: [dom4j-user] simplest way to use namespaces URI in xpath in dom4j?
Hi Guys
 
One other option is to explicity setup an org.jaxen.NamespaceContext object with one or more namespace prefixes matched to the namespace URIs you need which can then be used in an XPath.
 
I'm thinking of adding something like the following to the XPath interface (along with maybe similar changes in Jaxen)
 
Document doc = ..;
 
// create the XPath with our namespace prefixes
XPath xpath = doc.createXPath( "/foo:aaa/foo:bbb/foo:xxx" );
 
// map the prefixes to the URIs we want
xpath.addNamespace( "foo", "http://www.example.org/my/" );
 
// now lets use it
List list = xpath.selectNodes( doc );
 
A word of warning though - namespaces seem to be an area of much confusion - but the namespace prefixes that are mapped to URIs inside an XPath expressoin (such as the above xpath object) are totally independent of the prefixes used in the source document. To put that another way, it doesn't matter what prefixes you choose to use in your XPath expressions, only what URIs they map to and the namespace URIs that are in the source document.

James
----- Original Message -----
Sent: Tuesday, September 25, 2001 7:24 AM
Subject: RE: [dom4j-user] simplest way to use namespaces URI in xpath in dom4j?

This is a way to match on a known namespace uri and local element name. It's a bit on the verbose side.

/*[local-name()="aaa" and namespace-uri()="http://www.example.org/my/"]/*[local-name()="bbb" and namespace-uri()="http://www.example.org/my/"]

If you know that no other elements are called aaa or bbb in the document regardless of namespaces, you can omit the test for namespace-uri in the expression and end up with:

/*[local-name()="aaa"]/*[local-name()="bbb"]

which is a bit nicer.

BTW you will want to use single quotes instead of double quotes (or escape the double quotes) to embed the expressions in a Java String.

-- Steen

> -----Original Message-----
> From: Martijn Koster [mailto:[EMAIL PROTECTED]]
> Sent: Monday, September 24, 2001 6:16 PM
> To: [EMAIL PROTECTED]
> Subject: [dom4j-user] simplest way to use namespaces URI in xpath in
> dom4j?
>
>
> What is the easiest way in Dom4J to specify an XPath expression for a
> document where you know the namespace URI of the desired nodes, but
> not the prefix? Ie in:
>
>  <?xml version='1.0'?>
>  <my:aaa xmlns:my="http://www.example.org/my/"
>          xmlns:his="http://www.example.org/his/">
>    <my:bbb>
>      <my:xxx/>
>      <his:xxx/>
>    </my:bbb>
>  </my:aaa>
>
> I want to select element my:xxx, but I don't know the prefix "my",
> only "http://www.example.org/my/". I can navigate to it using QNames,
> but I want to use a single XPath expression. I can do it by hand:
>
>   Namespace ns =
> root.getNamespaceForURI("http://www.example.org/my/");
>   String prefix = ns.getPrefix();
>   doc.selectNodes("/" + prefix ":aaa/" + prefix + ":bbb/" +
> prefix + ":xxx");
>
> but that gets ugly. Is there a way I can do something like:
>
>   doc.selectNodes("/aaa/bbb/xxx", ns);
>
> or
>
>   doc.selectNodes(new XPathExpression("/aaa/bbb/xxx", ns));
>
> or some such?
>
> -- Martijn
>
> _______________________________________________
> dom4j-user mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/dom4j-user
>

Reply via email to