The manakai project

Web::XPath::Evaluator

XPath 1.0 evaluator

SYNOPSIS

  use Web::XPath::Evaluator;
  $eval = Web::XPath::Evaluator->new;
  $value = $eval->evaluate ($parsed_expression);

METHODS

Following methods are available:

$eval = Web::XPath::Evaluator->new

Return a new evaluator.

$code = $eval->onerror
$eval->onerror ($new_code)

Get or set the error handler for the evaluator. Any error, as well as warning and information, is reported to the handler. See Whatpm::Errors <https://github.com/wakaba/manakai/blob/master/lib/Whatpm/Errors.pod> for more information.

$package = $eval->function_library
$eval->function_library ($package)

Get or set the package name of the function library used to evaluate the XPath expression. It must be a Perl package name string, which identifies Web::XPath::FunctionLibrary, its subclass, or equivalent. The module is required if necessary. See Web::XPath::FunctionLibrary for details.

$vars = $eval->variable_bindings
$eval->variable_bindings ($vars)

Get or set the Web::XPath::VariableBindings object, containing the set of variable bindings used to evaluate XPath expressions.

$value = $eval->evaluate ($parsed_expr, $context_node, context_size => $n, context_position => $n)

Evaluate an expression.

The first argument must be a parsed XPath 1.0 expression data structure (See Web::XPath::Parser).

The second argument must be the context node.

Additionally, following key/value pairs can be specified: context_size is a Perl integer used as the context size; context_position is a Perl integer used as the context position. The context size must be greater than or equal to the context position. The context position must be positive. Their default is 1.

Any error during the evaluation of the expression is reported through the onerror handler. If a fatal error is encountered, the method returns undef. Otherwise, the result XPath value of the evaluation is returned.

$value = $self->to_xpath_boolean ($boolean)
$value = $self->to_xpath_number ($number)
$value = $self->to_xpath_string ($string)

Convert the specified Perl boolean, number, or string to equivalent XPath value.

$value = $self->to_xpath_node_set ([$node, $node, ...])

Create an XPath node-set from the specified array reference of zero or more nodes.

$value = $self->to_boolean ($value)
$value = $self->to_number ($value)
$value = $self->to_string ($value)

Convert the specified XPath value into boolean, number, or string, by the boolean, number(), or string() function, respectively. The argument must be an XPath value. If the argument is already of the specified type, it is returned as is. Otherwise a new value is returned.

Any error during the conversion is reported through the onerror handler. If a fatal error is encountered, the method returns undef.

$value = $self->to_string_value ($node)

Return an XPath string value representing the string-value of the specified node. The argument must be a node.

$self->sort_node_set ($value)

Sort the nodes in the node-set. The argument must be an XPath value. If the value is not a node-set, it is unchanged. If the nodes are already sorted, it is unchanged. Otherwise, the nodes are sorted in place by document order or reverse document order, depending on the reversed flag of the value.

VALUES

An XPath 1.0 value is represented as a hash reference.

Boolean

A boolean value is represented as following key/value pairs: type is boolean; value is a Perl boolean value, representing the boolean value.

Number

A number value is represented as following key/value pairs: type is number; value is a Perl number, representing the number value. Note that value can be a number value representing nan, -nan, inf, -inf, 0, or -0.

String

A string value is represented as following key/value pairs: type is string; value is a Perl string, representing the string value. Note that if value is not utf8-flagged, it is interpreted as Latin-1 string as usual.

Node-set

A node-set value is represented as following key/value pairs: type is node-set; value is an array reference, containing the nodes in the node-set; unordered is a Perl boolean value, representing whether the nodes are unsorted or not; reversed is a Perl boolean value, representing whether the nodes are sorted in reverse order or not.

If unordered is true, whether the nodes in the value is sorted or not is unknown. Otherwise, if reversed is true, the nodes in the value is sorted in reverse document order. Otherwise, the nodes in the value is sorted in document order.

Other types

No non-basic data type is supported.

NODES

The node must be a DOM node, whose implementation conforming the manakai's DOM Perl binding specification. In addition, the implementation must also support the manakaiIsHTML attribute of the Document interface and the manakaiTagName attribute of the Node interface. Web::DOM::Document and related modules in the web-dom package <https://github.com/manakai/perl-web-dom> is such a DOM implementation.

SEE ALSO

Web::XPath::Parser.

SPECIFICATIONS

XPATH

XML Path Language (XPath) Version 1.0 <http://www.w3.org/TR/xpath/>.

XML Path Language (XPath) Version 1.0 Specification Errata <http://www.w3.org/1999/11/REC-xpath-19991116-errata/>.

HTML Standard - Interactions with XPath and XSLT <http://www.whatwg.org/specs/web-apps/current-work/#interactions-with-xpath-and-xslt>.

The / at the beginning of the location path MUST select the root of the context node.

If an operand of = or != is an object of a type other than the four basic types, it is converted in a way that is dependent on that type. It is an error if it is not defined.

The document order is defined in terms of the compareDocumentPosition method of the Node interface [DOM] as follows. If nodeA.compareDocumentPosition (nodeB) & DOCUMENT_POSITION_PRECEDING is true, nodeA preceeds nodeB. Otherwise, if nodeA.compareDocumentPosition (nodeB) & DOCUMENT_POSITION_FOLLOWING is true, nodeA follows nodeB. Otherwise, they are same in document order.

If an error is found during the evaluation of an expression, the evaluation MUST be immediately aborted.

DOM

DOM Standard <http://dom.spec.whatwg.org/>.

HTML

HTML Standard - Interaction of template elements with XSLT and XPath <http://www.whatwg.org/specs/web-apps/current-work/#template-XSLT-XPath>.

AUTHOR

Wakaba <wakaba@suikawiki.org>.

LICENSE

Copyright 2013-2014 Wakaba <wakaba@suikawiki.org>.

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