chaosophist (kay-os-o-fist)

Chaosophistication – the subtle art of change

  • About
  • My Street Cred
  • What People Say
  • Made It
    • Edit
    • Delete
    • Tags
    • Autopost

    XML, Violence, Nokogiri and Xpath

    Sushi1

    I love Xpath.

     It makes XML easy to use and easy to query.  Gone are the days of parsing things with a SaxParser unless you're really hard up for control of you text.

     

    Also, I love the Ruby Nokogiri Gem.  

    XML is like violence - if it doesn’t solve your problems, you are not using enough of it.

    - Nokogiri docs

    But I do have to say that there is a lack of good examples and documentation for anything particularly advanced.  I found a working solution to my issue, but thought I'd paste here what I wanted to do versus what I ended up doing.

    Given the following XML, 

     

     

    I'd like to grab two elements that include "Cover" in the tag, and then operate on each of them.

    Nokogiri's use of Xpath easily allows the first query expression like so: price_xml = doc_xml.xpath('Container/Set/*[contains(name(), "Cover")]')

    I've selected all the elements (using *) in Set, and then used an Xpath Expression function:

    contains, in order to specify that Adult must be in the name.  This returns two Nokogiri XML Nodes in Nodeset.

     

    What I wanted to do was then select one of these elements based on a pattern in the tagname use my favorite tool, Xpath.

    But I just couldn't get Nokogiri to give it to me, and several solutions ending up selecting way more than the 1 element I wanted. (Because the nodes in the Nodeset still contain relationships with their parents)

     

    I'm cross posting this on StackOverflow as a question, just in case any Nokogiri Xpath enthusiasts want to recommend a solution that doesn't resort to find()

     http://stackoverflow.com/questions/8780791/how-to-use-xpath-with-nokogiri-to-...

     

     

    • 8 January 2012
    • Views
    • Permalink
    • Tweet
    • 0 responses
    • Like
    • Comment
  • Archive

    2012 (4)
    February (2)
    January (2)
    2011 (68)
    December (3)
    November (5)
    October (2)
    August (2)
    July (5)
    June (9)
    May (11)
    April (8)
    March (12)
    February (3)
    January (8)
    2010 (3)
    October (2)
    September (1)
    2009 (12)
    December (2)
    November (2)
    October (2)
    September (1)
    July (1)
    June (3)
    April (1)
    Tweets

    Sites I Like

    • 37 Signals
    • My Tech Tumblr
    • Dutch Rico
    • The Pros @ Adaptive Path
    • Martin Fowler /Thought Works
    • My Tweets
    • Kelway on UX
    • Petersen on UX/Product
    • Filipov on Travel
    • Travel Tech News
  • About Ashley Raiteri

    biz-technologist, data lover who dotes on his family, plays with tech. Europhile, travel junkie, startup-addict, CTO @ Flextrip (Tours & Activities Tech); Founding CTO of Everbread (Travel Search Tech).

  • Follow Me

      TwitterLinkedInTumblr

    Get Updates

    Follow this Space »
    You're following this Space (Edit)
    You're a contributor here (Edit)
    This is your Space (Edit)
    Follow by email »
    Get the latest updates in your email box automatically.
    Loading...
    Subscribe via RSS

Theme created for Posterous by Obox