The manakai project

Web::XPath::FunctionLibrary

XPath 1.0 Core Function Library

DESCRIPTION

The Web::XPath::FunctionLibrary module contains an implementation of the XPath 1.0 Core Function Library. It can be used directly, or by subclassing and adding more functions.

METHODS

The module that can be used as the function library in parsing or evaluating of expressions must implement the following class methods:

[$min, $max] = Web::XPath::FunctionLibrary->get_argument_number ($nsurl, $ln)

Return whether the specified function is defined or not, and if defined, the minimum and maximum numbers of the arguments for the function.

This method is invoked with two character string arguments: namespace URL and local name. If the namespace URL is undef, it represents the null namespace. Note that the empty string is different from undef. If the function identified by the namespace URL and local name is not defined, the method must return undef.

If the function is defined, the method must return an array reference with two non-negative integers. The first number represents the minimum number of arguments. The second number represents the maximum number of arguments. The first number must be less than or equal to the second number.

The method must not throw any exception. It must always return consistent result.

$code = Web::XPath::FunctionLibrary->get_code ($nsurl, $ln)

Return the implementation of the specified function.

This method is invoked with two character string arguments: namespace URL and local name. If the namespace URL is undef, it represents the null namespace. Note that the empty string is different from undef. If the function identified by the namespace URL and local name is not defined, the method must return undef.

If the function is defined, the method must return a code reference. The code reference will be invoked with three arguments: $self, $args, and $context.

The $self argument is the Web::XPath::Evaluator object on which the XPath expression is evaluated. The code may invoke (non-private) methods on the object, and it may report any error by invoking the $self->onerror code reference.

The $args argument is an array reference, containing the XPath values obtained by evaluating the argument expressions of the function, in order. The code must not modify them.

The $context argument is a hash reference, containing the following key/value pairs: node is the context node; size is the context size, represented as a Perl number; position is the context position, represented as a Perl number. The code must not modify them.

If the code reports a must-level error, it may return the undef value. If the undef value is returned, the evaluation is immediately terminated. Otherwise, the code must return an XPath value. The value is used as the return value of the function.

See Web::XPath::Evaluator for details on how XPath values are represented.

The method and the code reference returned by the method must not throw any exception. They must always return consistent result.

These methods must return consistent result.

FUNCTIONS

This module implements the following XPath functions from the XPath 1.0 Core Function Library: boolean(), ceiling(), concat(), contains(), false(), floor(), lang(), normalize-space(), not(), number(), round(), starts-with(), string(), string-length(), substring(), substring-after(), substring-before(), sum(), translate(), and true().

Handling of surrogate characters

Web browsers use UTF-16 as its internal character code, while Perl uses UTF-8. This difference affects behavior of some functions when arguments or relevant values contain one or more surrogate code points and/or characters that are represented by surrogate code points in UTF-16 (i.e. U+D800-DFFF and U+10000-U+10FFFF).

Following functions are implemented in terms of characters: concat(), string(), and translate(), and string match operations (including the = operation, the substring-before() function and so on). In other words, they are implemented in the same way as XPath implementations strictly conforming to the XPath 1.0 specification and their result can be different from those in Web browsers.

Following functions are implemented in terms of code points: string-length() and substring(). That is, their results are same as Web browsers and can be different from strict XPath 1.0 implementations.

For more information on handling of surrogates, see manakai's DOM Perl Binding specification [MANAKAI].

SEE ALSO

Web::XPath::Parser, Web::XPath::Evaluator.

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/>.

For the purpose of the number() function, whitespace is defined as the production rule S [XML].

It is an error if the argument to the number(), boolean(), or string() function is not of a supported type.

For the purpose of the lang() function, the xml:lang attribute value MUST be compared ASCII case-insensitively.

For the purpose of the sum() function, convertion of a string to a number MUST be done by the same way as the number() function, and the calculation of the sum MUST be done by adding the numbers to positive zero in the same way as the + operator.

For the purpose of the id() function, the element whose unique ID is i MUST be obtained by the following steps:

  1. If I<i> is the empty string, return null and abort these steps.

  2. Otherwise, evaluate C</descendant-or-self::*[dom:id () = $i][1]>
     where C<dom:id()> is a function that returns the unique
     identifier (ID) of the element [DOM] and I<$i> is string I<i>.

  3. If the previous step results in an empty node-set, return null.
     Otherwise, return the node in the node-set.

For the purpose of the substring() function and the string-length() function, UTF-16 code units MUST be considered as characters.

For a node whose type is not defined by XPath 1.0 specification, the string-value of the node is the empty string. It has no expanded-name.

XML

Extensible Markup Language (XML) 1.0 <http://www.w3.org/TR/xml/>.

DOM

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

MANAKAI

manakai's DOM Perl Binding <http://suika.suikawiki.org/~wakaba/wiki/sw/n/manakai's%20DOM%20Perl%20Binding>.

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.