![]() |
![]()
| ![]() |
![]()
NAMEClass::XPath - adds xpath matching to object trees SYNOPSISIn your node class, use Class::XPath: # generate xpath() and match() using Class::XPath use Class::XPath get_name => 'name', # get the node name with the 'name' method get_parent => 'parent', # get parent with the 'parent' method get_root => \&get_root, # call get_root($node) to get the root get_children => 'kids', # get children with the 'kids' method get_attr_names => 'param', # get names and values of attributes get_attr_value => 'param', # from param get_content => 'data', # get content from the 'data' method ; Now your objects support XPath-esque matching: # find all pages, anywhere in the tree @nodes = $node->match('//page'); # returns an XPath like "/page[1]/paragraph[2]" $xpath = $node->xpath(); DESCRIPTIONThis module adds XPath-style matching to your object trees. This means that you can find nodes using an XPath-esque query with match() from anywhere in the tree. Also, the xpath() method returns a unique path to a given node which can be used as an identifier. To use this module you must already have an OO implementation of a tree. The tree must be a true tree - all nodes have a single parent and the tree must have a single root node. Also, the order of children within a node must be stable. NOTE: This module is not yet a complete XPath implementation. Over time I expect the subset of XPath supported to grow. See the SYNTAX documentation for details on the current level of support. USAGEThis module is used by providing it with information about how your class works. Class::XPath uses this information to build the match() and xpath() methods for your class. The parameters passed to 'use Class::XPath' may be set with strings, indicating method names, or subroutine references. They are:
ALTERNATE USAGEIf you're using someone else's OO tree module, and you don't want to subclass it, you can still use Class::XPath to add XPath matching to it. This is done by calling "Class::XPath-"add_methods()> with all the options usually passed to "use" and one extra one, "target". For example, to add xpath() and match() to HTML::Element (the node class for HTML::TreeBuilder): # add Class::XPath routines to HTML::Element Class::XPath->add_methods(target => 'HTML::Element', get_parent => 'parent', get_name => 'tag', get_attr_names => sub { my %attr = shift->all_external_attr; return keys %attr; }, get_attr_value => sub { my %attr = shift->all_external_attr; return $attr{$_[0]}; }, get_children => sub { grep { ref $_ } shift->content_list }, get_content => sub { grep { not ref $_ } shift->content_list }, get_root => sub { local $_=shift; while($_->parent) { $_ = $_->parent } return $_; }); Now you can load up an HTML file and do XPath matching on it: my $root = HTML::TreeBuilder->new; $root->parse_file("foo.html");1 # get a list of all paragraphs my @paragraphs = $root->match('//p'); # get the title element my ($title) = $root->match('/head/title'); GENERATED METHODSThis module generates two public methods for your class:
SYNTAXThis module supports a small subset of XPath at the moment. Here is a list of the type of expressions it knows about:
NOTE: this module has no support for Unicode. If this is a problem for you please consider sending me a patch. I'm certain that I don't know enough about Unicode to do it right myself. BUGSI know of no bugs in this module. If you find one, please file a bug report at: http://rt.cpan.org Alternately you can email me directly at sam@tregar.com. Please include the version of the module and a complete test case that demonstrates the bug. TODOPlanned future work:
ACKNOWLEDGMENTSI would like to thank the creators of XPath for their fine work and the W3C for supporting them in their efforts. The following people have sent me patches and/or suggestions: Tim Peoples Mark Addison Timothy Appnel COPYRIGHT AND LICENSECopyright (C) 2002 Sam Tregar This program is free software; you can redistribute it and/or modify it under the same terms as Perl 5 itself. AUTHORSam Tregar <sam@tregar.com> SEE ALSOThe XPath W3C Recommendation: http://www.w3.org/TR/xpath
|