Saturday 16 October 2010

Even more robust XPath to go with Nokogiri

I'm not big on XPath and truthfully only using it when I do screen scraping with Nokogiri (Blimey! I know). Anyway, I found XPath to be clever and surprisingly simple. Here below one of its examples :

//*[@id='someid']/tr[contains(.,'somestring') and not(contains(.,'otherstring'))]

We can mix match almost any HTML elements in it, it's very readable we can describe what we want like describing something to another human being. Say, we want filled fields on a row containing the word 'Usage', this row is placed on a table with class = 'billing_table', and the table is put inside a div with id = 'counterbox'. Here's how it will look like on XPath :

//div[@id='counterbox']/table[@class='billing_table']/tr[contains(.,'Usage')]/td[not(.,contains(''))]


Have fun with XPath! ^^

No comments:

Post a Comment