XPath query for XML node with colon in node name

29,812

Solution 1

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']

Solution 2

What worked for me is:

/item/*[local-name()='thumbnail']

Solution 3

If you're looping an XmlNodeList array just use *[local-name()='thumbnail']

Share:
29,812
Izmoto
Author by

Izmoto

i speak fluent c#/c++

Updated on August 26, 2020

Comments

  • Izmoto
    Izmoto almost 4 years

    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.

  • bkk
    bkk about 11 years
    how to write the xpath if media:thumbnail had child element with media:image and we had to get media:image element
  • Dimitre Novatchev
    Dimitre Novatchev about 11 years
    @bhatkrishnakishor, Use //*[name()='media:thumbnail']/*[name() = 'media:image']
  • Skystrider
    Skystrider over 4 years
    /item/*[local-name()='thumbnail'] is what worked for me.
  • rawkintrevo
    rawkintrevo over 4 years
    While the accepted answer is a better answer in that it is more thorough and explains what is going on, this is the better answer because the solution works.
  • Skystrider
    Skystrider over 4 years
    lol ya that's because I don't really understand. I just noticed local-name() in completely unrelated xpath and since name() wasn't working for me I tried local-name() and it worked. So the accepted answer is much better, in situations where name() works.
  • qxotk
    qxotk over 4 years
    For whatever reason this did not work with .NET System.Xml namespace tools in C#. I had to specifically add the namespace to my XmlDoc object. By the way, the namespace it is using for "media:thumbnail" is: xmlns: media = 'search.yahoo.com/mrss'
  • Dushyanth Kandiah
    Dushyanth Kandiah almost 4 years
    I'm looping a XmlNodeList but on every loop, I get the same child node which is the first one, I have no clue why this happeing
  • Dimitre Novatchev
    Dimitre Novatchev almost 4 years
    @DushyanthKandiah: Please, ask a question and provide all relevant code and data. Also, explain there what you want to select with the XPath expression.
  • Dushyanth Kandiah
    Dushyanth Kandiah almost 4 years
    @DimitreNovatchev this line of code worked for me *[local-name()='thumbnail']
  • Dimitre Novatchev
    Dimitre Novatchev almost 4 years
    @DushyanthKandiah, This means that you have elements named thumbnail that are in more than one namespace. Generally I discourage people from using local-name() because they may not be aware of the different namespace issue and if they were, they would probably only want to select elements in a particular (not all) namespace.