Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath query for XML node with colon in node name

Tags:

xpath

What XPath query will select the <media:thumbnail /> node in the following XML?

<item>   <title>Sublime Federer crushes Wawrinka</title>   <description>Defending champion Roger Federer cruises past Stanislas Wawrinka 6-1 6-3 6-3 to take his place in the Australian Open semi-finals.</description>   <link>http://news.bbc.co.uk/go/rss/-/sport2/hi/tennis/9372592.stm</link>   <guid isPermaLink="false">http://news.bbc.co.uk/sport1/hi/tennis/9372592.stm</guid>   <pubDate>Tue, 25 Jan 2011 04:21:23 GMT</pubDate>   <category>Tennis</category>   <media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/50933000/jpg/_50933894_011104979-1.jpg"/> </item> 

The XML came from this RSS feed.

like image 428
Izmoto Avatar asked Jan 27 '11 13:01

Izmoto


People also ask

Can a XML tag have colon?

The colons indicate that those elements are in the ns1 namespace. This is needed when you are using multiple schemas. Assuming the document only uses ns1, it is essentially equivalent to not having any "ns1:" in those tags.

Which is a valid node type in XPath?

In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document nodes. XML documents are treated as trees of nodes. The topmost element of the tree is called the root element.


1 Answers

You need to learn about namespaces and how to define/register a namespace in your XPath engine so that you can then use the associated prefix for names in that registered namespace. There are plenty of questions in the xpath tag asking how to use names that are in a namespace -- with good answers. Search for them.

A very rough answer (ignoring namespaces at all) is:

//*[name()='media:thumbnail'] 
like image 51
Dimitre Novatchev Avatar answered Oct 07 '22 15:10

Dimitre Novatchev