From: Petr P. <pa...@us...> - 2003-04-11 16:39:37
|
Update of /cvsroot/perl-xml/XML-LibXML-XPathContext In directory sc8-pr-cvs1:/tmp/cvs-serv2279 Modified Files: XPathContext.pm Changes Log Message: Index: XPathContext.pm =================================================================== RCS file: /cvsroot/perl-xml/XML-LibXML-XPathContext/XPathContext.pm,v retrieving revision 1.20 retrieving revision 1.21 diff -u -d -r1.20 -r1.21 --- XPathContext.pm 4 Apr 2003 09:08:46 -0000 1.20 +++ XPathContext.pm 11 Apr 2003 16:39:32 -0000 1.21 @@ -19,13 +19,20 @@ $USE_LIBXML_DATA_TYPES = 0; sub findnodes { - my ($self, $xpath) = @_; + my ($self, $xpath, $node) = @_; my @nodes; + my $prev_node; + if (ref($node)) { + $prev_node=$self->getContextNode(); + $self->setContextNode($node); + } $self->_enter; eval { @nodes = $self->_findnodes($xpath); }; $self->_leave; + $self->setContextNode($prev_node) if ref($node); + if ($@) { die $@; } if (wantarray) { @@ -37,19 +44,26 @@ } sub findvalue { - my ($self, $xpath) = @_; - return $self->find($xpath)->to_literal->value; + my $self = shift; + return $self->find(@_)->to_literal->value; } sub find { - my ($self, $xpath) = @_; + my ($self, $xpath, $node) = @_; my ($type, @params); + my $prev_node; + if (ref($node)) { + $prev_node=$self->getContextNode(); + $self->setContextNode($node); + } $self->_enter; eval { ($type, @params) = $self->_find($xpath); }; $self->_leave; + $self->setContextNode($prev_node) if ref($node); + if ($@) { die $@; } if ($type) { @@ -148,9 +162,13 @@ $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); =head1 DESCRIPTION @@ -305,13 +323,15 @@ Same as I<unregisterFunctionNS> but without a namespace. -=item B<findnodes($xpath)> +=item B<findnodes($xpath, [ $context_node ])> Performs the xpath statement on the current node and returns the result as an array. In scalar context returns a -L<XML::LibXML::NodeList|XML::LibXML::NodeList> object. +L<XML::LibXML::NodeList|XML::LibXML::NodeList> object. Optionally, a +node may be passed as a second argument to set the context node for +the query. -=item B<find($xpath)> +=item B<find($xpath, [ $context_node ])> Performs the xpath expression using the current node as the context of the expression, and returns the result depending on what type of @@ -321,9 +341,12 @@ L<XML::LibXML::Boolean|XML::LibXML::Boolean> object, or a L<XML::LibXML::Literal|XML::LibXML::Literal> object (a string). Each of those objects uses Perl's overload feature to "do the right thing" -in different contexts. +in different contexts. Optionally, a node may be passed as a second +argument to set the context node for the query. + -=item B<findvalue($xpath)> + +=item B<findvalue($xpath, [ $context_node ])> Is exactly equivalent to: @@ -332,7 +355,8 @@ 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"/>. +<xsl:value-of select="some_xpath"/>. Optionally, a node may be passed +in the second argument to set the context node for the query. =item B<getContextNode()> Index: Changes =================================================================== RCS file: /cvsroot/perl-xml/XML-LibXML-XPathContext/Changes,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- Changes 4 Apr 2003 18:43:02 -0000 1.6 +++ Changes 11 Apr 2003 16:39:32 -0000 1.7 @@ -4,6 +4,9 @@ Not in CVS +* added optional $context_node argument to findnodes(), find(), and +findvalue() to simplify temporarily setting the context node. + * fixed bugs in new(), setContextNode(), and register* causing loosing stored context data passed from a variable if the variable itself gets changed |