The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
<HTML><HEAD><TITLE>XML::XQL - A perl module for querying XML tree structures with XQL</TITLE></HEAD><BODY><H1><A NAME="NAME">NAME

</A></H1><P>XML::XQL - A perl module for querying XML tree structures with XQL

<P><HR><H1><A NAME="SYNOPSIS">SYNOPSIS

</A></H1><PRE> use XML::XQL;
 use XML::XQL::DOM;

</PRE><PRE> $parser = new XML::DOM::Parser;
 $doc = $parser-&gt;parsefile ("file.xml");

</PRE><PRE> # Return all elements with tagName='title' under the root element 'book'
 $query = new XML::XQL::Query (Expr =&gt; "book/title");
 @result = $query-&gt;solve ($doc);
 $query-&gt;dispose; # Avoid memory leaks - Remove circular references

</PRE><PRE> # Or (to save some typing)
 @result = XML::XQL::solve ("book/title", $doc);

</PRE><PRE> # Or (to save even more typing)
 @result = $doc-&gt;xql ("book/title");

</PRE><P><HR><H1><A NAME="DESCRIPTION">DESCRIPTION

</A></H1><P>The XML::XQL module implements the XQL (XML Query Language) proposal
submitted to the XSL Working Group in September 1998.
The spec can be found at: <A HREF="http://www.w3.org/TandS/QL/QL98/pp/xql.html">http://www.w3.org/TandS/QL/QL98/pp/xql.html</A>
Most of the contents related to the XQL syntax can also be found in the
<A HREF="XQL/Tutorial.html">XML::XQL::Tutorial</A> that comes with this distribution. 
Note that XQL is not the same as XML-QL!

<P>The current implementation only works with the <A HREF="DOM.html">XML::DOM</A> module, but once the
design is stable and the major bugs are flushed out, other extensions might
follow, e.g. for XML::Grove.

<P>XQL was designed to be extensible and this implementation tries to stick to that.
Users can add their own functions, methods, comparison operators and data types.
Plugging in a new XML tree structure (like XML::Grove) should be a piece of cake.

<P>To use the XQL module, either

<PRE>  use XML::XQL;

</PRE><P>or

<PRE>  use XML::XQL::Strict;

</PRE><P>The Strict module only provides the core XQL functionality as found in the
XQL spec. By default (i.e. by using XML::XQL) you get 'XQL+', which has
some additional features.

<P>See the section <A HREF="#Additional_Features_in_XQL_">Additional Features in XQL+</A> for the differences.

<P>This module is still in development. See the To-do list in XQL.pm for what
still needs to be done. Any suggestions are welcome, the sooner these 
implementation issues are resolved, the faster we can all use this module.

<P>If you find a bug, you would do me great favor by sending it to me in the
form of a test case. See the file t/xql_template.t that comes with this distribution.

<P>If you have written a cool comparison operator, function, method or XQL data 
type that you would like to share, send it to enno@att.com and I will
add it to this module.

<P><HR><H1><A NAME="XML_XQL_global_functions">XML::XQL global functions

</A></H1><DL><DT><A NAME="solve_QUERY_STRING_INPUT_LIST_"><STRONG><P>solve (QUERY_STRING, INPUT_LIST...)

</STRONG></A><PRE> @result = XML::XQL::solve ("doc//book", $doc);

</PRE><DD>This is provided as a shortcut for:

<PRE> $query = new XML::XQL::Query (Expr =&gt; "doc//book");
 @result = $query-&gt;solve ($doc);
 $query-&gt;dispose;

</PRE><P>Note that with <A HREF="XQL/DOM.html">XML::XQL::DOM</A>, you can also write (see <A HREF="DOM/Node.html">XML::DOM::Node</A>
for details):

<PRE> @result = $doc-&gt;xql ("doc//book");

</PRE><DT><A NAME="setDocParser_PARSER_"><STRONG><P>setDocParser (PARSER)

</STRONG></A><DD>Sets the XML::DOM::Parser that is used by the new XQL+ document() method.
By default it uses an XML::DOM::Parser that was created without any arguments,
i.e.

<PRE>  $PARSER = new XML::DOM::Parser;

</PRE><DT><A NAME="defineFunction_NAME_FUNCREF_ARGCOUNT_ALLOWED_OUTSIDE_CONST_QUERY_ARG_"><STRONG><P>defineFunction (NAME, FUNCREF, ARGCOUNT [, ALLOWED_OUTSIDE [, CONST, [QUERY_ARG]]])

</STRONG></A><DD>Defines the XQL function (at the global level, i.e. for all newly created 
queries) with the specified NAME. The ARGCOUNT parameter can either be a single
number or a reference to a list with numbers. 
A single number expands to [ARGCOUNT, ARGCOUNT]. The list contains pairs of 
numbers, indicating the number of arguments that the function allows. The value
-1 means infinity. E.g. [2, 5, 7, 9, 12, -1] means that the function can have
2, 3, 4, 5, 7, 8, 9, 12 or more arguments.
The number of arguments is checked when parsing the XQL query string.

<P>The second parameter must be a reference to a Perl function or an anonymous
sub. E.g. '\&amp;my_func' or 'sub { ... code ... }'

<P>If ALLOWED_OUTSIDE (default is 0) is set to 1, the function or method may 
also be used outside subqueries in <I>node queries</I>.
(See NodeQuery parameter in Query constructor)

<P>If CONST (default is 0) is set to 1, the function is considered to be 
"constant". See <A HREF="#Constant_Function_Invocations">Constant Function Invocations</A> for details.

<P>If QUERY_ARG (default is 0) is not -1, the argument with that index is
considered to be a 'query parameter'. If the query parameter is a subquery, 
that returns multiple values, the result list of the function invocation will
contain one result value for each value of the subquery. 
E.g. 'length(book/author)' will return a list of Numbers, denoting the string 
lengths of all the author elements returned by 'book/author'.

<P>Note that only methods (not functions) may appear after a Bang "!" operator.
This is checked when parsing the XQL query string.

<P>See also: defineMethod

<DT><A NAME="generateFunction_NAME_FUNCNAME_RETURN_TYPE_ARGCOUNT_ALLOWED_OUTSIDE_CONST_QUERY_ARG_"><STRONG><P>generateFunction (NAME, FUNCNAME, RETURN_TYPE [, ARGCOUNT [, ALLOWED_OUTSIDE [, CONST [, QUERY_ARG]]]])

</STRONG></A><DD>Generates and defines an XQL function wrapper for the Perl function with the
name FUNCNAME. The function name will be NAME in XQL query expressions.
The return type should be one of the builtin XQL Data Types or a class derived
from XML::XQL::PrimitiveType (see <A HREF="#Adding_Data_Types">Adding Data Types</A>.)
See defineFunction for the meaning of ARGCOUNT, ALLOWED_OUTSIDE, CONST and
QUERY_ARG.

<P>Function values are always converted to Perl strings with xql_toString before
they are passed to the Perl function implementation. The function return value
is cast to an object of type RETURN_TYPE, or to the empty list [] if the
result is undef. It uses expandType to expand XQL primitive type names.
If RETURN_TYPE is "*", it returns the function 
result as is, unless the function result is undef, in which case it returns [].

<DT><A NAME="defineMethod_NAME_FUNCREF_ARGCOUNT_ALLOWED_OUTSIDE_"><STRONG><P>defineMethod (NAME, FUNCREF, ARGCOUNT [, ALLOWED_OUTSIDE])

</STRONG></A><DD>Defines the XQL method (at the global level, i.e. for all newly created 
queries) with the specified NAME. The ARGCOUNT parameter can either be a single
number or a reference to a list with numbers. 
A single number expands to [ARGCOUNT, ARGCOUNT]. The list contains pairs of 
numbers, indicating the number of arguments that the method allows. The value
-1 means infinity. E.g. [2, 5, 7, 9, 12, -1] means that the method can have
2, 3, 4, 5, 7, 8, 9, 12 or more arguments.
The number of arguments is checked when parsing the XQL query string.

<P>The second parameter must be a reference to a Perl function or an anonymous
sub. E.g. '\&amp;my_func' or 'sub { ... code ... }'

<P>If ALLOWED_OUTSIDE (default is 0) is set to 1, the function or method may 
also be used outside subqueries in <I>node queries</I>.
(See NodeQuery parameter in Query constructor)

<P>Note that only methods (not functions) may appear after a Bang "!" operator.
This is checked when parsing the XQL query string.

<P>See also: defineFunction

<DT><A NAME="defineComparisonOperators_NAME_FUNCREF_NAME_FUNCREF_"><STRONG><P>defineComparisonOperators (NAME =&gt; FUNCREF [, NAME =&gt; FUNCREF]*)

</STRONG></A><DD>Defines XQL comparison operators at the global level.
The FUNCREF parameters must be a references to a Perl function or an anonymous
sub. E.g. '\&amp;my_func' or 'sub { ... code ... }'

<P>E.g. define the operators $my_op$ and $my_op2$:

<PRE> defineComparisonOperators ('my_op' =&gt; \&amp;my_op,
                            'my_op2' =&gt; sub { ... insert code here ... });

</PRE><DT><A NAME="defineElementValueConvertor_TAG_NAME_FUNCREF_"><STRONG><P>defineElementValueConvertor (TAG_NAME, FUNCREF)

</STRONG></A><DD>Defines that the result of the value() call for Elements with the specified
TAG_NAME uses the specified function. The function will receive
two parameters. The second one is the TAG_NAME of the Element node 
and the first parameter is the Element node itself.
FUNCREF should be a reference to a Perl function, e.g. \&amp;my_sub, or
an anonymous sub.

