This appendix describes the functions that are built into XQuery. Table A-1 lists all of the built-in functions by subject for easy reference. They are all in the XPath Functions Namespace, http://www.w3.org/2005/xpath-functions.
In addition to a brief sentence explaining the purpose of the function, each function is described by the following characteristics:
"Signature" lists the parameters and their types, and the return type of the function.
"Usage Notes" covers the function in more detail.
"Special Cases" lists error conditions and other unusual situations.
"Example(s)" provides one or more example function calls with their return values.
"Related Functions" lists names of related functions.
XSLT 2.0 has some additional built-in functions, namely current
, current-group
, current-grouping-key
, document
, element-available
, format-date
, format-dateTime
, format-number
, format-time
, function-available
, generate-id
, key
, regex-group
, system-property
, type-available
, unparsed-entity-uri
, unparsed-entity-public-id
, unparsed-text
, unparsed-text-available
. These functions are part of XSLT 2.0 only, not XQuery 1.0 and XPath 2.0, and are therefore not covered in this appendix.
Many of the built-in functions have more than one signature. For example, adjust-date-to-timezone
has a signature with just $arg
, and another with two arguments—$arg
and $timezone
:
adjust-date-to-timezone($arg as xs:date?) as xs:date? adjust-date-to-timezone($arg as xs:date?, $timezone as xs:dayTimeDuration?) as xs:date?
For simplicity, in this appendix, only one signature is shown, with the "required" arguments in constant
width
bold
, and the "optional" ones in constant
width
italic
. For example:
adjust-date-to-timezone($arg as xs:date?
,$timezone as xs:dayTimeDuration?
) as xs:date?
This convention indicates that the function can be called with or without the $timezone
argument. Don't forget that passing the empty sequence or a zero-length string for an argument is not the same as omitting an argument.
It is possible to use this convention because the built-in functions (shown in Table A-1) have been designed so that in cases where there are several versions of a function with different numbers of arguments, the common arguments have the same type and meaning in each case. User-defined functions don't have to follow this design pattern, but it's good practice.
Table A-1. Function finder
Numeric functions |
|
String functions |
|
Date functions |
|
Date functions (component extraction) |
|
Boolean functions |
|
Document and URI functions |
|
Name and namespace functions |
|
Node-related functions |
|
Sequence-related functions |
|
Error handling and trapping functions |
|
abs: Finds the absolute value of a number
abs($arg as numeric?
) as numeric?
This function accepts any numeric value and returns its absolute value. It returns a numeric value of type xs:float
, xs:double
, xs:decimal
, or xs:integer
, depending on which type the argument is derived from. If $arg
is untyped, it is cast to xs:double
.
If $arg
is the empty sequence, the function returns the empty sequence.
If $arg
is INF
or -INF
, the function returns INF
.
If $arg
is NaN
, the function returns NaN
.
Example |
Return value |
---|---|
|
|
|
|
|
|
adjust-date-to-timezone: Adjusts the time zone of a date
adjust-date-to-timezone($arg as xs:date?
,$timezone as xs:dayTimeDuration?
) as xs:date?
The behavior of this function depends on whether the $arg
date value already has a time zone and on the value of the time zone provided. Table A-2 shows the possible combinations.
The $timezone
argument is expressed as an xs:dayTimeDuration
, for example, -PT5H
for U.S. Eastern Standard Time. If $timezone
is the empty sequence, it is assumed that the desired result is a date value that is in no time zone. If $timezone
is omitted from the function call,[*] it is assumed to be the implicit time zone.
The $arg
date is assumed for the sake of time zone calculation to be just like an xs:dateTime
value whose time is midnight (00:00:00). If $arg
does not already have a time zone, its date part stays the same, but it is now associated with the specified time zone.
If $arg
already has a time zone, its value is adjusted to that time zone. This may change the actual date in some cases. For example, if $arg
is 2006-02-15-05:00
, and $timezone
is -PT8H
, the resulting date is 2006-02-14-08:00
, which is the day before. This is because $arg
is considered to be 2006-02-15T00:00:00-05:00
, which is equivalent to 2006-02-14T21:00:00-08:00
. In other words, midnight in the U.S. Eastern time zone is equal to 9 P.M. the day before in the U.S. Pacific time zone.
For more information on time zones in XQuery, see "Time Zones" in Chapter 19.
If $arg
is the empty sequence, the function returns the empty sequence.
If the value of $timezone
is not between -PT14H
and PT14H
, inclusive, or if it does not have an integral number of minutes (i.e., the number of seconds is not 0), the error "Invalid timezone value" (FODT0003
) is raised.
Table A-2. Behavior of the adjust-*-to-timezone functions
Does $arg have a time zone? |
Value of $timezone argument |
Explanation of result |
---|---|---|
No |
An |
|
Yes |
An |
|
No |
The empty sequence |
|
Yes |
The empty sequence |
|
No |
Not provided |
|
Yes |
Not provided |
|
These six examples represent the six scenarios described in Table A-2.
Example[a] |
Return value |
---|---|
[a] | |
|
|
|
|
|
|
|
|
|
|
|
|
[a] This table assumes an implicit time zone of |
adjust-dateTime-to-timezone: Adjusts the time zone of a date/time
adjust-dateTime-to-timezone($arg as xs:dateTime?
,$timezone as xs:dayTimeDuration?
) as xs:dateTime?
The behavior of this function is identical to that of adjust-date-to-timezone
, described in Table A-2, except that the actual time, not midnight, is used.
If $arg
is the empty sequence, the function returns the empty sequence.
If the value of $timezone
is not between -PT14H
and PT14H
, inclusive, or if it does not have an integral number of minutes (i.e., the number of seconds is not 0), the error "Invalid timezone value" (FODT0003
) is raised.
These examples represent the scenarios described in Table A-2.
Example[a] |
Return value |
---|---|
[a] | |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[a] This table assumes an implicit time zone of |
adjust-time-to-timezone: Adjusts the time zone of a time
adjust-time-to-timezone($arg as xs:time?
,$timezone as xs:dayTimeDuration?
) as xs:time?
The behavior of this function is identical to that of adjust-date-to-timezone
, described in Table A-2, except that the actual time, not midnight, is used.
If $arg
is the empty sequence, the function returns the empty sequence.
If the value of $timezone
is not between - -PT14H
and PT14H
, inclusive, or if it does not have an integral number of minutes (i.e., the number of seconds is not 0), the error "Invalid timezone value" (FODT0003
) is raised.
Some examples are shown here. See Table A-2 for a description of the scenarios these examples represent.
Example[a] |
Return value |
---|---|
[a] | |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Error |
[a] This table assumes an implicit time zone of |
avg: Finds the average value of the items in a sequence
avg($arg as xs:anyAtomicType*
) as xs:anyAtomicType?
The $arg
sequence can contain a mixture of numeric and untyped values. Numeric values are promoted as necessary to make them all the same type. Untyped values are cast as numeric xs:double
values.
The function can also be used on duration values, so the $arg
sequence can contain all xs:yearMonthDuration
values or all xs:dayTimeDuration
values (but not a mixture of the two). The $arg
sequence cannot contain a mixture of duration and numeric values.
Special care should be taken with any "missing" values when using the avg
function. This is described further in "Counting "Missing" Values" in Chapter 7.
If $arg
is the empty sequence, the function returns the empty sequence.
If $arg
contains untyped values that cannot be cast to numbers, the error "Invalid value for cast/constructor" (FORG0001
) is raised.
If $arg
contains typed values that are not numeric or duration values, or values that have a variety of types, the error "Invalid argument type" (FORG0006
) is raised.
If $arg
contains values that are NaN
, the function returns NaN
.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
Error |
sum
, count
base-uri: Gets the base URI of a node
base-uri($arg as node( )?
) as xs:anyURI?
The essential purpose of a base URI is to establish a baseline for resolving any relative URIs in the content.
The $arg
argument may be any kind of node. If $arg
is a document node, this function usually returns the URI from which the document was retrieved,[*] if it is known. This can also be achieved by using the document-uri
function. An example where the base URI might not be known is where the document is created by parsing an anonymous input stream. Check your processor's documentation for details of how to supply a base URI in such cases.
If $arg
is an element, the function returns the value of its xml:base
attribute, if any, or the xml:base
attribute of its nearest ancestor. If no xml:base
attributes appear among its ancestors, it defaults to the base URI of the document node. If the original document consisted of multiple external entities (files), the base URI would be the URI of the containing entity.
If $arg
is any other kind of node, the function returns the same value as if the argument were its parent element or document.
For more information on URIs, see "Working with URIs" in Chapter 20.
If $arg
is the empty sequence, the function returns the empty sequence.
If $arg
is not provided, the function returns the base URI of the current context node. Note that this is not the same as the base URI from the static context, which is retrieved using the static-base-uri
function.
If $arg
is not provided, and the context item is not a node, the error XPTY0004
is raised.
If $arg
is not provided, and the context item is undefined, the error XPDY0002
is raised.
If no base URI can be found, for example because the element node has no base URI and does not have a document node as its root, the function returns the empty sequence.
These examples assume that the variable $cats
is bound to the input document http://datypic.com/cats.xml shown in Example A-1.
Example |
Return value |
---|---|
| |
| |
| |
| |
|
Example A-1. Using xml:base (http://datypic.com/cats.xml)
<catalogs> <catalog name="ACC" xml:base="http://example.org/ACC/"> <product number="443" href="prod443.html"/> <product number="563" href="prod563.html"/> </catalog> <catalog name="WMN" xml:base="http://example.org/WMN/"> <product number="557" href="prod557.html"/> </catalog> </catalogs>
static-base-uri
, resolve-uri
, document-uri
boolean: Finds the effective Boolean value
boolean($arg as item( )*
) as xs:boolean
This function calculates the effective Boolean value of a sequence (that is, any value). For more information, see "Effective Boolean Value" in Chapter 11.
In most cases, it is unnecessary to call this function because the effective Boolean value is calculated automatically in many expressions, including conditional and logical expressions, where
clauses, and predicates.
This boolean
function, which can also be written as fn:boolean
, should not be confused with the xs:boolean
constructor, which casts a value to xs:boolean
. In some cases, they return different results, namely when the argument is:
A single node that contains the value false
(xs:boolean
returns false
because it atomizes the node, while fn:boolean
returns true
)
The string value false
(xs:boolean
returns false
, fn:boolean
returns true
)
A zero-length string, or any string other than true
, false
, 0
, or 1
(xs:boolean
raises an error, fn:boolean
returns false
if it's a zero-length string; otherwise, true
)
A sequence of more than one node (xs:boolean
raises an error, fn:boolean
returns true
)
If the effective Boolean value of $arg
is undefined, for example because $arg
is a sequence of multiple atomic values, the error "Invalid argument type" (FORG0006
) is raised.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Error |
|
|
|
|
|
|
ceiling: Rounds a number up to the next integer
ceiling($arg as numeric?
) as numeric?
This function returns the smallest integer that is not less than $arg
. It returns a numeric value of type xs:float
, xs:double
, xs:decimal
, or xs:integer
, depending on which type the argument is derived from. If $arg
is untyped, it is cast to xs:double
.
If $arg
is the empty sequence, the function returns the empty sequence.
If $arg
is between −0.5 and −0 (inclusive), the function may return 0
or −0
(it is implementation-dependent).
If $arg
is one of the values 0
, −0
, NaN
, INF
, or -INF
, the function returns this same value.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
floor
, round
, round-half-to-even
codepoint-equal: Determines whether two strings contain the same code points
codepoint-equal($comparand1 as xs:string?
,$comparand2 as xs:string?
) as xs:boolean?
The function determines whether the two string arguments have the same Unicode code points, in the same order. This is similar to calling the compare
function with the simple collation http://www.w3.org/2005/xpath-functions/collation/codepoint, except that the result is a Boolean value.
If either argument is the empty sequence, the function returns the empty sequence.
Example |
Return value |
---|---|
|
|
|
|
|
|
codepoints-to-string: Constructs a string from Unicode code-point values
codepoints-to-string($arg as xs:integer*
) as xs:string
The $arg
argument is a sequence of integers representing Unicode code-point values.
If one or more of the $arg
integers does not refer to a valid XML character, the error "Code point not valid" (FOCH0001
) is raised.
If $arg
is the empty sequence, the function returns a zero-length string.
Example |
Return value |
---|---|
|
|
|
|
|
A zero-length string |
collection: Gets the nodes that make up a collection
collection($arg as xs:string?
) as node( )*
A collection may be any sequence of nodes, identified by a URI. Often, they are sequences of documents that are organized into collections so that they can be queried or managed together.
Exactly how the collection URI ($arg
) is associated with the nodes is defined by the implementation. Most XML database implementations allow you to define collections (and add documents to them) using the product's user interface or implementation-specific functions. Saxon, on the other hand, dereferences the URI to retrieve an XML collection document that lists all of the documents in the collection.
If $arg
is a relative URI, it is resolved based on the base URI of the static context. The base URI of the static context may be set by the processor outside the scope of the query, or it may be declared in the query prolog.
The collection
function is stable. This means that if you call the collection
function more than once with the exact same argument, within the same query, the result is the same, even if somehow the resources associated with the URI have changed.
If $arg
is not lexically a valid URI or it cannot be resolved, the error "Error retrieving resource" (FODC0004
) is raised.
If $arg
is not the URI of a collection supported by the implementation, the error "Invalid argument to fn:collection( )
" (FODC0004
) is raised.
If no argument is provided, or if $arg
is the empty sequence, the function returns the default collection as defined by the implementation. If no default collection is defined, the error "Error retrieving resource" (FODC0002
) is raised.
The expression collection("myXMLdocs")
will return all the document nodes of the XML documents associated with the collection myXMLdocs
.
doc
compare: Compares strings, optionally with an explicit collation
compare($comparand1 as xs:string?
,$comparand2 as xs:string?
,$collation as xs:string
) as xs:integer?
This function returns one of the values:
−1 if $comparand1
is less than $comparand2
0 if $comparand1
is equal to $comparand2
1 if $comparand1
is greater than $comparand2
A comparand is greater than the other comparand if it starts with the other comparand and has additional characters. For example, abc
is greater than ab
.
Comparison operators (=
, !=
, <
, <=
, >
, >=
) can also be used to compare strings, and you may find the syntax more convenient. However, compare
is also useful if you want to take different action in each of the three result cases. Also, if you need to use a specific collation other than the default, you must use the compare
function. More information can be found in "Collations" in Chapter 17.
If either comparand is the empty sequence, the function returns the empty sequence.
If $collation
is provided, the comparison uses that collation; otherwise, it uses the default collation.
Example[a] |
Return value |
---|---|
[a] | |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Error |
[a] The examples in this table assume that no default collation is specified. |
The fifth example in the table shows that when using the simple code-point collation, a lowercase a
comes after an uppercase B
. If you do not want case to be taken into account when comparing strings, convert the strings to uppercase first, as shown in the sixth example. Alternatively, you could use a case-insensitive collation.
concat: Concatenates two or more strings together
concat($arg1 as xs:anyAtomicType?
,$arg2 as xs:anyAtomicType?
, ...) as xs:string
The concat
function requires at least two arguments (which can be the empty sequence) and accepts an unlimited number of additional arguments. This is the only XQuery function that has a flexible number of arguments, for compatibility with XPath 1.0. The function is also unusual in that arguments that are not of type xs:string
will be cast to xs:string
.
The function does not accept a sequence of multiple values, just individual atomic values (or nodes) passed as separate arguments. To concatenate a sequence of multiple values, use the string-join
function instead.
If an argument is the empty sequence, it is treated as a zero-length string.
Example |
Return value |
---|---|
|
Error XPST0017 |
|
|
|
|
|
|
|
Error XPST0017 (use |
|
Error XPST0017 (use |
|
|
string-join
contains: Determines whether one string contains another
contains($arg1 as xs:string?
,$arg2 as xs:string?
,$collation as xs:string
) as xs:boolean
This function returns true
if $arg1
contains the characters of $arg2
anywhere in its contents, including at the beginning or end. Note that contains
does not test whether a sequence of multiple strings contains a given value. For that, use the = operator.
If $arg2
is a zero-length string or the empty sequence, the function returns true
.
If $arg1
is a zero-length string or the empty sequence, but $arg2
is not, the function returns false
.
If $collation
is provided, the comparison uses that collation; otherwise, it uses the default collation. Collations are described in Chapter 17.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
starts-with
, ends-with
, matches
count: Counts the number of items in a sequence
count($arg as item( )*
) as xs:integer
This function returns the number of items in a sequence as an xs:integer
.
To test whether or not the number of items in a sequence is zero, use the exists
or empty
function instead. Depending on your processor's optimizer, exists($x)
may be more efficient than count($x) = 0
.
If $arg
is the empty sequence, the function returns 0
.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
As shown in the third example, the count
function can be combined with the distinct-values
function to count only distinct values.
current-date: Gets the current date
current-date( ) as xs:date
This function takes no parameters and returns the current date with time zone. The time zone is implementation-dependent. To eliminate the time zone from the value, you can call adjust-date-to-timezone
with the empty sequence as the second argument, as in:
adjust-date-to-timezone(current-date(), ( ))
current-date( )
might return the xs:date
value 2006-04-10-05:00
, which is April 10, 2006 in the −05:00
time zone.
current-dateTime
, current-time
current-dateTime: Gets the current date and time
current-dateTime( ) as xs:dateTime
This function takes no parameters and returns the current date and time with time zone. If the function is called multiple times within the same query, it returns the same value every time. The time zone and the precision of the seconds part are implementation-dependent. To eliminate the time zone from the value, you can call adjust-dateTime-to-timezone
with the empty sequence as the second argument, as in:
adjust-dateTime-to-timezone(current-dateTime(), ( ))
current-dateTime( )
might return the xs:dateTime
value 2006-04-10T13:40:23.83-05:00
.
current-date
, current-time
current-time: Gets the current time
current-time( ) as xs:time
This function takes no parameters and returns the current time with time zone. If the function is called multiple times within the same query, it returns the same value every time. The time zone and the precision of the seconds part are implementation-dependent. To eliminate the time zone from the value, you can call adjust-time-to-timezone
with the empty sequence as the second argument, as in:
adjust-time-to-timezone(current-time(), ( ))
current-time( )
might return the xs:time
value 13:40:23.83-05:00
.
current-dateTime
, current-date
data: Extracts the typed value of one or more items
data($arg as item( )*
) as xs:anyAtomicType*
This function accepts a sequence of items and returns their typed values. For atomic values, this simply means returning the value itself, unchanged. For nodes, this means extracting the typed value of the node.
Calling this function is usually unnecessary because the typed value of a node is extracted automatically (in a process known as atomization) for many XQuery expressions, including comparisons, arithmetic operations, function calls, and sorting in FLWORs. The most common use case for the data
function is in element constructors. For example, the expression:
for $prod in doc("catalog.xml")//product return <newEl>{data($prod/name)}</newEl>
uses the data
function to extract the typed value of the name
element, in order to put it in the content of newEl
. If it had not used the data
function, the resulting newEl
elements would each have a name
child instead of just character data content.
In most cases, the typed value of an element or attribute is simply its string value, cast to the type of the element or attribute. For example, if the number
element has the type xs:integer
, the string value of the element is 784
(type xs:string
), while the typed value is 784
(type xs:integer
). If the number
element is untyped, its typed value is 784
(type xs:untypedAtomic
).
The typed value of an element will not be the empty sequence just because the element has no content. For example, the typed value of <name></name>
is the value ""
(type xs:untypedAtomic
) if name
is untyped, not the empty sequence.
There are some additional subtleties for schema-validated elements that are described in "Nodes and Typed Values" in Chapter 13.
Other kinds of nodes have typed values as well, but they are less useful; they are described in the appropriate sections of Chapter 21.
If $arg
is the empty sequence, the function returns the empty sequence.
If $arg
is an element whose type has element-only content, the error "Argument node does not have a typed value" (FOTY0012
) is raised.
Table A-3 shows some examples of the data
function applied to untyped nodes. They assume that the variable $cat
is bound to the document node of catalog.xml
, which has not been validated with a schema.
Table A-3. Examples of the data function on untyped nodes
Example |
Return value |
Return type |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Now suppose you have the schema shown in Example 13-1 in Chapter 13, and catalog.xml
was validated using this schema. The typed values of the nodes would then change, as shown in Table A-4.
Table A-4. Examples of the data function on typed nodes
Example |
Return value |
Return type |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
Error |
N/A |
|
|
|
string
dateTime: Constructs a date/time value from separate date and time values
dateTime($arg1 as xs:date?
,$arg2 as xs:time?
) as xs:dateTime?
This function constructs an xs:dateTime
value from an xs:date
value and an xs:time
value. It should not be confused with the xs:dateTime
constructor, which accepts a single argument that includes the date and time.
Time zone is taken into account when constructing the date/time. If neither the date nor the time has a time zone, the result has no time zone. If only one of the arguments has a time zone, or they both have the same time zone, the result has that time zone.
If the two arguments have different time zones, the error "Both arguments to fn:dateTime
have a specified timezone" (FORG0008
) is raised.
If either of the two arguments is the empty sequence, the function returns the empty sequence.
dateTime(xs:date("2006-08-15"),xs:time("12:30:45-05:00"))
returns the xs:dateTime
value 2006-08-15T12:30:45-05:00
.
day-from-date: Gets the day portion of a date
day-from-date($arg as xs:date?
) as xs:integer?
This function returns the day portion from an xs:date
value as an integer between 1 and 31 inclusive.
If $arg
is the empty sequence, the function returns the empty sequence.
day-from-date(xs:date("2006-08-15"))
returns 15
.
day-from-dateTime
day-from-dateTime: Gets the day portion of a date/time
day-from-dateTime($arg as xs:dateTime?
) as xs:integer?
This function returns the day portion from an xs:dateTime
value as an integer between 1 and 31 inclusive.
If $arg
is the empty sequence, the function returns the empty sequence.
day-from-dateTime(xs:dateTime("2006-08-15T10:30:23"))
returns 15
.
day-from-date
days-from-duration: Gets the normalized number of days in a duration
days-from-duration($arg as xs:duration?
) as xs:integer?
This function calculates the total number of whole days in an xs:duration
value. This is not necessarily the same as the integer that appears before the D
in the value. For example, if the duration is P1DT36H
, the function returns 2
rather than 1
. This is because 36 hours is equal to 1.5 days, and the normalized value is therefore P2DT12H
.
The days-from-duration
function does not round the number of days; if the duration is 2 days and 23 hours, it returns the integer 2
.
If $arg
is a negative duration, the function returns a negative value.
If $arg
is the empty sequence, the function returns the empty sequence.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
deep-equal: Determines whether the values of two sequences of items are equal (contain the same data)
deep-equal($parameter1 as item( )*
,$parameter2 as item( )*
,$collation as xs:string
) as xs:boolean
This function returns true
if the $parameter1
and $parameter2
sequences contain the same values, in the same order.
Atomic values are compared based on their typed values, using the eq
operator. If two atomic values cannot be compared (e.g., because one is a number and the other is a string), the function returns false
rather than raising an error.
The comparison of nodes takes into account the descendants and attributes. In order to be considered deep-equal, two nodes must:
Have the same qualified name.
Have the same node kind.
If they are elements, have the exact same attributes with the same qualified names and the same values (possibly in a different order).
If they are attributes or elements with simple content (no children), their typed values must be equal. For example, 2
is considered the same as 02
if they are both typed as integers, but not if they are both strings.
If they are elements with children, have element children and text nodes in the same order, that are themselves deep-equal.
If they are document nodes, have element and text children in the same order, that are themselves deep-equal.
If they are processing-instruction nodes, have the same name (target) and the exact same string value.
If they are text or comment nodes, have the exact same string value.
The two nodes do not have to have the same type, parent, or base URI. Namespace declarations are considered only to the extent that they affect element and attribute names or values typed as QNames; "unused" namespaces are ignored.
When comparing two element or document nodes, child comments and processing instructions are ignored. However, the presence of a comment that splits a text node in two will cause the text nodes to be unequal. Whitespace-only text nodes are considered significant.
If both $parameter1
and $parameter2
are the empty sequence, the function returns true
.
If only one of $parameter1
or $parameter2
is the empty sequence, the function returns false
.
If $collation
is provided, values of type xs:string
are compared using that collation; otherwise, the default collation is used.
In the context of this function, NaN
is considered equal to itself. This is to ensure that a node is always deep-equal to itself, even if some descendant has a typed value of NaN
.
The following two product
elements are considered deep-equal:
<product dept="MEN" id="P123"> <number>784</number> </product> <product id="P123" dept="MEN"><!--comment--> <number>784</number> </product>
The examples below assume that the variables $prod1
and $prod2
are bound to the two product
elements above.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
default-collation: Gets the default collation
default-collation( ) as xs:string
This function returns the default collation that is used in most operations where a collation is not explicitly specified. If no default collation is specified in the query prolog, the function returns the system default collation. If no system default collation is defined, the function returns a value representing the Unicode code-point collation, http://www.w3.org/2005/xpath-functions/collation/codepoint. See "Collations" in Chapter 17 for more information.
default-collation( )
might return http://datypic.com/collations/custom if that is the name of the default collation declared in the query prolog.
distinct-values: Selects distinct atomic values from a sequence
distinct-values($arg as xs:anyAtomicType*
,$collation as xs:string
) as xs:anyAtomicType*
This function returns a sequence of unique atomic values from $arg
. Values are compared based on their typed value. Values of different numeric types may be equal—for example, the xs:integer
value 1
is equal to the xs:decimal
value 1.0
, so the function only returns one of these values. If two values have incomparable types, e.g., xs:string
and xs:integer
, they are considered distinct, and no error is raised. Untyped values are treated like strings.
The $arg
sequence can contain atomic values or nodes, or a combination of the two. The nodes in the sequence have their typed values extracted using the usual function conversion rules. This means that only the contents of the nodes are compared, not any other properties of the nodes (for example, their names or identities). To eliminate nodes by identity instead, you can simply use the expression $seq/.
, which resorts the nodes and removes duplicates.
Because XQuery does not specify which of the duplicates to exclude, there may be some variation among implementations in the order and type of items in the result sequence.
If $arg
is the empty sequence, the function returns the empty sequence.
If $arg
contains more than one NaN
value, only one of them is returned (even though one NaN
value is technically not equal to other NaN
values).
Dates and times with no time zone component are assumed to be in the implicit time zone.
If $collation
is provided, values of type string
are compared using that collation; otherwise, it uses the default collation.
If $arg
contains an element whose schema type has element-only content, the type error XPTY0004
is raised, because such nodes do not have typed values.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
doc: Opens one input document based on its URI
doc($uri as xs:string?
) as document-node( )?
This function returns the document node of the resource associated with the specified URI. For example:
doc("http://datypic.com/order.xml")
returns the document node of the document whose URI is http://datypic.com/order.xml. Relative URI references are also allowed, as in:
doc("order.xml")
If $uri
is a relative URI, it is resolved based on the base URI of the static context. The base URI of the static context may be set by the processor outside the scope of the query, typically to the URI of the file from which the query was read. Alternatively, it may be declared in the query prolog.
If you are accessing documents on a filesystem, your implementation may require you to precede the filename with file:///
, use forward slashes to separate directory names, and/or escape each space in the filename with %20
. For example,
doc("file:///C:/Documents%20and%20Settings/my%20order.xml")
The doc
function is often combined with a path expression to retrieve specific children, as in:
doc("catalog.xml")/catalog/product
Note that the doc
function returns the document node, not the outermost element node. Therefore, you need to include the outermost element node in your path (catalog
in the previous example).
Processors interpret the URI passed to the doc
function in different ways. Some, like Saxon, will dereference the URI, that is, go out to the URL and retrieve the resource at that location. Other implementations, such as those embedded in XML databases, consider the URIs to be just names. The processor might take the name and look it up in an internal catalog to find the document associated with that name. Many processors provide user hooks or configuration options allowing the behavior to be controlled by the application, and the result may also depend on the configuration of the underlying environment (for example, HTTP proxy settings).
Implementations also have some leeway in how they handle errors when retrieving documents, how they handle different MIME types, and whether they validate the documents against a schema or DTD.
The doc
function is stable, meaning that it returns the same results each time it is called within a query. If you call the doc
function more than once with the exact same argument, within the same query, the result is the same, even if somehow the resource at $uri
has changed. Furthermore, the document nodes retrieved from each of these calls are identical to each other.
The doc
function should not be confused with the XSLT document
function, which is not available in XQuery. The document
function has different behavior, in that it will accept multiple URIs, allows the specification of a base URI, and has specific processing defined for handling fragment identifiers. These effects can be achieved in XQuery by combining use of doc
with other functions such as resolve-uri
and base-uri
or by using it in a FLWOR expression.
If $uri
is the empty sequence, the function returns the empty sequence.
If $uri
is not a lexically valid URI, the error "Invalid argument to fn:doc
" (FODC0005
) is raised.
If $uri
refers to a resource that is not supported by the implementation, the error "Invalid argument to fn:doc
" (FODC0005
) is raised.
If the resource cannot be retrieved or parsed, for example, because it does not reference a resource, or the resource is not well-formed XML, the behavior is implementation-defined. It may result in the error "Error retrieving resource" (FODC0002
) being raised, or in some other error handling behavior (such as a default document being opened).
doc("
http://datypic.com/order.xml
")
returns the document node of the document associated with the URI http://datypic.com/order.xml.
collection
, doc-available
doc-available: Determines whether a document is available
doc-available($uri as xs:string?
) as xs:boolean
The doc-available
function is a way to avoid the errors returned by the doc
function if a document is not available. This function will return true
if calling the doc
function on the same URI will result in a document node. It will return false
if the doc
function will not return a document node.
If $uri
is not a valid lexical value of xs:anyURI
, the error "Invalid argument to fn:doc
" (FODC0005
) is raised.
If $arg
is the empty sequence, the function returns false
.
This query will check if an XML document named http://datypic.com/new_order.xml is available:
if (doc-available("http://datypic.com/new_order.xml")) then doc("http://datypic.com/new_order.xml") else ( )
If a document is available, it will return its document node. Otherwise, it will return the empty sequence. If the doc
function had been called without verifying the existence of the document first, an error might have been raised.
doc
document-uri: Gets the URI of a document node
document-uri($arg as node( )?
) as xs:anyURI?
This function is basically the inverse of the doc
function. Where the doc
function accepts a URI and returns a document node, the document-uri
function accepts a document node and returns the absolute URI associated with it. The URI may represent the location from which it was retrieved, or simply the URI that serves as its name in an XML database.
In most cases, calling this function has the same result as calling the base-uri
function with the document node.
If $arg
is not a document node, the function returns the empty sequence.
If $arg
does not have a document URI (for example, because it is constructed), the function returns the empty sequence.
If the $arg
node's document URI is a relative URI that cannot be resolved, the function returns the empty sequence.
If $arg
is the empty sequence, the function returns the empty sequence.
If the variable $orderDoc
is bound to the result of doc("
http://datypic.com/order.xml
")
, then document-uri($orderDoc)
returns "http://datypic.com/order.xml".
doc
, base-uri
empty: Determines whether a sequence is empty
empty($arg as item( )*
) as xs:boolean
A sequence is empty if it contains zero items. A sequence is not considered empty just because it only contains a zero-length string, the value 0
, or an element with empty content. To test whether an element has empty content, use the expression:
string($node1) = ""
It is often unnecessary to call the empty
function because sequences are automatically converted to their effective Boolean value where a Boolean value is expected. For example, if you want to test whether there are any item
children of items
, you can use the if
clause:
if not($input//catalog/product) then ....
In this case, the sequence of selected item
elements is converted to the Boolean value true
if the sequence is not empty, and false
if the sequence is empty. There is no need to call the empty
function to determine this. But beware: this only works for node sequences, not for atomic values.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exists
, boolean
encode-for-uri: Applies URI escaping rules to a string that is to be used as a path segment of a URI
encode-for-uri($uri-part as xs:string?
) as xs:string
URIs require that some characters be escaped with their hexadecimal Unicode code point preceded by the % character. This includes non-ASCII characters and some ASCII characters, namely control characters, spaces, and several others.
In addition, certain characters in URIs are separators that are intended to delimit parts of URIs, namely the characters ; , / ? : @ & = + $ [ ] %. If the intended use of a string is as a segment of a URI path, where such separators have special meaning, the encode-for-uri
function allows you to escape these separator characters, while also escaping the other necessary special characters.
Like the escape-html-uri
and iri-to-uri
functions, the encode-for-uri
function replaces each special character with an escape sequence in the form %xx
(possible repeating), where xx
is two hexadecimal digits (in uppercase) that represent the character in UTF-8. For example, édition.html
is changed to %C3%A9dition.html
, with the é
escaped as %C3%A9
.
The encode-for-URI
function is the most aggressive of the three encoding functions. All characters except the following are escaped:
Letters a through z and A through Z
Digits 0 through 9
Hyphen (-), underscore (_), period (.), and tilde (~)
Because the function escapes delimiter characters, it's unsuitable for escaping a complete URI. Instead, it's useful for escaping strings before they are assembled into a URI—for example, the values of query parameters.
If $uri-part
is the empty sequence, the function returns a zero-length string.
The first example below shows a typical use case, where a filename contains the separator % character and some spaces that need to be escaped. The second example shows the escaping of an entire URL using this function, which can have undesired results. The escape-html-uri
function would have been a better choice.
Example |
Return value |
---|---|
|
|
|
|
escape-html-uri
, iri-to-uri
ends-with: Determines whether one string ends with another
ends-with($arg1 as xs:string?
,$arg2 as xs:string?
,$collation as xs:string
) as xs:boolean
This function returns an xs:boolean
value indicating whether one string ($arg1
) ends with the characters of a second string ($arg2
). Trailing whitespace is significant, so you may want to use the normalize-space
function to trim the strings before using this function.
If $arg2
is a zero-length string or the empty sequence, the function returns true
.
If $arg1
is a zero-length string or the empty sequence, but $arg2
is not, the function returns false
.
If $collation
is provided, the comparison uses that collation; otherwise, it uses the default collation.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
starts-with
, contains
, matches
error: Explicitly raise an error
error($error as xs:QName?, $description as xs:string
,$error-object as item( )*
) as none
This function allows you to stop execution of the query, with a specific error message. This is useful if an unexpected or invalid condition exists, such as a missing or invalid data item. You can incorporate calls to the error
function in your query to signal such problems to the query user. For example:
if (not ($product/number)) then error(QName("http://datypic.com/err", "ProdNumReq"), "Missing product number.") else $product/number
results in a ProdNumReq
error if $product
has no number
child.
How a processor will use the optional $description
and $error-object
arguments is implementation-dependent. Most processors will report the $description
as part of the error message to the user.
Some processors may report the error name as a URI, where the local part is a fragment identifier, as in http://datypic.com/err#ProdNumReq.
The error
function is the same function that the processor calls implicitly whenever there is an error during query evaluation. The return type of none
is only used for the error
function and is not available to query authors. It simply means that the error
function never returns any value; evaluation of the query stops once the error
function is called.
Remember that order of execution in XQuery is undefined. You can't always rely on the fact that your error test will be evaluated before some other expression that it is designed to guard. In fact, you can't always rely on the error expression being evaluated at all if, for example, it appears as part of a larger expression (perhaps a variable assignment) whose result is never used. However, simple cases such as if ($condition) then $value else error( )
should be safe.
If no $error
argument is provided, the error name defaults to FOER0000
("Unidentified error"), in the http://www.w3.org/2005/xqt-errors namespace.
Example |
---|
[a] |
|
|
|
|
[a] Assumes the |
trace
escape-html-uri: Escapes all non-ASCII characters in a string
escape-html-uri($uri as xs:string?
) as xs:string
HTML agents require that some URI characters be escaped with their hexadecimal Unicode code point preceded by the % character. This includes non-ASCII characters and some ASCII characters, namely control characters, spaces, and several others.
The escape-html-uri
function replaces each of these special characters with an escape sequence in the form %xx
(possible repeating), where xx
is two hexadecimal digits (in uppercase) that represent the character in UTF-8. For example, édition.html
is changed to %C3%A9dition.html
, with the é
escaped as %C3%A9
. Specifically, it escapes everything except those ASCII characters whose decimal code point is between 32 and 126 inclusive. This allows these URIs to be appropriately handled by HTML agents such as web browsers, for example in HTML href
attributes.
The way this function is specified is a pragmatic compromise. HTTP requires special characters such as spaces to be escaped. However, HTML documents often contain URIs designed for local use within the browser—for example, JavaScript function calls—and these local URIs (which are never sent over HTTP) will often fail if spaces and other ASCII characters are escaped. This function therefore only escapes non-ASCII characters.
If $uri
is the empty sequence, the function returns a zero-length string.
Example |
Return value |
---|---|
| |
|
|
|
|
encode-for-uri
, iri-to-uri
exactly-one: Verifies that a sequence contains exactly one item
exactly-one($arg as item( )*
) as item( )
If $arg
contains one item, $arg
is returned. Otherwise, the error "fn:exactly-one
called with a sequence containing zero or more than one item" (FORG0005
) is raised.
This function is useful when static typing is in effect, to avoid apparent static type errors. For example, to use a computed element constructor, you might be tempted to write the expression:
element {node-name($prod)} { 563 }
However, if static typing is used, this expression causes a static error. This is because the node-name
function returns 0
or 1 xs:QName
values, while the computed element constructor requires that one and only one xs:QName
value be provided. A static error can be avoided by using the expression:
element {exactly-one(node-name($prod))} { 563 }
In this case, no static error is raised. Rather, a dynamic error is raised if node-name
returns the empty sequence. For more information on static typing, see Chapter 14.
If static typing is NOT in effect, calling exactly-one
is not usually necessary, but it does no harm. The effect is usually to make explicit a runtime type check that would otherwise have been done automatically.
Example |
Return value |
---|---|
|
Error |
|
|
|
Error |
one-or-more
, zero-or-one
exists: Determines whether a sequence is not empty
exists($arg as item( )*
) as xs:boolean
This function returns true
if the sequence contains one or more items; it is the opposite of the empty
function. It is often unnecessary to call the exists
function because sequences are automatically converted to the effective Boolean value where a Boolean value is expected. For example, if you want to test whether there are any product
elements in catalog.xml
, you can use the if
clause:
if (doc("catalog.xml")//product) then ....
In this case, the sequence of selected product
elements is converted to the Boolean value true
if the sequence is not empty, and false
if the sequence is empty. There is no need to call the exists
function to determine this. But beware: this only works for node sequences, not for atomic values.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
empty
, one-or-more
false: Constructs a Boolean false
value
false( ) as xs:boolean
This function, which takes no arguments, is useful for constructing the Boolean value false
. XQuery uses the false( )
and true( )
functions instead of keywords false
and true
. This is most commonly used to supply a value in a function call where a Boolean value is required.
The expression false( )
returns the xs:boolean
value false
.
true
floor: Rounds a number down to the next lowest integer
floor($arg as numeric?
) as numeric?
This function returns the largest integer that is not greater than $arg
. It returns a numeric value of type xs:float
, xs:double
, xs:decimal
, or xs:integer
, depending on which type the argument is derived from. If $arg
is untyped, it is cast to xs:double
.
If $arg
is the empty sequence, the function returns the empty sequence.
If $arg
is one of the values 0
, −0
, NaN
, INF
, or -INF
, the function returns this same value.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
ceiling
, round
, round-half-to-even
hours-from-dateTime: Gets the hour portion of a date/time
hours-from-dateTime($arg as xs:dateTime?
) as xs:integer?
This function returns the hour portion of an xs:dateTime
value, as an integer between 0 and 23 inclusive.
If $arg
is the empty sequence, the function returns the empty sequence.
If the time portion of $arg
is 24:00:00
, the function will return 0
.
Example |
Return value |
---|---|
|
|
|
|
hours-from-time
hours-from-duration: Gets the normalized number of hours in a duration
hours-from-duration($arg as xs:duration?
) as xs:integer?
This function calculates the hours component of a normalized xs:duration
value, as an integer between −23 and 23 (inclusive). This is not necessarily the same as the integer that appears before the H
in the value. For example, if the duration is PT1H90M
, the function returns 2
rather than 1
. This is because 90 minutes is equal to 1.5 hours, and the normalized value is therefore PT2H30M
. Likewise, if the duration is PT36H
, the result is 12
, because the normalized value is P1DT12H
.
If $arg
is a negative duration, the function returns a negative value.
If $arg
is the empty sequence, the function returns the empty sequence.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
hours-from-time: Gets the hour portion of a time
hours-from-time($arg as xs:time?
) as xs:integer?
This function returns the hour portion of an xs:time
value, as an integer between 0 and 23 inclusive.
If $arg
is the empty sequence, the function returns the empty sequence.
If $arg
is 24:00:00
, the function returns 0
.
Example |
Return value |
---|---|
|
|
|
|
hours-from-dateTime
id: Returns elements by their IDs
id($arg as xs:string*
,$node as node( )
) as element( )*
Given a sequence of IDs, this function returns the elements with those IDs. If $node
is not provided, the function looks for elements in the same document as the current context item. If $node
is provided, the function looks for elements in the same document as $node
.
The strings in the $arg
sequence can either be individual ID values or space-separated lists of ID values. Although $arg
is a sequence of xs:string
values, you can also pass values of type xs:IDREF
or xs:IDREFS
, since these types are derived from xs:string
.
An element is considered to have a particular ID if either:
It has an attribute, that is marked as an ID, that has that ID value.
The type of the element itself is marked as an ID and its content is that particular ID value.
An element or attribute node can become "marked as an ID" by being validated by a schema where it is declared as having type xs:ID
, or (for an attribute) being described in a DTD as being of type ID. Also, if it's name is xml:id
, it is automatically considered to be an ID.
The default collation is not used to match ID values; a simple comparison of Unicode code points is used.
The function returns the nodes in document order, not the order designated by the sequence of $arg
values. The result sequence contains no duplicate elements, even if an ID value was included twice in $arg
.
Working with IDs and IDREFs is discussed in further detail in "Working with IDs" in Chapter 20.
Any values in $arg
that are not lexically valid IDs (i.e., XML NCNames) are ignored, even if there is an element with that invalid ID.
If there is no element with the specified ID, an error is not raised, but no element is returned for that ID value.
In the case of an invalid (but well-formed) document where more than one element has the same ID, the function returns the first element with that ID.
If $arg
is the empty sequence, the function returns the empty sequence.
If no matching elements were found, the function returns the empty sequence.
The error "No context document" (FODC0001
) is raised if:
$node
is not part of a document (its root is not a document node).
$node
is not provided and the context node is not part of a document.
If $node
is not provided and no context item is defined, the error XPDY0002
is raised.
If $node
is not provided and the context item is an atomic value rather than a node, the error XPTY0004
is raised.
These examples use the input document book.xml
shown in Example A-2. It is assumed that the input document has been validated and that the type of the id
attribute is xs:ID
.
Example |
Return value |
---|---|
|
The first |
|
The first two |
|
The first two |
|
The second |
|
The second and third |
|
The second and third |
|
The first |
Example A-2. XML document with IDs and IDREFs (book.xml)
<book> <section id="preface">This book introduces XQuery... The examples are downloadable<fnref ref="fn1"/>... </section> <section id="context">...</section> <section id="language">...Expressions, introduced in <secRef refs="context"/>, are... </section> <section id="types">...As described in <secRef refs="context language"/>, you can... </section> <fn fnid="fn1">See http://datypic.com.</fn> </book>
idref
idref: Finds references to a specified set of IDs
idref($arg as xs:string*
,$node as node( )
) as node( )*
This function returns the nodes (elements or attributes) that reference one of a specified sequence of IDs. If $node
is not provided, the function looks for elements and attributes in the same document as the current context item. If $node
is provided, the function looks for elements and attributes in the same document as $node
.
In order to be considered to reference an ID, the node must be marked as an IDREF or IDREFS type, and it must contain that ID value. Generally, this means that it was declared to be of type xs:IDREF
or xs:IDREFS
in an in-scope schema definition (or the equivalent in a DTD). If it is an IDREFS value, only one of the values in the list need match the ID. The default collation is not used to match ID values; a simple comparison of Unicode code points is used.
The function returns the nodes in document order, not the order designated by the sequence of $arg
values. The result sequence contains no duplicate elements, even if an ID value was included twice in $arg
. However, there may be several elements or attributes in the results that reference the same ID.
Note that if the IDREF value is contained in an attribute, the function returns the attribute node, not the containing element. This is designed to handle cases where an element has more than one IDREF attribute and you need to know which one matched.
Any values in $arg
that are not lexically valid IDs (i.e., XML NCNames) are ignored, even if there is an element with a matching invalid IDREF.
If $arg
is the empty sequence, the function returns the empty sequence.
If no matching nodes were found, the function returns the empty sequence.
If $node
is not part of a document (its root is not a document node), or if $node
is not provided and the context node is not part of a document, the error "No context document" (FODC0001
) is raised.
If $node
is not provided and no context item is defined, the error XPDY0002
is raised.
If $node
is not provided and the context item is an atomic value rather than a node, the error XPTY0004
is raised.
Working with IDs and IDREFs is discussed in further detail in "Working with IDs" in Chapter 20.
These examples use the input document book.xml
shown in Example A-2. It is assumed that the input document has been validated, that the type of the id
and fnid
attributes is xs:ID
, the type of the ref
attribute is xs:IDREF
, and the type of the refs
attribute is xs:IDREFS
.
Example |
Return value |
---|---|
|
The |
|
The |
|
The |
|
The |
|
The |
id
implicit-timezone: Gets the implicit time zone used by the processor
implicit-timezone( ) as xs:dayTimeDuration
This function returns the implicit time zone as an xs:dayTimeDuration
value. The implicit time zone is used in comparisons and calculations involving date and time values that do not have explicitly defined time zones.
The implicit time zone is implementation-defined. In practice it will often default to the time zone used by the system clock in the operating system, or perhaps it will depend on the locale of the individual user.
implicit-timezone( )
returns -PT5H
if the implicit time zone is UTC minus five hours (also represented as −05:00
).
in-scope-prefixes: Gets a list of all namespace prefixes that are in the scope of a specified element
in-scope-prefixes($element as element( )
) as xs:string*
This function returns a sequence of prefixes (as strings) that are used in the in-scope namespaces for the $element
element. The results include a zero-length string if there is a default namespace declaration. It also always includes the xml
prefix, which is built into the XML recommendation.
Note that the function uses in-scope namespaces, as opposed to statically known namespaces. That is, it returns information about the namespaces declared in the source document, not the namespaces declared in the query. The difference between in-scope and statically known namespaces is described further in "In-Scope Versus Statically Known Namespaces" in Chapter 10. More on working with namespaces and qualified names can be found in "Working with Qualified Names" in Chapter 20.
The query:
in-scope-prefixes(<prefList xmlns="http://datypic.com/prod" xmlns:prod2="http://datypic.com/prod2">xyz</prefList>)
returns the sequence ("", "prod2", "xml")
in some arbitrary order.
namespace-uri-for-prefix
index-of: Determines where (and whether) an atomic value appears in a sequence
index-of($seqParam as xs:anyAtomicType*
,$srchParam as xs:anyAtomicType
,$collation as xs:string
) as xs:integer*
The $seqParam
argument is the sequence to be searched, while $srchParam
is the value to search for. This function returns a sequence of integers representing the position(s) of the value within the sequence, in order, starting with 1 (not 0).
The items in $seqParam
are compared to those in $srchParam
by their typed value, not their name or node identity. If either sequence contains nodes, those nodes are atomized to extract their atomic values. Untyped values are treated like strings.
If $srchParam
cannot be compared with a value in $seqParam
, for example because $srchParam
is a string and $seqParam
contains an integer, it will not match that value, but it will not raise an error.
If the $srchParam
value does not appear in $seqParam
, the function returns the empty sequence.
If $seqParam
is the empty sequence, the function returns the empty sequence.
If $collation
is provided, values of type xs:string
are compared using that collation; otherwise, the default collation is used.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
position
insert-before: Inserts items into a sequence
insert-before($target as item( )*
,$position as xs:integer
,$inserts as item( )*
) as item( )*
This function returns a copy of the $target
sequence with the item(s) in $inserts
inserted at the position indicated by $position
. Position numbers start at 1, not 0.
If $inserts
is the empty sequence, the function returns the $target
sequence.
If $target
is the empty sequence, the function returns the $inserts
sequence.
If $position
is greater than the number of items in $target
, the $inserts
items are appended to the end.
If $position
is less than 1, the $inserts
items are inserted at the beginning.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
remove
iri-to-uri: Applies URI escaping rules to a string
iri-to-uri($uri-part as xs:string?
) as xs:string
URIs require that some characters be escaped with their hexadecimal Unicode code point preceded by the % character. This includes non-ASCII characters and some ASCII characters, namely control characters, spaces, and several others.
The iri-to-uri
function replaces each special character with an escape sequence in the form %xx
(possible repeating), where xx
is two hexadecimal digits (in uppercase) that represent the character in UTF-8. For example, édition.html
is changed to %C3%A9dition.html
, with the é
escaped as %C3%A9
.
All characters except the following are escaped:
Letters a through z and A through Z
Digits 0 through 9
Hyphen (-), underscore (_), period (.), exclamation point (!), tilde (~), asterisk (*), apostrophe ('), parentheses ("(" and ")"), and hash mark (#)
Semicolon (;), forward slash (/), question mark (?), colon (:), at sign (@), ampersand (&), equals sign (=), plus sign (+), dollar sign ($), comma (,), square brackets ([ and ]), and percent sign (%)
The last set of characters specified in the preceding list is generally used to delimit parts of URIs. If you are escaping a single step of a URI path (as opposed to an entire URI), it is better to use the encode-for-uri
function, which does escape these characters.
If $uri-part
is the empty sequence, the function returns a zero-length string.
Example |
Return value |
---|---|
|
|
|
|
|
|
encode-for-uri
, escape-html-uri
lang: Tests whether the language of a node matches a specified language
lang($testlang as xs:string?
, $node as node( )
) as xs:boolean
The language of a node is determined by the existence of an xml:lang
attribute on the node itself or among its ancestors. The lang
function can be used on any node, not just one containing string
values. It is often used in the predicates of path expressions to filter data for a particular language.
The $testlang
argument specifies the language to test. The function returns true
if the relevant xml:lang
attribute of the $node
has a value that matches the $testlang
value. The function returns false
if the relevant xml:lang
attribute does not match $testlang
, or if there is no relevant xml:lang
attribute.
The relevant xml:lang
attribute is the one that appears as an attribute of the context node itself, or of one of its ancestors. If more than one xml:lang
attribute can be found among the node and its ancestors, the nearest one applies.
The matching process is case-insensitive. If $testlang
is en
, it matches the xml:lang
value EN
, and vice versa. Also, the value of the xml:lang
attribute can be a sublanguage of the $testlang
value. For example, en-US
, en-UK
, and en-US-UK
are all sublanguages of en
. Therefore, if $testlang
is en
, and xml:lang
is en-US
, the node will be matched. This does not work in reverse; if $testlang
is en-US
, and xml:lang
is en
, it will not match.
More information on the format of languages can be found with the description of the xs:language
type in Appendix B.
If $testlang
is the empty sequence, it is treated like a zero-length string.
If $node
is not provided, the context item is used.
If no xml:lang
attributes exist on the ancestors of a node, the function will return false
.
If $node
is not provided, and the context item is undefined, the error XPDY0002
is raised.
If $node
is not provided, and the context item is not a node, the error XPTY0004
is raised.
These examples make use of the input document shown in Example A-3.
Example |
Return value |
---|---|
|
The first |
|
The first |
|
The second |
|
The first |
|
( ) |
|
The second |
last: Gets the number of items in the current context
last( ) as xs:integer
The last
function returns an integer representing the number of items in the current context. It is most often used in the predicate of path expressions, to retrieve the last item. For example, catalog/product[last( )]
returns the last product
child of catalog
. That is because the last
function returns 4
, which serves as a positional predicate to retrieve the fourth product
. The last
function is also useful for testing whether an item is the last one in the sequence.
If the context item is undefined, the error XPDY0002
is raised.
doc("catalog.xml")/catalog/product[last( )]
returns the last product
child of catalog
. Example A-4 demonstrates how you might test whether an item is the last one in the sequence. It shows a query that returns a list of all the product numbers, separated by commas. However, after the last product number, it includes a period rather than a comma. It uses the last
function and an is
expression to compare each product
element to the last one in the catalog.
position
local-name: Gets the local part of a node name, as a string
local-name($arg as node( )?
) as xs:string
This function is useful only for element, attribute, and processing instruction nodes. For an element or attribute, this is simply its name, stripped of any prefix it might have.
To find elements with a particular local name, instead of writing a/*[local-name( )='nnn']
, you can write a/*:nnn
. However, the predicate test is useful if the name you are looking for is variable.
If $arg
is not provided, the context node is used.
The function returns a zero-length string if:
$arg
is the empty sequence.
$arg
is a node that does not have a name (i.e., a comment, document, or text node).
If $arg
is a processing instruction node, the function returns its target.
If $arg
is not provided, and the context item is undefined, the error XPDY0002
is raised.
If $arg
is not provided, and the context item is not a node, the error XPTY0004
is raised.
These examples use the input document names.xml
shown in Example A-5. They also assume that the prefixes pre
and unpre
have been mapped to the namespaces http://datypic.com/pre and http://datypic.com/unpre, respectively, in the query prolog.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
name
, node-name
, namespace-uri
local-name-from-QName: Gets the local part of a QName
local-name-from-QName($arg as xs:QName?
) as xs:NCName?
This function returns the local part of an xs:QName
. If you want to retrieve the local part of an element or attribute name, you should consider using the local-name
function instead; it requires one less step.
If $arg
is the empty sequence, the function returns the empty sequence.
Example |
Return value |
---|---|
|
|
|
|
|
|
local-name
, namespace-uri-from-QName
, resolve-QName
, QName
lower-case: Converts a string to lowercase
lower-case($arg as xs:string?
) as xs:string
The mappings between lowercase and uppercase characters are determined by Unicode case mappings. If a character in $arg
does not have a corresponding lowercase character, it is included in the result string unchanged.
For English, you can do a case-blind comparison by writing lower-case($A)=lower-case($B)
(or use upper-case
instead). However this doesn't always work well for other languages. It's better to use a case-insensitive collation.
If $arg
is the empty sequence, the function returns a zero-length string.
Example |
Return value |
---|---|
|
|
|
|
|
|
upper-case
matches: Determines whether a string matches a particular pattern
matches($input as xs:string?
,$pattern as xs:string
,$flags as xs:string
) as xs:boolean
The $pattern
argument is a regular expression, whose syntax is covered in Chapter 18. Unlike many of the string-related functions, the matches
function does not use collations at all. Regular expression matching is solely based on Unicode code points. Unless the anchors ^
or $
are used, the function returns true if any substring of $input
matches the regular expression.
The $flags
parameter allows for additional options in the interpretation of the regular expression. It is discussed in detail in "Using Flags" in Chapter 18.
If $input
is the empty sequence, it is treated like a zero-length string.
If $pattern
is a zero-length string, the function will return true
.
If $pattern
is not a valid regular expression, the error "Invalid regular expression" (FORX0002
) is raised.
If $flags
contains unsupported options, the error "Invalid regular expression flags" (FORX0001
) is raised.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Error |
The additional examples shown in the following table use the $flags
argument. They assume that the variable $address
is bound to the following string (the line break is significant):
123 Main Street Traverse City, MI 49684
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Error |
contains
max: Returns the maximum of the values in a sequence
max($arg as xs:anyAtomicType*
,$collation as xs:string
) as xs:anyAtomicType?
The $arg
sequence can only contain values of one type, or of types derived from it. The one exception is that they can be all numeric (of different numeric types), in which case numeric promotion rules apply. That type must be ordered; it must be possible to compare the values using the <
and >
operators.
This function assumes untyped values are numeric unless they are explicitly cast to xs:string
. To treat untyped data as strings, use the string
function as shown in the last example.
The max
function returns an atomic value, not the node that contains that value. For example, the expression:
max(doc("catalog.xml")//number)
will return the number 784
, not the number
element that contains 784
.
If $arg
is the empty sequence, the function returns the empty sequence.
If $arg
contains the value NaN
, the function returns NaN
.
If $arg
contains untyped values that cannot be cast to xs:double
, the error "Invalid value for cast/constructor" (FORG0001
) is raised.
If $arg
contains values of a type that does not support the <
and >
operators, the error "Invalid argument type" (FORG0006
) is raised.
If $arg
contains values of various types, the error "Invalid argument type" (FORG0006
) is raised.
If $arg
contains a date or time with no time zone, it is given the implicit time zone.
If $collation
is provided, values of type xs:string
are compared using that collation; otherwise, the default collation is used.
Example |
Return value |
---|---|
|
|
|
|
|
The current date |
|
Error |
|
|
|
Type error, if |
|
|
min
min: Returns the minimum of the values in a sequence
min($arg as xs:anyAtomicType*
,$collation as xs:string
) as xs:anyAtomicType?
The $arg
sequence can only contain values of one type, or a type derived from it. The one exception is that they can be all numeric, in which case numeric promotion rules apply. That type must be ordered; it must be possible to compare the values using the <
and >
operators.
This function assumes untyped values are numeric unless they are explicitly cast to xs:string
. To treat untyped data as strings, use the string
function as shown in the last example.
The min
function returns an atomic value, not the node that contains that value. For example, the expression:
min(doc("catalog.xml")//number)
will return the number 443
, not the number
element that contains 443
.
If $arg
is the empty sequence, the function returns the empty sequence.
If $arg
contains untyped values that cannot be cast to xs:double
, the error "Invalid value for cast/constructor" (FORG0001
) is raised.
If $arg
contains values of a type that does not support the <
and >
operators, the error "Invalid argument type" (FORG0006
) is raised.
If $arg
contains values of various types, the error "Invalid argument type" (FORG0006
) is raised.
If $arg
contains a date or time with no time zone, it is assumed to be in the implicit time zone.
If $collation
is provided, values of type xs:string
are compared using that collation; otherwise, the default collation is used. Collations are described in Chapter 17.
Example |
Return value |
---|---|
|
|
|
|
|
Error |
|
|
|
|
Note that the last example evaluates to black
because the string
function is only called for existing color attributes. The second-to-last example returns a zero-length string because it finds the string value of every item's color attribute, whether it has one or not.
max
minutes-from-dateTime: Gets the minutes portion of a date/time
minutes-from-dateTime($arg as xs:dateTime?
) as xs:integer?
This function returns the minutes portion of an xs:dateTime
value, as an integer between 0 and 59 inclusive.
If $arg
is the empty sequence, the function returns the empty sequence.
minutes-from-dateTime(xs:dateTime("2006-08-15T10:30:23"))
returns 30
.
minutes-from-time
minutes-from-duration: Gets the normalized number of minutes in a duration
minutes-from-duration($arg as xs:duration?
) as xs:integer?
This function calculates the minutes component of a normalized xs:duration
value, as an integer between −59 and 59 inclusive. This is not necessarily the same as the integer that appears before the M
in the value. For example, if the duration is PT1M90S
, the function returns 2
rather than 1
. This is because 90 seconds is equal to 1.5 minutes, and the normalized value is therefore PT2M30S
. Likewise, if the duration is PT90M
, the result is 30
, because the normalized value is PT1H30M
.
If $arg
is a negative duration, the function returns a negative value.
If $arg
is the empty sequence, the function returns the empty sequence.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
minutes-from-time: Gets the minutes portion of a time
minutes-from-time($arg as xs:time?
) as xs:integer?
This function returns the minutes portion of an xs:time
value, as an integer between 0 and 59 inclusive.
If $arg
is the empty sequence, the function returns the empty sequence.
minutes-from-time(xs:time("10:30:23"))
returns 30
.
minutes-from-dateTime
month-from-date: Gets the month portion of a date
month-from-date($arg as xs:date?
) as xs:integer?
This function returns the month portion of an xs:date
value, as an integer between 1 and 12 inclusive.
If $arg
is the empty sequence, the function returns the empty sequence.
month-from-date(xs:date("2006-08-15"))
returns 8
.
month-from-dateTime
month-from-dateTime: Gets the month portion of a date/time
month-from-dateTime($arg as xs:dateTime?
) as xs:integer?
This function returns the month portion of an xs:dateTime
value, as an integer between 1 and 12 inclusive.
If $arg
is the empty sequence, the function returns the empty sequence.
month-from-dateTime(xs:dateTime("2006-08-15T10:30:23"))
returns 8
.
month-from-date
months-from-duration: Gets the normalized number of months in a duration
months-from-duration($arg as xs:duration?
) as xs:integer?
This function calculates the months component of a normalized xs:duration
value, as an integer between −11 and 11 inclusive. This is not necessarily the same as the integer that appears before the M
in the value. For example, if the duration is P18M
, the function returns 6
rather than 18
. This is because 12 of those months are considered to be one year, and the normalized value is therefore P1Y6M
.
If $arg
is a negative duration, the function returns a negative value.
If $arg
is the empty sequence, the function returns the empty sequence.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
name: Gets the qualified name of a node as a string
name($arg as node( )?
) as xs:string
This function returns a string that consists of the namespace prefix and colon (:), concatenated with the local part of the name. If the $arg
node's name is not in a namespace, the function returns the local part of the name, with no prefix. If the name is associated with the default namespace, the resulting name will not be prefixed.
The name
function returns a value that depends on the choice of prefixes in the source document. This makes it well suited for displaying the name, for example in error messages, but it should never be used to test the name, because choice of prefixes shouldn't affect the result. For this purpose, use node-name
instead.
If $arg
is not provided, the context node is used.
The function returns a zero-length string if:
$arg
is the empty sequence.
$arg
is a node that does not have a name (i.e., a document, comment, or text node).
If $arg
is a processing instruction node, the function returns its target.
If $arg
is not provided, and the context item is undefined, the error XPDY0002
is raised.
If $arg
is not provided, and the context item is not a node, the error XPTY0004
is raised.
These examples use the input document names.xml
shown in Example A-5, in the section "local-name." They also assume that the prefixes pre2
and unpre2
have been mapped to the namespaces http://datypic.com/pre and http://datypic.com/unpre, respectively, in the query prolog.
The prefixes pre2
and unpre2
are used rather than pre
and unpre
to demonstrate that when selecting nodes from an instance document, the prefix used in the instance document is returned (not the query).
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
local-name
, node-name
, namespace-uri
namespace-uri: Gets the namespace part of an element or attribute node name
namespace-uri($arg as node( )?
) as xs:anyURI
This function returns the namespace part of the element or attribute name. This is the namespace that is mapped to its prefix, or the default namespace if it is unprefixed. If the element or attribute name is not in a namespace, a zero-length value is returned.
If $arg
is not provided, the context node is used.
The function returns a zero-length xs:anyURI
value if:
$arg
is the empty sequence.
$arg
is a kind of node that does not have a namespace (i.e., a document, comment, processing instruction or text node).
$arg
is an element or attribute whose name is not in a namespace.
If $arg
is not provided, and the context item is undefined, the error XPDY0002
is raised.
If $arg
is not provided, and the context item is not a node, the error XPTY0004
is raised.
These examples use the input document names.xml
shown in Example A-5, in the section "local-name." They also assume that the prefixes pre
and unpre
have been mapped to the namespaces http://datypic.com/pre and http://datypic.com/unpre, respectively, in the query prolog.
Example |
Return value |
---|---|
|
A zero-length URI value |
| |
| |
| |
|
A zero-length URI value |
local-name
, name
, node-name
namespace-uri-for-prefix: Gets the namespace mapped to a particular prefix, within the scope of a particular element
namespace-uri-for-prefix($prefix as xs:string?
,$element as element( )
) as xs:anyURI?
This function returns the namespace mapped to $prefix
using the in-scope namespaces of $element
. If $prefix
is a zero-length string or the empty sequence, the function returns the default namespace, if any.
The function is most often used in conjunction with the in-scope-prefixes
function to determine all the namespaces that are declared on a particular element, including any namespaces that appear to be unused.
If $prefix
is not mapped to a namespace in scope, the function returns the empty sequence.
If $prefix
is a zero-length string or the empty sequence, and there is no default namespace, the function returns the empty sequence.
These examples use the input document names.xml
shown in Example A-5, in the section "local-name." They also assume that the prefixes pre
and unpre
have been mapped to the namespaces http://datypic.com/pre and http://datypic.com/unpre, respectively, in the query prolog.
Example |
Return value |
---|---|
|
|
|
|
| |
| |
|
in-scope-prefixes
, resolve-QName
, QName
namespace-uri-from-QName: Gets the namespace URI part of a QName
namespace-uri-from-QName($arg as xs:QName?
) as xs:anyURI?
This function returns the namespace part of an xs:QName
. If you want to retrieve the namespace of an element or attribute name, you should consider using the namespace-uri
function instead; it saves a step.
If $arg
is in no namespace, the function returns a zero-length string.
If $arg
is the empty sequence, the function returns the empty sequence.
Example |
Return value |
---|---|
| |
|
A zero-length URI value |
|
|
local-name-from-QName
, resolve-QName
, QName
, namespace-uri
, namespace-uri-for-prefix
nilled: Determines whether an element is nilled
nilled($arg as node( )?
) as xs:boolean?
In a schema, element declarations can designate elements as nillable. This allows them to appear in an instance document empty, even if their type would otherwise require them to have some content (character data or children or both).
An element is not considered to be nilled just because it is empty. For an element to be nilled, it must have an attribute xsi:nil
whose value is true
. Nilled elements are always empty; it is not valid for an element to have content and also have the xsi:nil
attribute set to true
.
On the other hand, some elements may be validly empty, but not be nilled. This may occur if an element has a complex type that specifies all optional children, or a simple type that allows blank values, such as xs:string
. To test for an empty (but not necessarily nilled) element, you can use the expression string($node) = ""
.
It is useful to be able to check for a nilled element using the nilled
function to avoid unexpected results. For example, suppose you want to subtract the value of a discount
element from the value of a price
element. If the discount
element is nilled, its typed value will be the empty sequence, and the result of the expression price - discount
will be the empty sequence. You can avoid this using the expression price - (if nilled(discount) then 0 else discount)
.
If $arg
is not an element, the function returns the empty sequence.
If $arg
is the empty sequence, the function returns the empty sequence.
These examples use the input document nils.xml
shown in Example A-6.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
empty
, exists
node-name: Gets the qualified name of a node
node-name($arg as node( )?
) as xs:QName?
This function returns the qualified name of a node as an xs:QName
. It is useful only for element, attribute, and processing instruction nodes. For elements and attributes, it is simply the names used in XML documents. If the node's name is not in any namespace, the namespace portion of the QName is empty, while the local part is the appropriate name.
If $arg
does not have a name (because it is a text, comment, or document node), the function returns the empty sequence.
If $arg
is a processing instruction node, the function returns its target.
If $arg
is the empty sequence, the function returns the empty sequence.
These examples use the input document names.xml
shown in Example A-5, in the section "local-name." They also assume that the prefixes pre2
and unpre2
have been mapped to the namespaces http://datypic.com/pre and http://datypic.com/unpre, respectively, in the query prolog.
The prefix pre2
is used rather than pre
to demonstrate that when selecting nodes from an instance document, it is the prefix used in the instance document is returned (not the query). Although prefixes are technically irrelevant, they are saved in QName values by XQuery processors.
Example |
Return value (xs:QName) |
---|---|
|
Namespace: empty Prefix: empty Local part: |
|
Namespace: http://datypic.com/pre Prefix: Local part: |
|
Namespace: http://datypic.com/unpre Prefix: empty Local part: |
|
Namespace: http://datypic.com/pre Prefix: Local part: |
|
Namespace: empty Prefix: empty Local part: |
local-name
, name
, namespace-uri
normalize-space: Normalize the whitespace in a string
normalize-space($arg as xs:string?
) as xs:string
This function collapses whitespace in a string. Specifically, it performs three steps:
Replaces each carriage return (#xD
), line feed (#xA
), and tab (#x9
) character with a single space (#x20
)
Collapses all consecutive spaces into a single space
Removes all leading and trailing spaces
If $arg
is the empty sequence, the function returns a zero-length string.
If $arg
is not provided, the function uses the value of the context item.
If $arg
is not provided and the context item is undefined, the error XPDY0002
is raised.
Example |
Return value |
---|---|
[a] | |
|
|
|
|
|
|
|
|
|
|
|
A zero-length string |
|
A zero-length string |
|
A zero-length string |
|
|
[a] The line break in this example is significant. |
normalize-unicode: Performs Unicode normalization on a string
normalize-unicode($arg as xs:string?
,$normalizationForm as xs:string
) as xs:string
Unicode normalization allows text to be compared without regard to subtle variations in character representation. It replaces certain characters with equivalent representations. Two normalized values can then be compared to determine whether they are the same. Unicode normalization is also useful for allowing character strings to be sorted appropriately.
The $normalizationForm
argument controls which normalization form is used, and hence which characters are replaced. Examples of replacements that might be made include:
Some characters may be replaced by equivalent characters. The symbol £ (U+FFE1) is converted to an equivalent symbol,£ (U+00A3) using the form NFKC.
Some characters with accents or other marks represented by one code point may be decomposed to an equivalent representation that has two or more code points. The ç character (U+00E7) is changed to a representation that uses two code points (U+0063, which is "c", and U+0327, which is the cedilla) using form NFKD. Other normalization forms may combine the two code points into a single code point.
Some characters that represent symbols may be replaced by letters or other characters. The symbol ㎗ (U+3397) is replaced by the two letters dl (U+0064 + U+006C) using the form NFKC. This change is not made when using the form NFC.
Valid values for $normalizationForm
are listed in Table A-5. The value may be specified with leading or trailing spaces, in upper-, lower-, or mixed case. All implementations support the value NFC
; some implementations may support the other values listed, as well as additional normalization forms.
Table A-5. Valid values for the $normalizationForm argument
Value |
Meaning |
---|---|
[a] | |
|
Unicode Normalization Form C (NFC). |
|
Unicode Normalization Form D (NFD). |
|
Unicode Normalization Form KC (NFKC). |
|
Unicode Normalization Form KD (NFKD). |
|
The fully normalized form, according to the W3C definition. This form takes into account XML constructs such as entity references and CDATA sections in text.[a] |
Zero-length string |
No normalization is performed. |
[a] Essentially, "fully normalized" is NFC with the additional rule that "combining characters" (such as free-standing accents) may not appear on their own at the start of a string. The advantage of this form is that concatenating two fully normalized strings will always give a fully normalized string. For more information, see Character Model for the World Wide Web at http://www.w3.org/TR/charmod. |
If $normalizationForm
is not provided, NFC
is used as a default.
If $normalizationForm
is a form that is not supported by the implementation, the error "Unsupported normalization form" (FOCH0003
) is raised.
If $normalizationForm
is a zero-length string, no normalization is performed.
If $arg
is the empty sequence, the function returns a zero-length string.
Example |
Return value |
---|---|
|
|
|
|
|
£ |
|
|
|
|
|
|
For more information on Unicode normalization forms, see http://www.unicode.org/unicode/reports/tr15. The normalization charts, which identify the replacements for each character, for each form, can be found at http://www.unicode.org/charts/normalization.
not: Negates any Boolean value, turning false to true and true to false
not($arg as item( )*
) as xs:boolean
This function accepts a sequence of items, from which it calculates the effective Boolean value of the sequence as a whole before negating it. This means that when $arg
is either a single Boolean value false
, a zero-length string, the number 0
or NaN
, or the empty sequence, it returns true
. Otherwise, it usually returns false
. The detailed rules for evaluating the effective Boolean value of a sequence are described in "Effective Boolean Value" in Chapter 11.
If the effective Boolean value of $arg
is undefined, for example because $arg
is a sequence of multiple atomic values, the error "Invalid argument type" (FORG0006
) is raised.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
number: Constructs an xs:double
value
number($arg as xs:anyAtomicType?
) as xs:double
This function constructs an xs:double
value either from a node that contains a number, or from an atomic value. This function is useful for telling the processor to treat a node or value as a number, regardless of its declared type (if any). It returns the argument cast as an xs:double
.
The difference between using the number
function and the xs:double
constructor is that the number
function returns the value NaN
in the case that the argument cannot be cast to a numeric value, whereas the xs:double
constructor will raise an error.
If $arg
is not provided, the number
function uses the context item.
If $arg
is not provided, and the context item is undefined, the error XPDY0002
is raised.
The function returns the value NaN
in the case that the argument cannot be cast to a numeric value, in any of the following situations:
No argument is passed to it and the context item is undefined, or the context item is not a node.
$arg
is the empty sequence.
$arg
is a value that cannot be cast to xs:double
.
Example |
Return value |
---|---|
|
|
|
Error |
|
|
|
|
|
|
|
|
|
The two |
one-or-more: Verifies that a sequence contains one or more items
one-or-more($arg as item( )*
) as item( )+
If $arg
contains one or more items, $arg
is returned. Otherwise, the error "fn:one-or-more
called with a sequence containing zero items" (FORG0004
) is raised.
This function is useful when static typing is in effect, to avoid apparent static type errors. For example, suppose you wanted to call a user-defined concatNames
function that takes as an argument one or more strings (but not the empty sequence). To use the function, you might be tempted to write the expression:
local:concatNames (doc("catalog.xml")//name)
However, if static typing is used, this expression causes a static error if the name
element is optional in the schema. This is because the path expression might return the empty sequence, while the concatNames
function requires that at least one string be provided. A static error can be avoided by using the expression:
local:concatNames (one-or-more(doc("catalog.xml")//name))
In this case, no static error is raised. Rather, a dynamic error is raised if the path expression returns the empty sequence. For more information on static typing, see Chapter 14.
If static typing is not in effect, calling one-or-more
is not usually necessary, but it does no harm. The effect is usually to make explicit a runtime type check that would otherwise have been done automatically.
Example |
Return value |
---|---|
|
Error FORG0004 |
|
|
|
|
zero-or-one
, exactly-one
position: Gets the position of the context item within the context sequence
position( ) as xs:integer
This function returns an integer representing the position (starting with 1, not 0) of the current context item within the context sequence (the current sequence of items being processed). In XQuery, the position
function is almost invariably used in predicates (in square brackets) to test the relative position of a node.
If the context item is undefined, the error XPDY0002
is raised.
The expression:
doc("catalog.xml")/catalog/product[position( ) < 3]
returns the first two product
children of catalog
. You could also select the first two children of product
, with any name, using a wildcard, as in:
doc("catalog.xml")/catalog/product/*[position( ) < 3]
Note that the expression product[position( ) = 3]
is equal to the expression product[3]
, so the position
function is not very useful in this case. However, you might use the expression:
doc("catalog.xml")/catalog/product[position( ) = (1 to 3)]
to get the first three products.
last
prefix-from-QName: Gets the prefix associated with a particular qualified name
prefix-from-QName($arg as xs:QName?
) as xs:NCName?
This function returns the prefix associated with a particular qualified name. Note that the prefix associated with a qualified name selected from an input document will be the prefix that is used in that input document, not a prefix used in the query.
If $arg
is the empty sequence, the function returns the empty sequence.
If $arg
has no prefix (e.g., because it is associated with the default namespace, or it is not in a namespace), the function returns the empty sequence.
These examples use the input document names.xml
shown in Example A-5, in the section "local-name." They also assume that the prefixes pre2
and unpre
have been mapped to the namespaces http://datypic.com/pre and http://datypic.com/unpre, respectively, in the query prolog.
The prefix pre2
is used rather than pre
to demonstrate that when selecting nodes from an instance document, it is the prefix used in the instance document (not the query) that matters. However, when constructing a new element, the prefix used in the query itself is the one associated with the name, as shown in the last example.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
local-name-from-QName
, namespace-uri-from-QName
, namespace-uri-for-prefix
, in-scope-prefixes
QName: Constructs a QName from a URI and a local part
QName($paramURI as xs:string?
,$paramQName as xs:string
) as xs:QName
This function takes a namespace URI and a qualified (optionally prefixed) name as arguments and constructs a QName
value from them. If $paramQName
is prefixed, that prefix is retained in the resulting xs:QName
value.
Unlike the xs:QName
constructor, the QName
function does not require a literal argument. Therefore, the name could be the result of a dynamically evaluated expression. Because this function supplies all three components of the QName (local name, prefix, and URI) the effect does not depend on the context. There is no requirement that the prefix and namespace are bound in an outer expression.
If $paramURI
is a zero-length string or the empty sequence, and $paramQName
has a prefix, the error "Invalid lexical value" (FOCA0002
) is raised.
If $paramURI
is a zero-length string or the empty sequence, and $paramQName
does not have a prefix, the resulting name is considered to be in no namespace.
If $paramQName
does not follow the lexical rules for an XML qualified name (e.g., because it starts with a number or it contains two colons), the error "Invalid lexical value" (FOCA0002
) is raised.
Example |
Return value (xs:QName) |
---|---|
|
Namespace: http://datypic.com/prod Prefix: empty Local part: |
|
Namespace: http://datypic.com/prod Prefix: Local part: |
|
Namespace: empty Prefix: empty Local part: |
|
Error |
resolve-QName
, local-name-from-QName
, namespace-uri-from-QName
, namespace-uri-for-prefix
remove: Removes an item from a sequence based on its position
remove($target as item( )*
,$position as xs:integer
) as item( )*
This function returns a copy of $target
with the item at position $position
removed. Position numbers start at 1, not 0.
A common usage is to get the "tail" of a sequence (all items except the first). This can be written as remove($seq, 1)
.
If $position
is less than 1 or greater than the number of items in $target
, no items are removed.
If $target
is the empty sequence, the function returns the empty sequence.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
replace: Replaces substrings that match a pattern with a specified replacement string
replace($input as xs:string?
,$pattern as xs:string
,$replacement as xs:string
,$flags as xs:string
) as xs:string
The $pattern
argument is a regular expression; its syntax is covered in Chapter 18.
While it is nice to have the power of regular expressions, you don't have to be familiar with regular expressions to replace a particular sequence of characters; you can just specify the string you want replaced for $pattern
, as long as it doesn't contain any special characters.
The $replacement
argument specifies a string (not a pattern) that is to be used as a replacement. The $flags
argument allows for additional options in the interpretation of the regular expression, such as multi-line processing and case insensitivity. It is discussed in detail in "Using Flags" in Chapter 18.
Reluctant quantifiers and sub-expressions are two extremely useful features that can be used in conjunction with the replace
function. They are described in Chapter 18 in the sections entitled "Reluctant Quantifiers" and "Using Sub-Expressions with Replacement Variables," respectively.
If $input
is the empty sequence, it is treated like a zero-length string.
If $pattern
is not a valid regular expression, the error "Invalid regular expression" (FORX0002
) is raised.
If the entire $pattern
matches a zero-length string, for example q?
, the error "Regular expression matches zero-length string" (FORX0003
) is raised.
If $replacement
contains an unescaped dollar sign ($) that is not followed by a digit, the error "Invalid replacement string" (FORX0004
) is raised. ($ is used to escape a dollar sign.)
If $replacement
contains an unescaped backslash () that is not followed by a dollar sign ($), the error "Invalid replacement string" (FORX0004
) is raised. (\ is used to escape a backslash.)
If $flags
contains unsupported options, the error "Invalid regular expression flags" (FORX0001
) is raised.
If two overlapping strings match $pattern
, only the first is replaced.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
A zero-length string |
|
A zero-length string |
|
Error FORX0003 |
|
Error FORX0002 |
|
|
|
|
If more than one sub-expression matches, starting at the same position, the first alternative is chosen. This is exhibited by the second-to-last example, where Chap
is replaced instead of Chapter
.
The last example illustrates the meaning of non-overlapping. There are actually two substrings in the original string that match the pattern: ele
and eme
. Only the first of these is replaced, because the second overlaps the first. Note that this example uses a reluctant quantifier; otherwise, the whole eleme
would be replaced.
translate
, tokenize
resolve-QName: Constructs a QName from a string using the in-scope namespaces of an element
resolve-QName($qname as xs:string?
,$element as element( )
) as xs:QName?
The $qname
argument is a string representing a qualified name. It may be prefixed (for example, prod:number
), or unprefixed (for example, number
). The $element
argument is the element whose in-scope namespaces are to be used to determine which namespace URI is mapped to the prefix. If $qname
is unprefixed, the default namespace declaration of $element
is used. If there is no default namespace declaration in scope, the constructed QName has no namespace.
Note that when using the function, the prefix is never resolved using the context of the query. For example, if you map the prefix pre
to the namespace http://datypic.com/pre in the query prolog, that is irrelevant when you call the resolve-QName
function with the first argument pre:myName
. It is only relevant how that prefix is mapped in $element
. If you want to use the context of the query, you can simply use the xs:QName
constructor, as in xs:QName("pre:myName")
.
Typically, this function is used (in the absence of a schema) to resolve a QName appearing in the content of a document against the namespace context of the element where the QName appears. For example, to retrieve all products that carry the attribute xsi:type="prod:ProductType"
, you can use a path such as:
declare namespace prod = "http://datypic.com/prod"; doc("catalog.xml"//product[resolve-QName(@xsi:type, .) = xs:QName("prod:ProductType")]
This test allows the value of xsi:type
in the input document to use any prefix (not just prod
), as long as it is bound to the http://datypic.com/prod namespace.
If $qname
is prefixed, and that prefix is not mapped to a namespace in scope, the error "No namespace found for prefix" (FONS0004
) is raised.
If $qname
is not a lexically correct QName (for example, if it is not a valid XML name, or if it contains more than one colon), the error "Invalid lexical value" (FOCA0002
) is raised.
If $qname
is the empty sequence, the function returns the empty sequence.
These examples assume that the variable $root
is bound to the root
element, and $order
is bound to the order
element in the following input document:
<root> <order xmlns:ord="http://datypic.com/ord" xmlns="http://datypic.com"> <!-- ... --> </order> </root>
Example |
Return value (xs:QName) |
---|---|
|
Namespace: empty Prefix: empty Local part: |
|
Namespace: http://datypic.com Prefix: empty Local part: |
|
Error |
|
Namespace: http://datypic.com/ord Prefix: Local part: |
|
Error |
QName
, local-name-from-QName
, namespace-uri-from-QName
, namespace-uri-for-prefix
resolve-uri: Resolves a relative URI reference, based on a base URI
resolve-uri($relative as xs:string?
,$base as xs:string
) as xs:anyURI?
This function takes a base URI ($base
) and a relative URI ($relative
) as arguments and constructs an absolute URI.
If $base
is not provided, the base URI of the static context is used. This may have been set by the processor outside the scope of the query, or it may have been declared in the query prolog.
If $relative
is already an absolute URI, the function returns $relative
unchanged.
If $relative
cannot be resolved relative to $base
(e.g., because $base
itself is a relative URI), the error "Error in resolving a relative URI against a base URI in fn:resolve-uri
" (FORG0009
) is raised.
If $base
is not provided and the base URI of the static context is undefined, the error "Base-uri not defined in the static context" (FONS0005
) is raised.
If $relative
or $base
is not a syntactically valid URI, the error "Invalid argument to fn:resolve-uri
" (FORG0002
) is raised.
If $relative
is the empty sequence, the function returns the empty sequence.
Example[a] |
Return value |
---|---|
[a] | |
| |
| |
| |
| |
| |
| |
| |
[a] This table assumes that the base URI of the static context is http://datypic.com. |
base-uri
, encode-for-uri
reverse: Reverses the order of the items in a sequence
reverse($arg as item( )*
) as item( )*
This function returns the items in $arg
in reverse order. These items may be nodes, or atomic values, or both.
If $arg
is the empty sequence, the function returns the empty sequence.
Example |
Return value |
---|---|
|
|
|
|
|
|
root: Gets the root of the tree containing a node
root($arg as node( )?
) as node( )?
This function returns a document node if the $arg
node is part of a document, but it may also return an element if the $arg
node is not part of a document. The root
function can be used in conjunction with path expressions to find siblings and other elements that are in the same document. For example:
root($myNode)/descendant-or-self::product
retrieves all product
elements that are in the same document (or document fragment) as $myNode
.
Calling the root
function is similar to starting a path with / or //. It is more flexible in that it can appear anywhere in a path or other expression. Also, unlike starting a path with /, the root
function does not require the root to be a document node; it could be an element in the case of a document fragment.
If $arg
is the empty sequence, the function returns the empty sequence.
If $arg
is the root node, the function simply returns $arg
.
If $arg
is not provided, the function uses the context item.
If $arg
is not provided, and the context item is undefined, the error XPDY0002
is raised.
If $arg
is not provided, and the context item is not a node, the error XPTY0004
is raised.
Example |
Return value |
---|---|
|
All |
|
The root of the current context node |
|
The root of the current context node |
|
The document node of |
|
The |
round: Rounds a number to the nearest whole number
round($arg as numeric?
) as numeric?
The round
function is used to round a numeric value to the nearest integer. If the decimal portion of the number is .5 or greater, it is rounded up to the greater whole number (even if it is negative); otherwise, it is rounded down.
The function returns a numeric value of type xs:float
, xs:double
, xs:decimal
, or xs:integer
, depending on which type the argument is derived from. If $arg
is untyped, it is cast to xs:double
.
If $arg
is the empty sequence, the function returns the empty sequence.
If $arg
is between −0.5 and −0 (inclusive), the function may return 0
or −0
(it is implementation-dependent).
If $arg
is one of the values 0
, −0
, NaN
, INF
, or -INF
, the function returns this same value.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
round-half-to-even
, floor
, ceiling
round-half-to-even: Rounds a number using a specified precision
round-half-to-even($arg as numeric?
,$precision as xs:integer
) as numeric?
This type of rounding is used in financial and statistical applications so that the sum of a column of rounded numbers comes closer to the sum of the same unrounded numbers.
The returned value is rounded to the number of decimal places indicated by $precision
. For example, if the precision specified is 2
, the function rounds 594.3271
to 594.33
. If the precision is 0
, the number is rounded to an integer. Specifying a negative precision results in the number being rounded to the left of the decimal point. For example, if $precision
is −2
, the function rounds 594.3271
to 600
. If $precision
is omitted, it defaults to 0
.
If the argument is exactly half way between two values, it is rounded to whichever adjacent value is an even number.
The function returns a numeric value of type xs:float
, xs:double
, xs:decimal
, or xs:integer
, depending on the type from which the argument is derived. If $arg
is untyped, it is cast to xs:double
.
The function works best with xs:decimal
values. With xs:double
and xs:float
values, the rounding may not work exactly as expected. This is because an xs:double
value written as 0.005
, being only an approximation to a decimal number, is often not precisely midway between 0.01
and 0.02
.
If $arg
is the empty sequence, the function returns the empty sequence.
If $arg
is between −0.5 and −0 (inclusive), the function may return 0
or −0
(it is implementation-dependent).
If $arg
is one of the values 0
, −0
, NaN
, INF
, or -INF
, the function returns this same value.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
round
, floor
, ceiling
seconds-from-dateTime: Gets the seconds portion of a date/time
seconds-from-dateTime($arg as xs:dateTime?
) as xs:decimal?
This function returns the seconds portion of an xs:dateTime
value, as a decimal number.
If $arg
is the empty sequence, the function returns the empty sequence.
seconds-from-dateTime(xs:dateTime("2006-08-15T10:30:23.5"))
returns 23.5
.
seconds-from-time
seconds-from-duration: Gets the normalized number of seconds in a duration
seconds-from-duration($arg as xs:duration?
) as xs:decimal?
This function calculates the seconds component of a normalized xs:duration
value, as a decimal number between −60 and 60 exclusive. This is not necessarily the same as the number that appears before the S
in the value. For example, if the duration is PT90S
, the function returns 30
rather than 90
. This is because 60 of those seconds are considered to be 1 minute, and the normalized value would therefore be PT1M30S
.
If $arg
is a negative duration, the function returns a negative value.
If $arg
is the empty sequence, the function returns the empty sequence.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
seconds-from-time: Gets the seconds portion of a time
seconds-from-time($arg as xs:time?
) as xs:decimal?
This function returns the seconds portion of an xs:time
value, as a decimal number.
If $arg
is the empty sequence, the function returns the empty sequence.
seconds-from-time(xs:time("10:30:23.5"))
returns 23.5
.
seconds-from-dateTime
starts-with: Determines whether one string starts with another
starts-with($arg1 as xs:string?
,$arg2 as xs:string?
,$collation as xs:string
) as xs:boolean
This function returns an xs:boolean
value indicating whether one string ($arg1
) starts with the characters of another string ($arg2
). Leading and trailing whitespace is significant, so you may want to use the normalize-space
function to trim the strings before using this function.
If $arg2
is a zero-length string or the empty sequence, the function returns true
.
If $arg1
is a zero-length string or the empty sequence, but $arg2
is not, the function returns false
.
If $collation
is provided, the comparison uses that collation; otherwise, the default collation is used. Collations are described in Chapter 17.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ends-with
, contains
, matches
static-base-uri: Gets the base URI of the static context
static-base-uri( ) as xs:anyURI?
This function returns the base URI of the static context. This may have been set by the processor outside the scope of the query, or it may have been declared in the query prolog.
The base URI of the static context is used for constructed elements and for resolving relative URIs when no other base URI is available. It is not the same as the base URI of any given element or document node. For more information, see "The base URI of the static context" in Chapter 20.
static-base-uri( )
might return http://datypic.com/prod, if that is the base URI of the static context.
base-uri
string: Returns the string value of an item
string($arg as item( )?
) as xs:string
If $arg
is a node, this function returns its string value. The method of determining the string value of a node depends on its kind. Table A-6 describes how the string value is determined for each node kind.
If $arg
is an atomic value, the function returns that value, cast to xs:string
. For more information on casting a typed value to string
, see "Casting to xs:string or xs:untypedAtomic" in Chapter 11.
Table A-6. String value based on node kind
Node kind |
String value |
---|---|
Element |
The text content of the element and all its descendant elements, concatenated together in document order. Attribute values are not included. |
Document |
The string values of all of the descendant elements concatenated together in document order. That is, the text content of the original XML document, minus all the markup. |
Attribute |
The attribute value |
Text |
The text itself |
Processing instruction |
The content of the processing instruction (everything but its target) |
Comment |
The content of the comment |
When $arg
is a typed node, there may be some differences in the formatting such as leading and trailing whitespace and leading zeros. This is because the implementation can optionally provide the canonical representation of the value instead of the actual characters that appear in an input document. For example, if <myInt> 04 </myInt>
appears in an input document, and it is validated and annotated with the type xs:integer
, its string value may be returned as 4
without leading or trailing spaces instead of 04
.
If you want the resulting value to be some type other than xs:string
, you should use the data
function instead. For example, if $myInt
is bound to the myInt
element from the previous prargraph, the expression data($myInt)
returns the integer value 4
.
If $arg
is not provided, the function uses the context item.
If $arg
is not provided, and the context item is undefined, the error XPDY0002
is raised.
If $arg
is the empty sequence, the function returns a zero-length string.
If $arg
is an element with empty content (e.g., <a></a>
or <a/>
), the function returns a zero-length string.
Given the fourth product element in our catalog:
<product dept="MEN"> <number>784</number> <name language="en">Cotton Dress Shirt</name> <colorChoices>white gray</colorChoices> <desc>Our <i>favorite</i> shirt!</desc> </product>
the string value of the product
element is:
784Cotton Dress Shirtwhite grayOur favorite shirt!
assuming a schema is in place; otherwise, there will be additional whitespace between the values. The string value of the number
element is 784
and the string value of the desc
element is Our favorite shirt!
.
data
string-join: Concatenates a sequence of strings together, optionally using a separator
string-join($arg1 as xs:string*
,$arg2 as xs:string
) as xs:string
The $arg1
argument specifies the sequence of strings to concatenate, while $arg2
specifies the separator. If $arg2
is a zero-length string, no separator is used.
If $arg1
is the empty sequence, the function returns a zero-length string.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
A zero-length string |
concat
string-length: Finds the length of a string
string-length($arg as xs:string?
) as xs:integer
This function returns an xs:integer
value indicating the number of characters in the string. Whitespace is significant, so leading and trailing whitespace characters are counted.
If $arg
is not provided, the function uses the string value of the context item.
If $arg
is not provided, and the context item is undefined, the error XPDY0002
is raised.
If $arg
is the empty sequence, the function returns 0
.
Unlike some programming languages, Unicode characters above 65535 count as one character, not two.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
string-to-codepoints: Converts a string to a sequence of Unicode code-point values
string-to-codepoints($arg as xs:string?
) as xs:integer*
This function returns a sequence of xs:integer
values representing the Unicode code points.
If $arg
is a zero-length string or the empty sequence, the function returns the empty sequence.
Example |
Return value |
---|---|
|
|
|
|
|
|
codepoints-to-string
subsequence: Extracts a portion of a sequence, based on a starting position and optional length
subsequence($sourceSeq as item( )*
,$startingLoc as xs:double
,$length as xs:double
) as item( )*
This function returns a sequence of $length
items of $sourceSeq
, starting at the position $startingLoc
. The first item in the sequence is considered to be at position 1, not 0. If no $length
is passed, or if $length
is greater than the number of items that can be returned, the function includes items to the end of the sequence. An alternative to calling the subsequence
function is using a predicate. For example, subsequence($a,3,4)
is equivalent to $a[position( ) = (3 to 6)]
.
If $startingLoc
is zero or negative, the subsequence starts at the beginning of the sequence and still goes to $startingLoc
plus $length
, so the actual length of the subsequence may be less than $length
.
If $startingLoc
is greater than the number of items in the sequence, the function returns the empty sequence.
If $sourceSeq
is the empty sequence, the function returns the empty sequence.
The function will accept xs:double
values for $startingLoc
and $length
, in which case they are rounded to the nearest integer. This is because the result type of many calculations on untyped data is xs:double
. Accepting xs:double
values allows the $startingLoc
and $length
arguments to be calculated and passed directly to the function.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
substring: Extracts part of a string, based on a starting position and optional length
substring($sourceString as xs:string?
,$startingLoc as xs:double
,$length as xs:double
) as xs:string
The $startingLoc
argument indicates the starting location for the substring, where the first character is at position 1 (not 0). The optional $length
argument indicates the number of characters to include, relative to the starting location. If no $length
is provided, the entire rest of the string is included.
The function returns all characters whose position is greater than or equal to $startingLoc
and less than ($startingLoc + $length
). The $startingLoc
number can be zero or negative, in which case the function starts at the beginning of the string, and still only include characters up to (but not including) the position at ($startingLoc
+ $length)
. If ($startingLoc
+ $length)
is greater than the length of the string, the rest of the string is included.
If $sourceString
is the empty sequence, the function returns a zero-length string.
If $startingLoc
is greater than the length of the string, the function returns a zero-length string.
The function will accept xs:double
values for $startingLoc
and $length
, in which case they are rounded to the nearest integer.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
A zero-length string |
|
|
|
|
|
A zero-length string |
|
A zero-length string |
|
A zero-length string |
substring-after
, substring-before
substring-after: Extracts the substring that is after the first occurrence of another specified string
substring-after($arg1 as xs:string?
,$arg2 as xs:string?
,$collation as xs:string
) as xs:string
This function extracts all the characters of a string ($arg1
) that appear after the first occurrence of another specified string ($arg2
).
If $arg1
does not contain $arg2
, the function returns a zero-length string.
If $arg2
is a zero-length string or the empty sequence, the function returns $arg1
in its entirety.
If $arg1
is a zero-length string or the empty sequence, and $arg1
is not, the function returns a zero-length string.
If $collation
is provided, the comparison uses that collation; otherwise, the default collation is used.
Example |
Return value |
---|---|
|
|
|
|
|
A zero-length string |
|
A zero-length string |
|
|
|
A zero-length string |
substring
, substring-before
substring-before: Extracts the substring that is before the first occurrence of another specified string
substring-before($arg1 as xs:string?
,$arg2 as xs:string?
,$collation as xs:string
) as xs:string
This function extracts all the characters of a string ($arg1
) that appear before the first occurrence of another specified string ($arg2
).
If $arg1
does not contain $arg2
, the function returns a zero-length string.
If $arg1
is a zero-length string or the empty sequence, the function returns a zero-length string.
If $arg2
is a zero-length string or the empty sequence, the function returns a zero-length string.
If $collation
is provided, the comparison uses that collation; otherwise, the default collation is used. Collations are described in Chapter 17.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
A zero-length string |
|
A zero-length string |
|
A zero-length string |
|
A zero-length string |
substring
, substring-after
sum: Calculates the total value of the items in a sequence
sum($arg as xs:anyAtomicType*
,$zero as xs:anyAtomicType?
) as xs:anyAtomicType?
The $arg
sequence can contain a mixture of numeric and untyped values. Numeric values are promoted as necessary to make them all the same type. Untyped values are cast as numeric xs:double
values.
The function can also be used on duration values, so the $arg
sequence can contain all xs:yearMonthDuration
values or all xs:dayTimeDuration
values (but not a mixture of the two). The $arg
sequence cannot contain a mixture of duration and numeric values.
The $zero
argument allows you to specify an alternate value for the sum of the empty sequence. If $arg
is the empty sequence, and $zero
is provided, the function returns $zero
. The $zero
argument could be the empty sequence, the integer 0
, the value NaN
, a duration of zero seconds, or any other atomic value. The main use cases of $zero
are (a) to supply numeric zero in the desired datatype, e.g., xs:decimal
, and (b) to supply a zero duration if you are summing durations. Since the processor, in the absence of static typing, cannot tell the difference between a zero-length sequence of numbers and a zero-length sequence of durations, this is the only way to tell it which kind of value is being totaled.
If $arg
is the empty sequence, and $zero
is not provided, the function returns the xs:integer
value 0
.
If $arg
contains any NaN
values, the function returns NaN
.
If $arg
contains untyped values that cannot be cast to xs:double
, the error "Invalid value for cast/constructor" (FORG0001
) is raised.
If $arg
contains values of different types, or values that are not numbers or durations, the error "Invalid argument type" (FORG0006
) is raised.
Example |
Return value |
---|---|
|
|
|
|
|
Error |
|
|
|
|
|
Error |
|
|
|
|
timezone-from-date: Gets the time zone of a date
timezone-from-date($arg as xs:date?
) as xs:dayTimeDuration?
This function returns the time zone of an xs:date
value, offset from UTC, as an xs:dayTimeDuration
value between -PT14H
and PT14H
. If the time zone is UTC, the value PT0S
is returned.
If $arg
is the empty sequence, the function returns the empty sequence.
If $arg
does not have an explicit time zone, the function returns the empty sequence. It does not return the implicit time zone.
timezone-from-date(xs:date("2006-08-15-05:00"))
returns -PT5H
.
timezone-from-date(xs:date("2006-08-15"))
returns the empty sequence, regardless of the implicit time zone.
timezone-from-dateTime
, timezone-from-time
timezone-from-dateTime: Gets the time zone of a date/time
timezone-from-dateTime($arg as xs:dateTime?
) as xs:dayTimeDuration?
This function returns the time zone of an xs:dateTime
value, offset from UTC, as an xs:dayTimeDuration
value between -PT14H
and PT14H
. If the time zone is UTC, the value PT0S
is returned.
If $arg
is the empty sequence, the function returns the empty sequence.
If $arg
does not have an explicit time zone, the function returns the empty sequence. It does not return the implicit time zone.
timezone-from-dateTime(xs:dateTime("2006-08-15T10:30:23-05:00"))
returns -PT5H
.
timezone-from-dateTime(xs:dateTime("2006-08-15T10:30:23"))
returns the empty sequence, regardless of the implicit time zone.
timezone-from-date
, timezone-from-time
timezone-from-time: Gets the time zone of a time
timezone-from-time($arg as xs:time?
) as xs:dayTimeDuration?
This function returns the time zone of an xs:time
value, offset from UTC, as an xs:dayTimeDuration
value between -PT14H
and PT14H
. If the time zone is UTC, the value PT0S
is returned.
If $arg
is the empty sequence, the function returns the empty sequence.
If $arg
does not have an explicit time zone, the function returns the empty sequence. It does not return the implicit time zone.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
timezone-from-dateTime
, timezone-from-date
tokenize: Breaks a string into a sequence of strings, using a regular expression to identify the separator
tokenize($input as xs:string?
,$pattern as xs:string
,$flags as xs:string
) as xs:string*
The $pattern
argument is a regular expression that represents the separator. The regular expression syntax is covered in Chapter 18. The simplest patterns can be a single space, or a string that contains the separator character, such as ,
. However, certain characters must be escaped in regular expressions, namely . ? * + | ^ $ { } ( ) [ and ]. Table A-7 shows some useful patterns for separators.
Table A-7. Useful separator patterns
Pattern |
Meaning |
---|---|
|
A single whitespace character (space, tab, carriage return, or line feed) |
|
One or more consecutive whitespace characters |
|
Comma |
|
A comma followed by zero or more whitespace characters |
|
One or more consecutive commas and/or whitespace characters |
|
Tab character |
|
One or more consecutive carriage return and/or line-feed characters |
|
One or more nonword characters |
The separators are not included in the result strings. If two adjacent separators appear, a zero-length string is included in the result sequence. If the string starts with the separator, a zero-length string is the first value returned. Likewise, if the string ends with the separator, a zero-length string is the last value in the result sequence.
The $flags
argument allows for additional options in the interpretation of the regular expression, such as multi-line processing and case insensitivity. It is discussed in detail in "Using Flags" in Chapter 18.
If a particular point in the string could match more than one alternative, the first alternative is chosen. This is exhibited in the last row in the Example table, where the function considers the comma to be the separator, even though ",x"
also applies.
If $input
is the empty sequence, or $input
is a zero-length string, the function returns the empty sequence.
If $pattern
is not a valid regular expression, the error "Invalid regular expression" (FORX0002
) is raised.
If the entire $pattern
matches a zero-length string, for example q?
, the error "Regular expression matches zero-length string" (FORX0003
) is raised.
If $flags
contains unsupported options, the error "Invalid regular expression flags" (FORX0001
) is raised.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Error |
|
|
trace: Traces the value of an item for debugging or logging purposes
trace($value as item( )*
,$label as xs:string
) as item( )*
This function accepts an item and a label for that item, and returns the item unchanged. The exact behavior of the function is implementation-dependent, but generally the processor puts the label and the value of the item in a logfile or user console.
trace($var1, "The value of $var1 is: ")
might write the string The value of $var1 is: 4
to a logfile.
error
translate: Replace individual characters in a string with other individual characters
translate($arg as xs:string?
,$mapString as xs:string
,$transString as xs:string
) as xs:string
The $mapString
argument is a list of characters to be changed, and $transString
is the list of replacement characters. Each character in $mapString
is replaced by the character in the same position in $transString
. If $mapString
is longer than $transString
, the characters in $mapString
that have no corresponding character in $transString
are not included in the result. Characters in the original string that do not appear in $mapString
are copied to the result unchanged.
Note that this function is only for replacing individual characters with other individual characters or removing individual characters. If you want to replace sequences of characters, you should use the replace
function instead. This function is sometimes used for translating strings between lowercase and uppercase, but the upper-case
and lower-case
functions do this more robustly based on Unicode mappings.
If $arg
is the empty sequence, the function returns a zero-length string.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
A zero-length string |
|
A zero-length string |
trace
true: Constructs a Boolean true
value
true( ) as xs:boolean
This function, which takes no arguments, is useful for constructing the Boolean value true
. XQuery uses the false( )
and true( )
functions instead of keywords false
and true
. This is most commonly used to supply a value in a function call where a Boolean value is required.
The expression true( )
returns the xs:boolean
value true
.
false
unordered: Signals to the processor that order is insignificant
unordered($sourceSeq as item( )*
) as item( )*
In cases where the order of the results does not matter, the processor may be much more efficient if it does not have to keep track of order. This is especially true for FLWORs that perform joins. For example, processing multiple variable bindings in a for
clause might be significantly faster if the processor can decide which variable binding controls the join without regard to the order of the results. A query author can tell the processor that order does not matter by enclosing an expression in a call to the unordered
function.
unordered( for $item in doc("order.xml")//item, $product in doc("catalog.xml")//product where $item/@num = $product/number return <item number="{$item/@num}" name="{$product/name}" quantity="{$item/@quantity}"/> )
upper-case: Converts a string to uppercase
upper-case($arg as xs:string?
) as xs:string
The mappings between lowercase and uppercase characters are determined by Unicode case mappings. If a character in $arg
does not have a corresponding uppercase character, it is included in the result string unchanged.
For English, you can do a case-blind comparison by writing upper-case($A)=upper-case($B)
(or use lower-case
instead). However this doesn't always work well for other languages. It's better to use a case-insensitive collation.
If $arg
is the empty sequence, the function returns a zero-length string.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
lower-case
year-from-date: Gets the year portion of a date
year-from-date($arg as xs:date?
) as xs:integer?
This function returns the year portion of an xs:date
value as an integer.
If the year is negative, the function returns a negative number.
If $arg
is the empty sequence, the function returns the empty sequence.
year-from-date(xs:date("2006-08-15"))
returns 2006
.
year-from-dateTime
year-from-dateTime: Gets the year portion of a date/time
year-from-dateTime($arg as xs:dateTime?
) as xs:integer?
This function returns the year portion of an xs:dateTime
value as an integer.
If the year is negative, the function returns a negative number.
If $arg
is the empty sequence, the function returns the empty sequence.
year-from-dateTime(xs:dateTime("2006-08-15T10:30:23"))
returns 2006
.
year-from-date
years-from-duration: Gets the normalized number of years in a duration
years-from-duration($arg as xs:duration?
) as xs:integer?
This function calculates the years component of a normalized xs:duration
value. This is not necessarily the same as the integer that appears before the Y
in the value. For example, if the duration is P1Y18M
, the function returns 2
rather than 1
. This is because 18 months is equal to 1.5 years, and the normalized value is therefore P2Y6M
.
If $arg
is a negative duration, the function returns a negative value.
If $arg
is the empty sequence, the function returns the empty sequence.
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
|
zero-or-one: Verifies that a sequence does not contain more than one item
zero-or-one($arg as item( )*
) as item( )?
If $arg
contains zero or one items, $arg
is returned. Otherwise, the error "fn:zero-or-one
called with a sequence containing more than one item" (FORG0003
) is raised.
This function is useful when static typing is in effect, to avoid apparent static type errors. For example, to use the number
function on a particular price, you might be tempted to write the expression:
number (doc("prices.xml")//prod[@num = 557]/price)
However, if static typing is used, this expression causes a static error. This is because there could be more than one price
element that matches that criterion, while the number
function requires that one zero or one item be provided. A static error can be avoided by using the expression:
number (zero-or-one(doc("prices.xml")//prod[@num = 557]/price))
In this case, no static error is raised. Rather, a dynamic error is raised if more than one price
element is returned by the path expression. For more information on static typing, see Chapter 14.
If static typing is not in effect, calling exactly-one
is not usually necessary, but it does no harm. The effect is usually to make explicit a runtime type check that would otherwise have been done automatically.
Example |
Return value |
---|---|
|
|
|
|
|
Error |
one-or-more
, exactly-one
3.145.9.148