Search code examples
rubyxpathnokogiri

Which version of xpath that Nokogiri support?


I can't find an official statement of the xpath version that Nokogiri supports. Anyone can help me with it? In fact I want to extract some elements that have an attribute start with specified sub string. For example, I want to get all Book elements that have a category attribute start with the character C. How to do this with nokogiri?

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy?-->
<bookstore>

<book category="COOKING">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>

<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

<book category="WEB">
  <title lang="en">XQuery Kick Start</title>
  <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
</book>

<book category="WEB">
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>2003</year>
  <price>39.95</price>
</book>

</bookstore>

Solution

  • I don't know which specific version of XPath Nokogiri supports. But, you can do this:

    I want to get all book elements that have a category attribute start with the character C.

    using XPath's starts-with:

    doc = Nokogiri::XML(your_xml)
    doc.search('//book[starts-with(@category, "C")]').each { |e| puts e['category'] }
    # output is:
    # COOKING
    # CHILDREN
    

    You could also use a CSS3 "begins with" selector:

    doc = Nokogiri::XML(your_xml)
    doc.search('book[category^=C]').each { |e| puts e['category'] }
    # output is:
    # COOKING
    # CHILDREN