Rick's Tech Talk

All Tech Talk, Most of the Time

The Importance of the Namespace

Shakespeare asked "What's in a name? A rose by any another name would smell just as sweet." But he never worked in XML.

I was inside XML Notepad, looking at an XML file trying to debug an XSLT issue. I was pretty happy to see that XML Notepad had a Find dialog box that supported XPath. I threw in the XPath for the element I was looking for, and it produced a "Not Found" error.

XML Notepad cannot find elements with XPath (unless you use the namespace)

In the picture above, you can see that I'm using this XPath:

/statistics/index_preparation/stat[attribute::name="Sorts"]
I was confused, especially because the Java program that I was debugging processes this XPath correctly. So why wasn't the XPath working inside of XML Notepad? Staring at the error, I noticed that the message lists the prefixes for the each of the namespaces. In a spark of insight (aided by these references: [1] and [2]), I realized that I needed to add the prefixes to each of my elements!

XML Notepad finds elements with XPath provided you use the namespace prefix

In the picture above, XML Notepad now happily found my element. The XPath is now:

/a:statistics/a:index_preparation/a:stat[attribute::name="Sorts"]</p>
Adding the namespace helped! If Shakespeare were an XML coder, he'd be saying "A rose by any another name would smell just as sweet, provided you're in the right namespace."

Tags: