1HTML::Selector::XPath(3U)ser Contributed Perl DocumentatiHoTnML::Selector::XPath(3)
2
3
4
6 HTML::Selector::XPath - CSS Selector to XPath compiler
7
9 use HTML::Selector::XPath;
10
11 my $selector = HTML::Selector::XPath->new("li#main");
12 $selector->to_xpath; # //li[@id='main']
13
14 # functional interface
15 use HTML::Selector::XPath 'selector_to_xpath';
16 my $xpath = selector_to_xpath('div.foo');
17
18 my $relative = selector_to_xpath('div.foo', root => '/html/body/p' );
19 # /html/body/p/div[contains(concat(' ', @class, ' '), ' foo ')]
20
21 my $relative = selector_to_xpath('div:root', root => '/html/body/p' );
22 # /html/body/p/div
23
25 HTML::Selector::XPath is a utility function to compile full set of CSS2
26 and partial CSS3 selectors to the equivalent XPath expression.
27
29 selector_to_xpath
30 $xpath = selector_to_xpath($selector);
31
32 Shortcut for "HTML::Selector->new(shift)->to_xpath(@_)". Exported
33 upon request.
34
35 new
36 $sel = HTML::Selector::XPath->new($selector);
37
38 Creates a new object.
39
40 to_xpath
41 $xpath = $sel->to_xpath;
42 $xpath = $sel->to_xpath(root => "."); # ./foo instead of //foo
43
44 Returns the translated XPath expression. You can optionally pass
45 "root" parameter, to specify which root to start the expression. It
46 defaults to "/".
47
49 parse_pseudo
50 This method is called during xpath construction when we encounter a
51 pseudo selector (something that begins with comma). It is passed
52 the selector and a reference to the string we are parsing. It
53 should return one or more xpath sub-expressions to add to the parts
54 if the selector is handled, otherwise return an empty list.
55
57 CSS SELECTOR VALIDATION
58 This module doesn't validate whether the original CSS Selector
59 expression is valid. For example,
60
61 div.123foo
62
63 is an invalid CSS selector (class names should not begin with numbers),
64 but this module ignores that and tries to generate an equivalent XPath
65 expression anyway.
66
68 Tatsuhiko Miyagawa 2006-2011
69
70 Max Maischein 2011-
71
73 Tatsuhiko Miyagawa <miyagawa@bulknews.net>
74
75 Most of the logic is based on Joe Hewitt's getElementsBySelector.js on
76 http://www.joehewitt.com/blog/2006-03-20.php
77 <http://www.joehewitt.com/blog/2006-03-20.php> and Andrew Dupont's
78 patch to Prototype.js on <http://dev.rubyonrails.org/ticket/5171>, but
79 slightly modified using Aristotle Pegaltzis' CSS to XPath translation
80 table per <http://plasmasturm.org/log/444/>
81
82 Also see
83
84 http://www.mail-archive.com/www-archive@w3.org/msg00906.html
85 <http://www.mail-archive.com/www-archive@w3.org/msg00906.html>
86
87 and
88
89 http://kilianvalkhof.com/2008/css-xhtml/the-css3-not-selector/
90 <http://kilianvalkhof.com/2008/css-xhtml/the-css3-not-selector/>
91
93 This library is free software; you can redistribute it and/or modify it
94 under the same terms as Perl itself.
95
97 http://www.w3.org/TR/REC-CSS2/selector.html <http://www.w3.org/TR/REC-
98 CSS2/selector.html> <http://use.perl.org/~miyagawa/journal/31090>
99
100
101
102perl v5.12.4 2011-10-16 HTML::Selector::XPath(3)