My CSS is better than my xpath—if yours is too…

Monday, 15 March 2010

Each must be one line; tcsh quoting shown.

Install HTML::Selector::XPath.

cpan -i HTML::Selector::XPath

Then go crazy-

perl -MHTML::Selector::XPath -le 'print HTML::Selector::XPath->new("a[rel]")->to_xpath'


//a[@rel]
perl -MHTML::Selector::XPath -le 'print HTML::Selector::XPath->new("#content div > ul.links > a")->to_xpath'


//*[@id='content']//div/ul[contains(concat(' ', @class, ' '), ' links ')]/a

All purpose–

perl -MHTML::Selector::XPath -le 'print HTML::Selector::XPath->new(+shift)->to_xpath' 'body>ol>li>ul>li>b'


//body/ol/li/ul/li/b
digg stumbleupon del.icio.us reddit Fark Technorati Faves

Discussion

Comments


Sid Burn

Re: My CSS is better than my xpath—if yours is too…

You can also use "HTML::Query" if you want to use CSS Syntax on HTML Documents.

By Sid Burn on 16 March 2010 · 12:05
comment link · reply to this

Your information (required) Name*
Email*
Website

* Indicates required fields; email is used for validation and is not displayed on the site.

Your comment
Commenting on My CSS is better than my xpath—if yours is too…
Title

Body is limited to ≈1,000 words. Paragraphs—but not line breaks—are automatically inserted. Valid XHTML is required. These are the allowed tags–

<a href=""></a> <br/> <acronym title=""></acronym> <abbr title=""></abbr> <code></code> <pre></pre> <tt></tt> <ins></ins> <del></del> <hr/> <cite></cite> <b></b> <i></i> <sup></sup> <sub></sub> <strong></strong> <em></em> <h1></h1> <h2></h2> <h3></h3> <q></q> <blockquote></blockquote>