Title: 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