Bucket List

I’ve had a long running mental list of things I want to eventually do, but I thought it might be fun to post a few examples. This is only an “includes but is not limited to” sample, my list is pretty much endless, No order of priority implied: Lea…

I’ve had a long running mental list of things I want to eventually do, but I thought it might be fun to post a few examples.  This is only an “includes but is not limited to” sample, my list is pretty much endless, No order of priority implied:

  • Learn to fly a plane
  • Get reasonably good at Parkour
  • Visit Antarctica
  • Vist Tibet
  • Go flying in a wingsuit
  • Become competively good at Chess
  • Get a Phd in Physics
  • Go on a non-religuous service mission
  • Release a Musical Album that sells at least 1000 copies
  • Publish a fiction novel that sells at least 10,000 copies
  • Visit the great wall of China
  • Perform a life saving feat in a medical emergency
  • Learn to Wind Surf
  • Go Skydiving
  • Free Climb a 100 feet vertically (just enough to be life-threatening)
  • Learn how to do Yoga Breakdancing.

XML, Violence, Nokogiri and Xpath

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 you…

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, 

 

https://gist.github.com/1577136

 

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)

https://gist.github.com/1579343

 

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()