XML::LibXML::XPathContext - Perl interface to libxml2's xmlXPathContext |
XML::LibXML::XPathContext - Perl interface to libxml2's xmlXPathContext
use XML::LibXML::XPathContext;
my $xc = XML::LibXML::XPathContext->new; my $xc = XML::LibXML::XPathContext->new($node);
my $node = $xc->getContextNode; $xc->setContextNode($node);
$xc->registerNs($prefix, $namespace_uri); $xc->registerFunction($name, sub { ... }); $xc->registerFunctionNS($name, $namespace_uri, sub { ... }); $xc->registerVarLookupFunc(sub { ... }, $data);
$xc->unregisterNs($prefix); $xc->unregisterFunction($name); $xc->unregisterFunctionNS($name, $namespace_uri); $xc->unregisterVarLookupFunc($name);
my @nodes = $xc->findnodes($xpath); my @nodes = $xc->findnodes($xpath, $context_node); my $nodelist = $xc->findnodes($xpath); my $nodelist = $xc->findnodes($xpath, $context_node); my $result = $xc->find($xpath); my $result = $xc->find($xpath, $context_node); my $value = $xc->findvalue($xpath); my $value = $xc->findvalue($xpath, $context_node);
This module augments XML::LibXML by providing Perl interface to libxml2's xmlXPathContext structure. Besides just performing xpath statements on XML::LibXML's node trees it allows redefining certaint aspects of XPath engine. This modules allows
This example demonstrates registerNs() usage:
my $xc = XML::LibXML::XPathContext->new($xhtml_doc); $xc->registerNs('xhtml', 'http://www.w3.org/1999/xhtml'); my @nodes = $xc->findnodes('//xhtml:p');
This example demonstrates registerFunction() usage:
my $perlmatch = sub { die "Not a nodelist" unless $_[0]->isa('XML::LibXML::NodeList'); die "Missing a regular expression" unless defined $_[1];
my $nodelist = XML::LibXML::NodeList->new; my $i = 0; while(my $node = $_[0]->get_node($i)) { $nodelist->push($node) if $node->nodeName =~ $_[1]; $i ++; }
return $nodelist; };
my $xc = XML::LibXML::XPathContext->new($node); $xc->registerFunction('perlmatch', $perlmatch); my @nodes = $xc->findnodes('perlmatch(//*, "foo|bar")');
This example demonstrates registerVarLookup() usage:
sub var_lookup { my ($varname,$ns,$data)=@_; return $data->{$varname}; }
my $areas = XML::LibXML->new->parse_file('areas.xml'); my $empl = XML::LibXML->new->parse_file('employees.xml');
my $xc = XML::LibXML::XPathContext->new($empl);
my %results = ( A => $xc->find('/employees/employee[@salary>10000]'), B => $areas->find('/areas/area[district='Brooklyn']/street'), );
# get names of employees from $A woring in an area listed in $B $xc->registerVarLookupFunc(\&var_lookup, \%results); my @nodes = $xc->findnodes('$A[work_area/street = $B]/name');
undef
as $callback to registerFunctionNS.
1 * 3 + 52
results in a XML::LibXML::Number object being
returned. Other expressions might return a
XML::LibXML::Boolean object, or a
XML::LibXML::Literal object (a string). Each
of those objects uses Perl's overload feature to ``do the right thing''
in different contexts. Optionally, a node may be passed as a second
argument to set the context node for the query.
$node->find( $xpath )->to_literal;
That is, it returns the literal value of the results. This enables you to ensure that you get a string back from your search, allowing certain shortcuts. This could be used as the equivalent of <xsl:value-of select=``some_xpath''/>. Optionally, a node may be passed in the second argument to set the context node for the query.
XML::LibXML::XPathContext objects are not reentrant. It means you cannot register a Perl function with a XML::LibXML::XPathContext object if this Perl function uses itself the same XML::LibXML::XPathContext object internally.
For example, the following code will not work:
my $xc = XML::LibXML::XPathContext->new($node); $xc->registerFunction('func', sub { $xc->findvalue('1') }); my $result = $xc->findvalue('func()');
Based on XML::LibXML and XML::XSLT code by Matt Sergeant and Christian Glahn.
Maintained by Ilya Martynov and Petr Pajas.
Copyright 2001-2003 AxKit.com Ltd, All rights reserved.
For suggestions, bugreports etc. you may contact the maintainers directly (ilya@martynov.org and pajas@ufal.ms.mff.cuni.cz)
XML::LibXML::XPathContext issues can be discussed among other things on the perl XML mailing list (perl-xml@listserv.ActiveState.com)
XML::LibXML::XPathContext - Perl interface to libxml2's xmlXPathContext |