From: Aaron Patterson Date: 2009-01-11T03:22:13+09:00 Subject: Re: Help with HTML parsing On Sat, Jan 10, 2009 at 06:49:32PM +0900, Phlip wrote: > Tom Morris wrote: > >> XPath that solves this: >> contains(concat(' ', @class, ' '), ' w0 ') >> >> You can join multiple classes together like this: >> contains(concat(' ', @class, ' '), ' w0 ') && contains(concat(' ', >> @class, ' '), ' g ') >> >> It's not pretty. I've written it too much in my life. >> I do wish XPath had a class selector. > > Noted. I'm writing an XPath DSL, above the level of a raw XML library, > and I just added to its pending feature list these line-items: > > # TODO :class => :symbol should do the trick contains(concat(' ', > @class, ' '), ' w0 ') > # TODO :class => [] should do the trick contains(concat(' ', @class, ' > '), ' w0 ') && contains(concat(' ',@class, ' '), ' g ') > # TODO :class => a string should be raw. > > Those won't fix low-level XPath, but they will be useful when my DSL > targets XHTML... Couldn't you use CSS selectors when targeting XHTML (or XML even)? In Nokogiri, I have a CSS to XPath converter and just push the XPath down to libxml. For example: require 'nokogiri' doc = Nokogiri::HTML(<<-eoxml)
Hello
eoxml puts doc.at('.one.two').content It also doesn't care if you're using XML or HTML. The CSS selectors will get converted to XPath either way. -- Aaron Patterson http://tenderlovemaking.com/