Subject: | Cannot use id("Foo") |
If use XPath 'id("foo")' then follow error is occurred.
Can't locate object method "getElementById" via package
"HTML::TreeBuilder::XPath" at
/usr/local/share/perl/5.8.8/XML/XPathEngine/Function.pm line 121.
please add the method HTML::TreeBuilder::XPath->getElementById.
follow is a patch for library and test.
__END__
=== lib/HTML/TreeBuilder/XPath.pm
==================================================================
--- lib/HTML/TreeBuilder/XPath.pm (revision 7244)
+++ lib/HTML/TreeBuilder/XPath.pm (local)
@@ -11,6 +11,10 @@
use base( 'HTML::TreeBuilder');
+sub getElementById {
+ my ($self, $id) = @_;
+ ( $self->findnodes(qq{//*[\@id="$id"]}) )[0];
+}
package HTML::TreeBuilder::XPath::Node;
=== t/HTML-TreeBuilder-XPath.t
==================================================================
--- t/HTML-TreeBuilder-XPath.t (revision 7244)
+++ t/HTML-TreeBuilder-XPath.t (local)
@@ -3,7 +3,7 @@
#########################
-use Test::More tests => 17;
+use Test::More tests => 18;
BEGIN { use_ok('HTML::TreeBuilder::XPath') };
#########################
@@ -53,6 +53,8 @@
}
+is( $html->findvalue('id("foo")/@id'), 'foo', 'id function');
+
__END__
/html/body/h1 1 Example header
//@id[.="toto"] 2 toto