<P>E.g. to define that all Elements with tag name 'date-of-birth' should return
XML::XQL::Date objects:

<PRE>	defineElementValueConvertor ('date-of-birth', sub {
		my $elem = shift;
		# Always pass in the node as the second parameter. This is
		# the reference node for the object, which is used when
		# sorting values in document order.
		new XML::XQL::Date ($elem-&gt;xql_text, $elem); 
	});

</PRE><P>These convertors can only be specified at a global level, not on a per query
basis. To undefine a convertor, simply pass a FUNCREF of undef.

<DT><A NAME="defineAttrValueConvertor_ELEM_TAG_NAME_ATTR_NAME_FUNCREF_"><STRONG><P>defineAttrValueConvertor (ELEM_TAG_NAME, ATTR_NAME, FUNCREF)

</STRONG></A><DD>Defines that the result of the value() call for Attributes with the specified
ATTR_NAME and a parent Element with the specified ELEM_TAG_NAME 
uses the specified function. An ELEM_TAG_NAME of "*" will match regardless of
the tag name of the parent Element. The function will receive
3 parameters. The third one is the tag name of the parent Element (even if 
ELEM_TAG_NAME was "*"), the second is the ATTR_NAME and the first is the 
Attribute node itself.
FUNCREF should be a reference to a Perl function, e.g. \&amp;my_sub, or
an anonymous sub.

<P>These convertors can only be specified at a global level, not on a per query
basis. To undefine a convertor, simply pass a FUNCREF of undef.

<DT><A NAME="defineTokenQ_Q_"><STRONG><P>defineTokenQ (Q)

</STRONG></A><DD>Defines the token for the q// string delimiters at a global level.
The default value for XQL+ is 'q', for XML::XQL::Strict it is undef.
A value of undef will deactivate this feature.

<DT><A NAME="defineTokenQQ_QQ_"><STRONG><P>defineTokenQQ (QQ)

</STRONG></A><DD>Defines the token for the qq// string delimiters at a global level.
The default value for XQL+ is 'qq', for XML::XQL::Strict it is undef.
A value of undef will deactivate this feature.

<DT><A NAME="expandType_TYPE_"><STRONG><P>expandType (TYPE)

</STRONG></A><DD>Used internally to expand type names of XQL primitive types.
E.g. it expands "Number" to "XML::XQL::Number" and is not case-sensitive, so
"number" and "NuMbEr" will both expand correctly.

<DT><A NAME="defineExpandedTypes_ALIAS_FULL_NAME_"><STRONG><P>defineExpandedTypes (ALIAS, FULL_NAME [, ...])

</STRONG></A><DD>For each pair of arguments it allows the class name FULL_NAME to be abbreviated
with ALIAS. The definitions are used by expandType(). 
(ALIAS is always converted to lowercase internally, because expandType 
is case-insensitive.)

<P>Overriding the ALIAS for "date", also affects the object type returned by the
date() function.

<DT><A NAME="setErrorContextDelimiters_START_END_BOLD_ON_BOLD_OFF_"><STRONG><P>setErrorContextDelimiters (START, END, BOLD_ON, BOLD_OFF)

