14

I was given the following XML:

<root> <items> <item> <title>Item</title> <details> <data xmlns="http://some_url"> <length>10</length> <weight>1.2</weight> </data> </details> </item> </items> </root> 

Following XPath does not work meaning nothing is printed like the "data" element does not exists:

/root/items/item/details/data 

But when I remove "xmlns" namespace attribute of "data" element it's content is printed. How should the xpath expression look like to work without deleting "xmlns" namespace attribute of "data" element?

I'm using SAXON and XSL 1.0.

1
  • You need to register the namespace with your XPath engine, then use the alias you registered in your XPath: alias:data/alias:length. We can't get more specific than that without knowing what you are using to evaluate the XPath. Commented Jun 11, 2012 at 13:49

2 Answers 2

16

This is one of the most FAQ in XPath / XSLT:

XPath interprets an unprefixed element name as belonging to "no namespace" and this is the reason elements with unprefixed names belonging to a default (nonempty) namespace aren't selected when only their unprefixed name is specified as a node-test in an XPath expression.

The solution is either:

  1. Create a namespace binding where a prefix (say "x") is associated with the default namespace, then specify x:elementName instead of elementName.

  2. Use long, ugly and unreliable expressions like: *[name() = 'elementName']

Here is an XSLT transformation using the above method1. :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="http://some_url"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:value-of select= "/root/items/item/details/a:data/a:weight"/> </xsl:template> </xsl:stylesheet> 

When this transformation is applied (using Saxon 6.5.4 or any other compliant XSLT 1.0 processor) on the provided XML document:

<root> <items> <item> <title>Item</title> <details> <data xmlns="http://some_url"> <length>10</length> <weight>1.2</weight> </data> </details> </item> </items> </root> 

The correct/wanted node is selected and its string value is copied to the output:

1.2 
Sign up to request clarification or add additional context in comments.

Comments

0

In XPath, you have to assign a prefix to the namespace. How you do that depends on the XPath software/library you are using, but assuming you associate the namespace URI http://some_url with the namespace prefix someUrl, you can change your XPath expression as follows:

/root/items/item/details/someUrl:data 

2 Comments

Thank for the reply. I'm using SAXON and XSL 1.0. I tried to query xpath "/root/items/item/details/a:data" and it printed all content of "data" element. When I queried "/root/items/item/details/a:data/a:weight" I expected "1.2" to be printed but it again printed all content of "data" element. Could you please advise what am I doing wrong?
As you are using XSL, could you please show your complete XSL stylesheet? (or at least a minimized version that is reasonably complete?)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.