XPath for attribute value

After struggling with attributes for a long time, I finally discovered the easy way to obtain the text value of an attribute in a XML file with a XPath expression. Let’s say you got the following XML-file

  <note id="501">
    <body>Don't forget me this weekend!</body>
  <note id="502">
    <heading>Re: Reminder</heading>
    <body>I will not</body>

To get the value of the id attribute of the first note use an Xpath-expression with a string function

[ string( //note[1]/@id ) ]
and you get 501.

The puddle on my XPath

I always tried to stay out of the XML world, but there is no escaping for me now. Currently, I have to deal with the Open Archives Initiative (OAI) Protocol for Metadata Harvesting (PMH). This appears to be a popular protocol to disclose public archives. Technically, it boils down to XML messages with lot’s of different namespaces.To give you an example:

<?xml version="1.0" encoding="UTF-8"?>
<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
<request verb="GetRecord" metadataPrefix="abc" identifier="AB.abcid1">http://app.archive.nl/oai</request>
<ns1:ns1 xsi:schemaLocation="http://www.archive.nl/abc/1.0 http://www.archive.nl/xsd/abc
<ns1:beginTag xml:lang="en-us">

Now, how can you select with XPath the content between the metadata tags?

The metadata tag is in the default namespace, so my first guess was to use


as XPath. But to my surprise this delivers not the content between the metadata tags. It appears that although the tags record and metadata are in the default namespace, there is no link made with the default namespaceuri.The solution is to create an artificial NamespaceContext with a made up prefix and the default namespaceuri. Let’s take for the sake of the argument ‘oai’ as prefix for the default “http://www.openarchives.org/OAI/2.0/” namespaceuri. The XPath expression would then look like this


This delivers the content between the metadata tags.

Every technology has it’s quirks, but I hope not to meet many of them in the XML world. XML appears on the surface simple, but combined with XPath and XSLT you really can find yourself lost. I rather stick to Java were I am in my natural habitat.