</STRONG></A><DD>Sets the delimiters used when printing error messages during query evaluation.
The default delimiters on Unix are `tput smul` (underline on) and `tput rmal`
(underline off). On other systems (that don't have tput), the delimiters are
"&gt;&gt;" and "&lt;&lt;" resp. 

<P>When printing the error message, the subexpression that caused the error will
be enclosed by the delimiters, i.e. underlined on Unix.

<P>For certain subexpressions the significant keyword, e.g. "$and$" is enclosed in 
the bold delimiters BOLD_ON (default: `tput bold` on Unix, "" elsewhere) and 
BOLD_OFF (default: (`tput rmul` . `tput smul`) on Unix, "" elsewhere, 
see $BoldOff in XML::XQL::XQL.pm for details.)

<DT><A NAME="isEmptyList_VAR_"><STRONG><P>isEmptyList (VAR)

</STRONG></A><DD>Returns 1 if VAR is [], else 0. Can be used in user defined functions.

</DL><P><HR><H1><A NAME="Additional_Features_in_XQL_">Additional Features in XQL+

</A></H1><DL><DT><A NAME="Parent_operator_"><STRONG><P>Parent operator '..'

</STRONG></A><DD>The '..' operator returns the parent of the current node, where '.' would
return the current node. This is not part of any XQL standard, because you
would normally use return operators, which are not implemented here.

<DT><A NAME="Sequence_operators_and_"><STRONG><P>Sequence operators ';' and ';;'

</STRONG></A><DD>The sequence operators ';' (precedes) and ';;' (immediately precedes) are
not in the XQL spec, but are described in 'The Design of XQL' by Jonathan Robie
who is one of the designers of XQL. It can be found at
<A HREF="http://www.texcel.no/whitepapers/xql-design.html">http://www.texcel.no/whitepapers/xql-design.html</A>
See also the XQL Tutorial for a description of what they mean.

<DT><A NAME="q_and_qq_String_Tokens_"><STRONG><P>q// and qq// String Tokens

</STRONG></A><DD>String tokens a la q// and qq// are allowed. q// evaluates like Perl's single 
quotes and qq// like Perl's double quotes. Note that the default XQL strings do
not allow escaping etc., so it's not possible to define a string with both
single and double quotes. If 'q' and 'qq' are not to your liking, you may
redefine them to something else or undefine them altogether, by assigning undef
to them. E.g:

<PRE> # at a global level - shared by all queries (that don't (re)define 'q')
 XML::XQL::defineTokenQ ('k');
 XML::XQL::defineTokenQQ (undef);

</PRE><PRE> # at a query level - only defined for this query
 $query = new XML::XQL::Query (Expr =&gt; "book/title", q =&gt; 'k', qq =&gt; undef);
 
</PRE><P>From now on k// works like q// did and qq// doesn't work at all anymore.

<DT><A NAME="Query_strings_can_have_embedded_Comments_"><STRONG><P>Query strings can have embedded Comments

</STRONG></A><DD>For example:

<PRE> $queryExpr = "book/title          # this comment is inside the query string
	       [. = 'Moby Dick']"; # this comment is outside 

</PRE><DT><A NAME="Optional_dollar_delimiters_and_case_insensitive_XQL_keywords_"><STRONG><P>Optional dollar delimiters and case-insensitive XQL keywords

</STRONG></A><DD>The following XQL keywords are case-insensitive and the dollar sign delimiters 
may be omitted: $and$, $or$, $not$, $union$, $intersect$, $to$, $any$, $all$,
$eq$, $ne$, $lt$, $gt$, $ge$, $le$, $ieq$, $ine$, $ilt$, $igt$, $ige$, $ile$.

<P>E.g. $AND$, $And$, $aNd$, and, And, aNd are all valid replacements for $and$.

<P>Note that XQL+ comparison operators ($match$, $no_match$, $isa$, $can$) still
require dollar delimiters and are case-sensitive.

<DT><A NAME="Comparison_operator_match_or_"><STRONG><P>Comparison operator: $match$ or '=~'

</STRONG></A><DD>E.g. "book/title =~ '/(Moby|Dick)/']" will return all book titles containing
Moby or Dick. Note that the match expression needs to be quoted and should
contain the // or m// delimiters for Perl.

<P>When casting the values to be matched, both are converted to Text.

<DT><A NAME="Comparison_operator_no_match_or_"><STRONG><P>Comparison operator: $no_match$ or '!~'

</STRONG></A><DD>E.g. "book/title !~ '/(Moby|Dick)/']" will return all book titles that don't 
contain Moby or Dick. Note that the match expression needs to be quoted and 
should contain the // or m// delimiters for Perl.

<P>When casting the values to be matched, both are converted to Text.

<DT><A NAME="Comparison_operator_isa_"><STRONG><P>Comparison operator: $isa$

</STRONG></A><DD>E.g. '//. $isa$ "XML::XQL::Date"' returns all elements for which the value() 
function returns an XML::XQL::Date object. (Note that the value() function can
be overridden to return a specific object type for certain elements and 
attributes.) It uses expandType to expand XQL primitive type names.

<DT><A NAME="Comparison_operator_can_"><STRONG><P>Comparison operator: $can$

</STRONG></A><DD>E.g. '//. $can$ "swim"' returns all elements for which the value() 
function returns an object that implements the (Perl) swim() method. 
(Note that the value() function can be overridden to return a specific object 
type for certain elements and attributes.)

<DT><A NAME="Function_once_QUERY_"><STRONG><P>Function: once (QUERY)

</STRONG></A><DD>E.g. 'once(id("foo"))' will evaluate the QUERY expression only once per query.
Certain query results (like the above example) will always return the same
value within a query. Using once() will cache the QUERY result for the
rest of the query. 

<P>Note that "constant" function invocations are always cached.
See also <A HREF="#Constant_Function_Invocations">Constant Function Invocations</A>

<DT><A NAME="Function_subst_QUERY_EXPR_EXPR_MODIFIERS_MODE_"><STRONG><P>Function: subst (QUERY, EXPR, EXPR [,MODIFIERS, [MODE]])

</STRONG></A><DD>E.g. 'subst(book/title, "[M|m]oby", "Dick", "g")' will replace Moby or moby
with Dick globally ("g") in all book title elements. Underneath it uses Perl's
substitute operator s///. Don't worry about which delimiters are used underneath.
The function returns all the book/titles for which a substitution occurred.
The default MODIFIERS string is "" (empty.) The function name may be abbreviated 
to "s".

<P>For most Node types, it converts the value() to a string (with xql_toString)
to match the string and xql_setValue to set the new value in case it matched.
For XQL primitives (Boolean, Number, Text) and other data types (e.g. Date) it 
uses xql_toString to match the String and xql_setValue to set the result. 
Beware that performing a substitution on a primitive that was found in the 
original XQL query expression, changes the value of that constant.

<P>If MODE is 0 (default), it treats Element nodes differently by matching and
replacing <I>text blocks</I> occurring in the Element node. A text block is defined
as the concatenation of the raw text of subsequent Text, CDATASection and 
EntityReference nodes. In this mode it skips embedded Element nodes.
If a text block matches, it is replaced by a single Text node, regardless
of the original node type(s).

<P>If MODE is 1, it treats Element nodes like the other nodes, i.e. it converts
the value() to a string etc. Note that the default implementation of value()
calls text(), which normalizes whitespace and includes embedded Element
descendants (recursively.) This is probably not what you want to use in most
cases, but since I'm not a professional psychic... :-)

<DT><A NAME="Function_map_QUERY_CODE_"><STRONG><P>Function: map (QUERY, CODE)

</STRONG></A><DD>E.g. 'map(book/title, "s/[M|m]oby/Dick/g; $_")' will replace Moby or moby
with Dick globally ("g") in all book title elements. Underneath it uses Perl's
map operator. The function returns all the book/titles for which a 
change occurred.

<P>??? add more specifics

<DT><A NAME="Function_eval_EXPR_TYPE_"><STRONG><P>Function: eval (EXPR [,TYPE])

</STRONG></A><DD>Evaluates the Perl expression EXPR and returns an object of the specified TYPE.
It uses expandType to expand XQL primitive type names.
If the result of the eval was undef, the empty list [] is returned.

<P>E.g. 'eval("2 + 5", "Number")' returns a Number object with the value 7, and
     'eval("%ENV{USER}")' returns a Text object with the user name.

<P>Consider using once() to cache the return value, when the invocation will 
return the same result for each invocation within a query.

<P>??? add more specifics

<DT><A NAME="Function_new_TYPE_QUERY_PAR_"><STRONG><P>Function: new (TYPE [, QUERY [, PAR] *])

</STRONG></A><DD>Creates a new object of the specified object TYPE. The constructor may have any
number of arguments. The first argument of the constructor (the 2nd argument 
of the new() function) is considered to be a 'query parameter'.
See defineFunction for a definition of <I>query parameter</I>.
It uses expandType to expand XQL primitive type names.

<DT><A NAME="Function_document_QUERY_or_doc_QUERY_"><STRONG><P>Function: document (QUERY) or doc (QUERY)

</STRONG></A><DD>The document() function creates a new <A>XML::XML::Document</A> for each result 
of QUERY (QUERY may be a simple string expression, like "/usr/enno/file.xml". 
See t/xql_document.t or below for an example with a more complex QUERY.)

<P>document() may be abbreviated to doc().

<P>document() uses an XML::DOM::Parser underneath, which can be set with
XML::XQL::setDocParser(). By default it uses a parser that was created without
any arguments, i.e.

<PRE>  $PARSER = new XML::DOM::Parser;

</PRE><P>Let's try a more complex example, assuming $doc contains:

<PRE> &lt;doc&gt;
  &lt;file name="file1.xml"/&gt;
  &lt;file name="file2.xml"/&gt;
 &lt;/doc&gt;

</PRE><P>Then the following query will return two <A>XML::XML::Document</A>s, 
one for file1.xml and one for file2.xml:

<PRE> @result = XML::XQL::solve ("document(doc/file/@name)", $doc);

</PRE><P>The resulting documents can be used as input for following queries, e.g.

<PRE> @result = XML::XQL::solve ("document(doc/file/@name)/root/bla", $doc);

</PRE><P>will return all /root/bla elements from the documents returned by document().

<DT><A NAME="Method_DOM_nodeType_"><STRONG><P>Method: DOM_nodeType ()

</STRONG></A><DD>Returns the DOM node type. Note that these are mostly the same as nodeType(),
except for CDATASection and EntityReference nodes. DOM_nodeType() returns
4 and 5 respectively, whereas nodeType() returns 3, because they are 
considered text nodes.

<DT><A NAME="Function_wrappers_for_Perl_builtin_functions_"><STRONG><P>Function wrappers for Perl builtin functions

</STRONG></A><DD>XQL function wrappers have been provided for most Perl builtin functions.
When using a Perl builtin function like "substr" in an XQL+ querry, an
XQL function wrapper will be generated on the fly. The arguments to these
functions may be regular XQL+ subqueries (that return one or more values) for
a <I>query parameter</I> (see generateFunction for a definition.)
Most wrappers of Perl builtin functions have argument 0 for a query parameter,
except for: chmod (parameter 1 is the query parameter), chown (2) and utime (2).
The following functions have no query parameter, which means that all parameters
should be a single value: atan2, rand, srand, sprintf, rename, unlink, system.

<P>The function result is casted to the appropriate XQL primitive type (Number, 
Text or Boolean), or to an empty list if the result was undef.

</DL><H2><A NAME="XPath_functions_and_methods">XPath functions and methods

</A></H2><P>The following functions were found in the XPath specification:

<DL><DT><A NAME="Function_concat_STRING_STRING_STRING_"><STRONG><P>Function: concat (STRING, STRING, STRING*) 

</STRONG></A><DD>The concat function returns the concatenation of its arguments.

<DT><A NAME="Function_starts_with_STRING_STRING_"><STRONG><P>Function: starts-with (STRING, STRING) 

</STRONG></A><DD>The starts-with function returns true if the first argument string starts with 
the second argument string, and otherwise returns false.

<DT><A NAME="Function_contains_STRING_STRING_"><STRONG><P>Function: contains (STRING, STRING) 

</STRONG></A><DD>The contains function returns true if the first argument string contains the 
second argument string, and otherwise returns false.

<DT><A NAME="Function_substring_before_STRING_STRING_"><STRONG><P>Function: substring-before (STRING, STRING) 

</STRONG></A><DD>The substring-before function returns the substring of the first argument 
string that precedes the first occurrence of the second argument string
in the first argument string, or the empty string if the first argument 
string does not contain the second argument string. For example,

<PRE> substring-before("1999/04/01","/") returns 1999.

</PRE><DT><A NAME="Function_substring_after_STRING_STRING_"><STRONG><P>Function: substring-after (STRING, STRING) 

</STRONG></A><DD>The substring-after function returns the substring of the first argument string 
that follows the first occurrence of the second argument string in
the first argument string, or the empty string if the first argument string does
not contain the second argument string. For example,

<PRE> substring-after("1999/04/01","/") returns 04/01, 

</PRE><P>and 

<PRE> substring-after("1999/04/01","19") returns 99/04/01.

</PRE><DT><A NAME="Function_substring_STRING_NUMBER_NUMBER_"><STRONG><P>Function: substring (STRING, NUMBER [, NUMBER] ) 

</STRONG></A><DD>The substring function returns the substring of the first argument starting at 
the position specified in the second argument with length specified in
the third argument. For example, 

<PRE> substring("12345",2,3) returns "234". 

</PRE><P>If the third argument is not specified, it returns the substring 
starting at the position specified in the second argument and continuing to 
the end of the string. For example, 

<PRE> substring("12345",2) returns "2345".

</PRE><P>More precisely, each character in the string is considered 
to have a numeric position: the position of the first character is 1,
the position of the second character is 2 and so on.

<P>NOTE: This differs from the <B>substr</B> method , in which the
method treats the position of the first character as 0.

<P>The XPath spec says this about rounding, but that is not true in this 
implementation: 
<I>The returned substring contains those characters for which the position of the 
character is greater than or equal to the rounded value of the
second argument and, if the third argument is specified, less than the 
sum of the rounded value of the second argument and the rounded value of
the third argument; the comparisons and addition used for the above 
follow the standard IEEE 754 rules; rounding is done as if by a call to the
round function.</I>

<DT><A NAME="Method_string_length_QUERY_"><STRONG><P>Method: string-length ( [ QUERY ] )

</STRONG></A><DD>The string-length returns the number of characters in the string. 
If the argument is omitted, it defaults to the context node
converted to a string, in other words the string-value of the context node.

<P>Note that the generated XQL wrapper for the Perl built-in <B>substr</B> does not
allow the argument to be omitted.

<DT><A NAME="Method_normalize_space_QUERY_"><STRONG><P>Method: normalize-space ( [ QUERY ] )

</STRONG></A><DD>The normalize-space function returns the argument string with whitespace 
normalized by stripping leading and trailing whitespace and replacing
sequences of whitespace characters by a single space. Whitespace characters are 
the same as those allowed by the S production in XML. If the
argument is omitted, it defaults to the context node converted to a string, in 
other words the string-value of the context node.

<DT><A NAME="Function_translate_STRING_STRING_STRING_"><STRONG><P>Function: translate (STRING, STRING, STRING) 

</STRONG></A><DD>The translate function returns the first argument string with occurrences of 
characters in the second argument string replaced by the character at
the corresponding position in the third argument string. For example, 

<PRE> translate("bar","abc","ABC") returns the string BAr. 

</PRE><P>If there is a
character in the second argument string with no character at a corresponding
position in the third argument string (because the second argument
string is longer than the third argument string), then occurrences of that 
character in the first argument string are removed. For example,

<PRE> translate("--aaa--","abc-","ABC") returns "AAA". 

</PRE><P>If a character occurs more than once in the second argument string, then the 
first occurrence determines the replacement character. If the third argument 
string is longer than the second argument string, then excess characters
are ignored.

<P>NOTE: The translate function is not a sufficient solution for case conversion 
in all languages. A future version may
provide additional functions for case conversion.

<P>This function was implemented using tr///d.

<DT><A NAME="Function_sum_QUERY_"><STRONG><P>Function: sum ( QUERY ) 

</STRONG></A><DD>The sum function returns the sum of the QUERY results, by
converting the string values of each result to a number.

<DT><A NAME="Function_floor_NUMBER_"><STRONG><P>Function: floor (NUMBER) 

</STRONG></A><DD>The floor function returns the largest (closest to positive infinity) number 
that is not greater than the argument and that is an integer.

<DT><A NAME="Function_ceiling_NUMBER_"><STRONG><P>Function: ceiling (NUMBER) 

</STRONG></A><DD>The ceiling function returns the smallest (closest to negative infinity) number 
that is not less than the argument and that is an integer.

<DT><A NAME="Function_round_NUMBER_"><STRONG><P>Function: round (NUMBER) 

</STRONG></A><DD>The round function returns the number that is closest to the argument 
and that is an integer. If there are two such numbers, then the one that is
closest to positive infinity is returned.

</DL><P><HR><H1><A NAME="Implementation_Details">Implementation Details

</A></H1><DL><DT><A NAME="XQL_Builtin_Data_Types_"><STRONG><P>XQL Builtin Data Types

</STRONG></A><DD>The XQL engine uses the following object classes internally. Only Number, 
Boolean and Text are considered <I>primitive XQL types</I>:

<DL><DT><A NAME="_XML_XQL_Number_"><STRONG><P>* XML::XQL::Number

</STRONG></A><DD>For integers and floating point numbers.

<DT><A NAME="_XML_XQL_Boolean_"><STRONG><P>* XML::XQL::Boolean

</STRONG></A><DD>For booleans, e.g returned by true() and false().

<DT><A NAME="_XML_XQL_Text_"><STRONG><P>* XML::XQL::Text

</STRONG></A><DD>For string values.

<DT><A NAME="_XML_XQL_Date_"><STRONG><P>* XML::XQL::Date

</STRONG></A><DD>For date, time and date/time values. E.g. returned by the date() function.

<DT><A NAME="_XML_XQL_Node_"><STRONG><P>* XML::XQL::Node

</STRONG></A><DD>Superclass of all XML node types. E.g. all subclasses of XML::DOM::Node subclass
from this.

<DT><A NAME="_Perl_list_reference_"><STRONG><P>* Perl list reference

</STRONG></A><DD>Lists of values are passed by reference (i.e. using [] delimiters).
The empty list [] has a double meaning. It also means 'undef' in certain 
situations, e.g. when a function invocation or comparison failed.

</DL><DT><A NAME="Type_casting_in_comparisons_"><STRONG><P>Type casting in comparisons

</STRONG></A><DD>When two values are compared in an XML comparison (e.g. $eq$) the values are
first casted to the same data type. Node values are first replaced by their
value() (i.e. the XQL value() function is used, which returns a Text value by 
default, but may return any data type if the user so chooses.)
The resulting values are then casted to the type of the object with the highest
xql_primType() value. They are as follows: Node (0), Text (1), Number (2),
Boolean (3), Date (4), other data types (4 by default, but this may be
overriden by the user.)

<P>E.g. if one value is a Text value and the other is a Number, the Text value is 
cast to a Number and the resulting low-level (Perl) comparison is (for $eq$):

<PRE> $number-&gt;xql_toString == $text-&gt;xql_toString

</PRE><P>If both were Text values, it would have been

<PRE> $text1-&gt;xql_toString eq $text2-&gt;xql_toString

</PRE><P>Note that the XQL spec is vague and even conflicting where it concerns type
casting. This implementation resulted after talking to Joe Lapp, one of the
spec writers.

<DT><A NAME="Adding_Data_Types_"><STRONG><P>Adding Data Types

</STRONG></A><DD>If you want to add your own data type, make sure it derives from 
XML::XQL::PrimitiveType and implements the necessary methods.

<P>I will add more stuff here to explain it all, but for now, look at the code
for the primitive XQL types or the Date class (<A HREF="XQL/Date.html">XML::XQL::Date</A> in Date.pm.)

<DT><A NAME="Document_Order_"><STRONG><P>Document Order

</STRONG></A><DD>The XQL spec states that query results always return their values in 
<I>document order</I>, which means the order in which they appeared in the original
XML document. Values extracted from Nodes (e.g. with value(), text(), rawText(),
nodeName(), etc.) always have a pointer to the reference node (i.e. the Node
from which the value was extracted.) These pointers are acknowledged when
(intermediate) result lists are sorted. Currently, the only place where a
result list is sorted is in a $union$ expression, which is the only place
where the result list can be unordered.
(If you find that this is not true, let me know.)

<P>Non-node values that have no associated reference node, always end up at the end
of the result list in the order that they were added.
The XQL spec states that the reference node for an XML Attribute is the Element
to which it belongs, and that the order of values with the same reference node
is undefined. This means that the order of an Element and its attributes would 
be undefined.
But since the XML::DOM module keeps track of the order of the attributes, the
XQL engine does the same, and therefore, the attributes of an Element are
sorted and appear after their parent Element in a sorted result list.

<DT><A NAME="Constant_Function_Invocations_"><STRONG><P>Constant Function Invocations

</STRONG></A><DD>If a function always returns the same value when given "constant" arguments,
the function is considered to be "constant". A "constant" argument can be
either an XQL primitive (Number, Boolean, Text) or a "constant" function
invocation. E.g. 

<PRE> date("12-03-1998")
 true()
 sin(0.3)
 length("abc")
 date(substr("12-03-1998 is the date", 0, 10))

</PRE><P>are constant, but not:

<PRE> length(book[2])

</PRE><P>Results of constant function invocations are cached and calculated only once
for each query. See also the CONST parameter in defineFunction.
It is not necessary to wrap constant function invocations in a once() call.

<P>Constant XQL functions are: date, true, false and a lot of the XQL+
wrappers for Perl builtin functions. Function wrappers for certain builtins
are not made constant on purpose to force the invocation to be evaluated
every time, e.g. 'mkdir("/user/enno/my_dir", "0644")' (although constant
in appearance) may return different results for multiple invocations. 
See %PerlFunc in Plus.pm for details.

<DT><A NAME="Function_count_QUERY_"><STRONG><P>Function: count ([QUERY])

</STRONG></A><DD>The count() function has no parameters in the XQL spec. In this implementation
it will return the number of QUERY results when passed a QUERY parameter.

<DT><A NAME="Method_text_RECURSE_"><STRONG><P>Method: text ([RECURSE])

</STRONG></A><DD>When expanding an Element node, the text() method adds the expanded text() value
of sub-Elements. When RECURSE is set to 0 (default is 1), it will not include
sub-elements. This is useful e.g. when using the $match$ operator in a recursive
context (using the // operator), so it won't return parent Elements when one of
the children matches.

<DT><A NAME="Method_rawText_RECURSE_"><STRONG><P>Method: rawText ([RECURSE])

</STRONG></A><DD>See text().

</DL><P><HR><H1><A NAME="SEE_ALSO">SEE ALSO

</A></H1><P><A HREF="XQL/Query.html">XML::XQL::Query</A>, <A HREF="XQL/DOM.html">XML::XQL::DOM</A>, <A HREF="XQL/Date.html">XML::XQL::Date</A>

<P>The Japanese version of this document can be found on-line at
<A HREF="http://member.nifty.ne.jp/hippo2000/perltips/xml/xql.htm">http://member.nifty.ne.jp/hippo2000/perltips/xml/xql.htm</A>

<P>The <A HREF="XQL/Tutorial.html">XML::XQL::Tutorial</A> manual page. The Japanese version can be found at 
<A HREF="http://member.nifty.ne.jp/hippo2000/perltips/xml/xql/tutorial.htm">http://member.nifty.ne.jp/hippo2000/perltips/xml/xql/tutorial.htm</A>

<P>The XQL spec at <A HREF="http://www.w3.org/TandS/QL/QL98/pp/xql.html">http://www.w3.org/TandS/QL/QL98/pp/xql.html</A>

<P>The Design of XQL at <A HREF="http://www.texcel.no/whitepapers/xql-design.html">http://www.texcel.no/whitepapers/xql-design.html</A>

<P>The DOM Level 1 specification at <A HREF="http://www.w3.org/TR/REC-DOM-Level-1">http://www.w3.org/TR/REC-DOM-Level-1</A>

<P>The XML spec (Extensible Markup Language 1.0) at <A HREF="http://www.w3.org/TR/REC-xml">http://www.w3.org/TR/REC-xml</A>

<P>The <A HREF="Parser.html">XML::Parser</A> and <A HREF="Parser/Expat.html">XML::Parser::Expat</A> manual pages.

<P><HR><H1><A NAME="AUTHOR">AUTHOR

</A></H1><P>Please send bugs, comments and suggestions to Enno Derksen &lt;<I>enno@att.com</I>&gt;

<P><HR><I><FONT SIZE="-1">Last updated: Wed Feb 23 13:37:39 2000</FONT></I></BODY></HTML>