Codex

XML::LibXML::XPathContext

Section: User Contributed Perl Documentation (3pm)

Updated: 2014-04-12

Index?action=index Return to Main Contents


NAME

XML::LibXML::XPathContext - XPath Evaluation

SYNOPSIS

EXAMPLES

Namespaces

This example demonstrates method. It finds all paragraph nodes in an XHTML document.

my $xc = XML::LibXML::XPathContext->new($xhtml_doc); $xc->registerNs('xhtml', 'http://www.w3.org/1999/xhtml'); my @nodes = $xc->findnodes('//xhtml:p');

@]

Custom XPath functions

This example demonstrates method by defining a function filtering nodes based on a Perl regular expression:

Variables

This example demonstrates method. We use XPath variables to recycle results of previous evaluations:

METHODS

new

Creates a new XML::LibXML::XPathContext object without a context node.

Creates a new XML::LibXML::XPathContext object with the context node set to .

:

registerNs
Registers namespace to .

:

unregisterNs
Unregisters namespace .

:

lookupNs
Returns namespace URI registered with . If is not registered to any namespace URI returns .

:

registerVarLookupFunc
Registers variable lookup function . The registered function is executed by the XPath engine each time an XPath variable is evaluated. It takes three arguments: , variable name, and variable ns-URI and must return one value: a number or string or any object that can be a result of findnodes: Boolean, Literal, Number, Node (e.g. Document, Element, etc.), or NodeList. For convenience, simple (non-blessed) array references containing only XML::LibXML::Node objects can be used instead of an XML::LibXML::NodeList.

:

getVarLookupData
Returns the data that have been associated with a variable lookup function during a previous call to .

:

getVarLookupFunc
Returns the variable lookup function previously registered with .

:

unregisterVarLookupFunc

Unregisters variable lookup function and the associated lookup data.

:

registerFunctionNS
Registers an extension function in namespace. must be a CODE reference. The arguments of the callback function are either simple scalars or objects depending on the XPath argument types. The function is responsible for checking the argument number and types. Result of the callback code must be a single value of the following types: a simple scalar (number, string) or an arbitrary object that can be a result of findnodes: Boolean, Literal, Number, Node (e.g. Document, Element, etc.), or NodeList. For convenience, simple (non-blessed) array references containing only XML::LibXML::Node objects can be used instead of a XML::LibXML::NodeList.

:

unregisterFunctionNS
Unregisters extension function in namespace. Has the same effect as passing as to registerFunctionNS.

:

registerFunction
Same as but without a namespace.

:

unregisterFunction
Same as but without a namespace.

:

findnodes

Performs the xpath statement on the current node and returns the result as an array. In scalar context, returns an XML::LibXML::NodeList object. Optionally, a node may be passed as a second argument to set the context node for the query.

The xpath expression can be passed either as a string, or as a XML::LibXML::XPathExpression object.

:

find
Performs the xpath expression using the current node as the context of the expression, and returns the result depending on what type of result the XPath expression had. For example, the XPath 52"

@] results in an 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.

The xpath expression can be passed either as a string, or as a XML::LibXML::XPathExpression object.

:

findvalue

Is exactly equivalent to:

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.

The xpath expression can be passed either as a string, or as a XML::LibXML::XPathExpression object.

:

exists

This method behaves like findnodes, except that it only returns a boolean value (1 if the expression matches a node, 0 otherwise) and may be faster than findnodes, because the XPath evaluation may stop early on the first match (this is true for libxml2 >= 2.6.27).

For XPath expressions that do not return node-set, the method returns true if the returned value is a non-zero number or a non-empty string.

:

setContextNode

Set the current context node.

:

getContextNode

Get the current context node.

:

setContextPosition
Set the current context position. By default, this value is -1 (and evaluating XPath function in the initial context raises an XPath error), but can be set to any value up to context size. This usually only serves to cheat the XPath engine to return given position when XPath function is called. Setting this value to -1 restores the default behavior.

:

getContextPosition

Get the current context position.

:

setContextSize
Set the current context size. By default, this value is -1 (and evaluating XPath function in the initial context raises an XPath error), but can be set to any non-negative value. This usually only serves to cheat the XPath engine to return the given value when XPath function is called. If context size is set to 0, position is automatically also set to 0. If context size is positive, position is automatically set to 1. Setting context size to -1 restores the default behavior.

:

getContextSize

Get the current context size.

:

setContextNode

Set the current context node.

:

BUGS AND CAVEATS

XML::LibXML::XPathContext objects are reentrant, meaning that you can call methods of an XML::LibXML::XPathContext even from XPath extension functions registered with the same object or from a variable lookup function. On the other hand, you should rather avoid registering new extension functions, namespaces and a variable lookup function from within extension functions and a variable lookup function, unless you want to experience untested behavior.

AUTHORS

Ilya Martynov and Petr Pajas, based on XML::LibXML and XML::LibXSLT code by Matt Sergeant and Christian Glahn.

HISTORICAL REMARK

Prior to XML::LibXML 1.61 this module was distributed separately for maintenance reasons.

AUTHORS

Matt Sergeant, Christian Glahn, Petr Pajas

VERSION

2.0116

COPYRIGHT

2001-2007, AxKit.com Ltd.

2002-2006, Christian Glahn.

2006-2009, Petr Pajas.

LICENSE

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


Index

NAME

SYNOPSIS

EXAMPLES

Namespaces

Custom XPath functions

Variables

METHODS

BUGS AND CAVEATS

AUTHORS

HISTORICAL REMARK

AUTHORS

VERSION

COPYRIGHT

LICENSE