Hpricot XML text search?

One of the ideas behind XPath is it allows us to navigate a DOM similarly to a disk directory.

One of the ideas behind XPath is it allows us to navigate a DOM similarly to a disk directory: require 'hpricot' xml = Book1 march 1 2010 Bob book2 october 4 2009 Bill book3 June 5 2010 Steve Book4 march 1 2010 Bob EOT doc = Hpricot(xml) titles = (doc / '//authortext()="Bob"/../title' ) titles # => # That means: "find all the books by Bob, then look up one level and find the title tag". I added an extra book by "Bob" to test getting all occurrences. To get the item containing a book by Bob, just move back up a level: items = (doc / '//authortext()="Bob"/..' ) puts items # => nil # >> # >> Book1 # >> march 1 2010 # >> Bob # >> # >> # >> Book4 # >> march 1 2010 # >> Bob # >> I also figured out what (doc % :rss % :channel / :item) is doing.It's equivalent to nesting the searches, minus the wrapping parenthesis, and these should all be the same in Hpricot-ese: (doc % :rss % :channel / :item).

Size # => 4 (((doc % :rss) % :channel) / :item). Size # => 4 (doc / '//rss/channel/item'). Size # => 4 (doc / 'rss channel item').

Size # => 4 Because '//rss/channel/item' is how you'd normally see an XPath accessor, and 'rss channel item' is a CSS accessor, I'd recommend using those formats for maintenance and clarity.

I was trying @items=(doc / "//rss/channel/item/authortext()='Bob'/../self::title or self::pubDate") according to link but that didn't work. – Dejan Feb 12 at 4:36 Well, your question says you want to "Find all title written by author Bob". If you want items move up one level and don't descend again.

I'll add an example. – the Tin Man Feb 12 at 5:35.

If you're not set on Hpricot, here's one way to do this with XPath in Nokogiri: require 'nokogiri' doc = Nokogiri::XML( my_rss_string ) bobs_titles = doc. Xpath("//titleparent::item/authortext()='Bob'") p bobs_titles. Map{ |node| node.

Text } #=> "Book1" Edit: @theTinMan's XPath also works well, is more readable, and may very well be faster: bobs_titles = doc. Xpath("//authortext()='Bob'/../title").

1 for Nokogiri. It's supported by a great group of Ruby developers and, like Homer Simpson's doughnuts, there's nothing it can't do. – the Tin Man Feb 11 at 23:34 1 Yeah nokogiri is great and I do not see a reason to run hpricot these days.

– Michael Papile Feb 12 at 0:20 1 +1 Michael Papile, I switched to Nokogiri from Hpricot after it exploded on some malformed RSS, and never looked back. – the Tin Man Feb 12 at 1:40.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions