Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use XPath to parse element name containing a colon

Tags:

xpath

So I'm trying to parse some of the WootAPI with XPath. A problem that I'm running into is that a couple of the elements have colons in their name, such as woot:price or woot:condition. Now, trying to use the XPath //rss/channel/item/woot:price won't grab the contents of the element woot:price, because of the colon, I think. What can I do to get it anyway?

like image 781
Chiggins Avatar asked Nov 26 '10 02:11

Chiggins


Video Answer


1 Answers

The colons are because the elements have a namespace prefix and are bound to the Woot namespace.

You should read up on XML namespaces and how they affect XPATH and XSLT.

If you want to reference the Woot elements in your XPATH you will either need to:

  1. Declare the Woot namespace http://www.woot.com/ so that when you use that namespace prefix in your XPATH it will be understood.
  2. Use a more generic XPATH statement that uses predicate filters that use local-name() and namespace-uri() to match the element.
    • //rss/channel/item/*[local-name()='price' and namespace-uri()='http://www.woot.com/']
like image 184
Mads Hansen Avatar answered Sep 30 '22 12:09

Mads Hansen