XPath and XML: Multiple namespaces

Posted by emragins on Stack Overflow See other posts from Stack Overflow or by emragins
Published on 2011-02-25T22:43:00Z Indexed on 2011/02/25 23:25 UTC
Read the original article Hit count: 201

Filed under:
|

So I have a document that looks like

<a xmlns="uri1" xmlns:pre2="uri2">
 <b xmlns:pre3="uri3">
   <pre3:c>
     <stuff></stuff>
     <goes></goes>
     <here></here>
   </pre3:c>
   <pre3:d xmlns="uri4">
     <under></under>
     <the></the>
     <tree></tree>
   </pre3:d>
  </b>
</a>

I want an xpath expression that will get me <under>.

This has a namespaceURI of uri4.

Right now my expression looks like:

//ns:a/ns:b/pre3:d/pre4:under

I have the namespace manager add 'ns' for the default namespace (uri1 in this case) and I have it defined with pre2, pre3, and pre4 for uri2, uri3, and uri4 respectively.

I get the error "Expression must evaluate to a node-set."

I know that the node exists. I know that everything up until the pre4:under in my xpath works fine as I use it in the rest of the document with no issues. It's the additional pre4:under that causes the error, and I'm not sure why.

Any ideas?

Thanks.

© Stack Overflow or respective owner

Related posts about xpath

Related posts about xml-namespaces