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, %options);
31
32 Shortcut for "HTML::Selector->new(shift)->to_xpath(@_)". Exported
33 upon request.
34
35 new
36 $sel = HTML::Selector::XPath->new($selector, %options);
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
48 The optional "prefix" option allows you to specify a namespace
49 prefix for the generated XPath expression.
50
52 parse_pseudo
53 This method is called during xpath construction when we encounter a
54 pseudo selector (something that begins with comma). It is passed
55 the selector and a reference to the string we are parsing. It
56 should return one or more xpath sub-expressions to add to the parts
57 if the selector is handled, otherwise return an empty list.
58
60 CSS SELECTOR VALIDATION
61 This module doesn't validate whether the original CSS Selector
62 expression is valid. For example,
63
64 div.123foo
65
66 is an invalid CSS selector (class names should not begin with numbers),
67 but this module ignores that and tries to generate an equivalent XPath
68 expression anyway.
69
71 Tatsuhiko Miyagawa 2006-2011
72
73 Max Maischein 2011-
74
76 Tatsuhiko Miyagawa <miyagawa@bulknews.net>
77
78 Most of the logic is based on Joe Hewitt's getElementsBySelector.js on
79 <http://www.joehewitt.com/blog/2006-03-20.php> and Andrew Dupont's
80 patch to Prototype.js on <http://dev.rubyonrails.org/ticket/5171>, but
81 slightly modified using Aristotle Pegaltzis' CSS to XPath translation
82 table per <http://plasmasturm.org/log/444/>
83
84 Also see
85
86 <http://www.mail-archive.com/www-archive@w3.org/msg00906.html>
87
88 and
89
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>
98 <http://use.perl.org/~miyagawa/journal/31090>
99 <https://en.wikibooks.org/wiki/XPath/CSS_Equivalents>
100
101
102
103perl v5.34.0 2021-07-22 HTML::Selector::XPath(3)