This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes xpath does not match when namespace prefix changes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "xpath does not match when namespace prefix changes" Watch "xpath does not match when namespace prefix changes" New topic

xpath does not match when namespace prefix changes

Arjun Karthick
Ranch Hand

Joined: Jan 04, 2007
Posts: 77
Hi all,

The question might seem simple, but I'm not able to figure the solution.

I need to extract the text node of a particular element. This is the sample xml document

I am using the following xpath to extract.

This is not extracting the value as the namespace prefix does not match. Is there a work around. It works if namespace prefix in the xpath is changed.

Thanks and Regards

PS: This is a duplicate post. I have created the same post in the XML certification topic. Kindly delete the other post.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19728

It doesn't matter whether the prefix is the same or different. What does matter is the namespace URI applied to the prefixes in the two places; that should be the same.

In the XML document I can see the namespace URI which is applied to the "ns" prefix. But I don't see the code where you apply a namespace URI to the "tns" prefix in your XPath expression. Perhaps you didn't apply any? If you are asking about a javax.xml.xpath.XPath object (you didn't mention that) then the setNamespaceContext method is how you do that.
I agree. Here's the link:
subject: xpath does not match when namespace prefix changes
It's not a secret anymore!