W3C

XQuery 1.0 and XPath 2.0 Functions and Operators

W3C Working Draft 15 November 2002

This version:
http://www.w3.org/TR/2002/WD-xquery-operators-20021115/
Latest version:
http://www.w3.org/TR/xquery-operators/
Previous version:
http://www.w3.org/TR/2002/WD-xquery-operators-20020816/
Editors:
Ashok Malhotra (XML Query and XSL WGs), Microsoft <ashokma@microsoft.com>
Jim Melton (XML Query WG), Oracle Corp <jim.melton@acm.org>
Jonathan Robie (XML Query WG), Data Direct Technologies <jonathan.robie@datadirect-technolgies.com>
Norman Walsh (XSL WG), Sun Microsystems <Norman.Walsh@Sun.COM>

Abstract

This document defines basic operators and functions on the datatypes defined in [XML Schema Part 2: Datatypes] for use in XQuery, XPath, XSLT and other related XML standards. It also discusses operators and functions on nodes and node sequences as defined in the [XQuery 1.0 and XPath 2.0 Data Model] for use in XQuery, XPath, XSLT and other related XML standards.

Status of this Document

This section describes the status of this document at the time of its publication. Other documents may supersede this document. The latest status of this document series is maintained at the W3C.

This is a Public Working Draft of this document for review by W3C Members and other interested parties. It is a draft document and may be updated, replaced, or made obsolete by other documents at any time. It is inappropriate to use W3C Working Drafts as reference material or to cite them as other than "work in progress". This is work in progress and does not imply endorsement by the W3C membership.

This document describes constructor functions, operators, and functions that are used in [XPath 2.0], [XQuery 1.0: An XML Query Language], and [XSLT 2.0] and possibly other W3C specifications.

Among the more important changes from the previous version of this document is that an error function has been added (see 3 The Error Function) and there has been some amplification of the rules for constructing simple types and for casting (see section 4 Constructor Functions and section 16 Casting Functions). This work is not yet complete and will be further elaborated in forthcoming versions of this specification. Another area currently under discussion is whether the input timezone on date and time types needs to be preserved. This also impacts the functions to add and remove timezones from date and time values that is discussed in 8.6 Timezone Functions on dateTime, date, and time.

This document has been produced following the procedures set out for the W3C Process. This document was produced through the efforts of a joint task force of the W3C XML Query Working Group and the W3C XML Schema Working Group (both part of the W3C XML Activity) and a second joint task force of the W3C XML Query Working Group and the W3C XSL Working Group (part of the W3C Style Activity). It is designed to be read in conjunction with the following documents: [XQuery 1.0 and XPath 2.0 Data Model], [XPath 2.0], [XQuery 1.0: An XML Query Language] and [XSLT 2.0].

The following are identified as high priority issues. Reviewers are requested to provide feedback on these issues using the address below.

[Issue 186: Preserve original timezone in date/time values.]

[Issue 185: Remove op:duration-equal]

Public comments on this document and its open issues are welcome. Comments should be sent to the W3C XPath/XQuery mailing list, public-qt-comments@w3.org (archived at http://lists.w3.org/Archives/Public/public-qt-comments/).

Patent disclosures relevant to this specification may be found on the XML Query Working Group's patent disclosure page and the XSL Working Group's patent disclosure page.

A list of current W3C Recommendations and other technical documents can be found at http://www.w3.org/TR/.

Table of Contents

1 Introduction
    1.1 Terminology
    1.2 Datatypes
    1.3 Syntax
    1.4 Notations
    1.5 Namespace Prefix
2 Accessors
    2.1 fn:node-kind
    2.2 fn:node-name
    2.3 fn:string
    2.4 fn:data
    2.5 fn:base-uri
    2.6 fn:unique-ID
3 The Error Function
    3.1 Examples
4 Constructor Functions
    4.1 Constructor Functions for XML Schema Built-in Types
    4.2 Constructor Functions for User-Defined Types
5 Functions and Operators on Numerics
    5.1 Numeric Types
    5.2 Operators on Numeric Values
        5.2.1 op:numeric-add
        5.2.2 op:numeric-subtract
        5.2.3 op:numeric-multiply
        5.2.4 op:numeric-divide
        5.2.5 op:numeric-integer-divide
        5.2.6 op:numeric-mod
        5.2.7 op:numeric-unary-plus
        5.2.8 op:numeric-unary-minus
    5.3 Comparison of Numeric Values
        5.3.1 op:numeric-equal
        5.3.2 op:numeric-less-than
        5.3.3 op:numeric-greater-than
    5.4 Functions on Numeric Values
        5.4.1 fn:floor
        5.4.2 fn:ceiling
        5.4.3 fn:round
6 Functions on Strings
    6.1 String Types
    6.2 Functions to Assemble and Disassemble Strings
        6.2.1 fn:codepoints-to-string
        6.2.2 fn:string-to-codepoints
    6.3 Equality and Comparison of Strings
        6.3.1 fn:compare
    6.4 Functions on String Values
        6.4.1 fn:concat
        6.4.2 fn:string-join
        6.4.3 fn:starts-with
        6.4.4 fn:ends-with
        6.4.5 fn:contains
        6.4.6 fn:substring
        6.4.7 fn:string-length
        6.4.8 fn:substring-before
        6.4.9 fn:substring-after
        6.4.10 fn:normalize-space
        6.4.11 fn:normalize-unicode
        6.4.12 fn:upper-case
        6.4.13 fn:lower-case
        6.4.14 fn:translate
        6.4.15 fn:string-pad
        6.4.16 fn:matches
        6.4.17 fn:replace
        6.4.18 fn:tokenize
        6.4.19 fn:escape-uri
7 Functions and Operators on Booleans
    7.1 Boolean Constructor Functions
        7.1.1 fn:true
        7.1.2 fn:false
    7.2 Operators on Boolean Values
        7.2.1 op:boolean-equal
        7.2.2 op:boolean-less-than
        7.2.3 op:boolean-greater-than
    7.3 Functions on Boolean Values
        7.3.1 fn:not
8 Functions and Operators on Durations, Dates, and Times
    8.1 Duration, Date, and Time Types
    8.2 Two Totally Ordered Subtypes of Duration
        8.2.1 yearMonthDuration
        8.2.2 dayTimeDuration
    8.3 Comparisons of Duration, Date and Time Values
        8.3.1 op:duration-equal
        8.3.2 op:yearMonthDuration-equal
        8.3.3 op:yearMonthDuration-less-than
        8.3.4 op:yearMonthDuration-greater-than
        8.3.5 op:dayTimeDuration-equal
        8.3.6 op:dayTimeDuration-less-than
        8.3.7 op:dayTimeDuration-greater-than
        8.3.8 op:dateTime-equal
        8.3.9 op:dateTime-less-than
        8.3.10 op:dateTime-greater-than
        8.3.11 op:date-equal
        8.3.12 op:date-less-than
        8.3.13 op:date-greater-than
        8.3.14 op:time-equal
        8.3.15 op:time-less-than
        8.3.16 op:time-greater-than
        8.3.17 op:gYearMonth-equal
        8.3.18 op:gYear-equal
        8.3.19 op:gMonthDay-equal
        8.3.20 op:gMonth-equal
        8.3.21 op:gDay-equal
    8.4 Component Extraction Functions on Duration, Date and Time Values
        8.4.1 fn:get-years-from-yearMonthDuration
        8.4.2 fn:get-months-from-yearMonthDuration
        8.4.3 fn:get-days-from-dayTimeDuration
        8.4.4 fn:get-hours-from-dayTimeDuration
        8.4.5 fn:get-minutes-from-dayTimeDuration
        8.4.6 fn:get-seconds-from-dayTimeDuration
        8.4.7 fn:get-year-from-dateTime
        8.4.8 fn:get-month-from-dateTime
        8.4.9 fn:get-day-from-dateTime
        8.4.10 fn:get-hours-from-dateTime
        8.4.11 fn:get-minutes-from-dateTime
        8.4.12 fn:get-seconds-from-dateTime
        8.4.13 fn:get-timezone-from-dateTime
        8.4.14 fn:get-year-from-date
        8.4.15 fn:get-month-from-date
        8.4.16 fn:get-day-from-date
        8.4.17 fn:get-timezone-from-date
        8.4.18 fn:get-hours-from-time
        8.4.19 fn:get-minutes-from-time
        8.4.20 fn:get-seconds-from-time
        8.4.21 fn:get-timezone-from-time
    8.5 Arithmetic Functions on yearMonthDuration and dayTimeDuration
        8.5.1 op:add-yearMonthDurations
        8.5.2 op:subtract-yearMonthDurations
        8.5.3 op:multiply-yearMonthDuration
        8.5.4 op:divide-yearMonthDuration
        8.5.5 op:add-dayTimeDurations
        8.5.6 op:subtract-dayTimeDurations
        8.5.7 op:multiply-dayTimeDuration
        8.5.8 op:divide-dayTimeDuration
    8.6 Timezone Functions on dateTime, date, and time
        8.6.1 fn:add-timezone-to-dateTime
        8.6.2 fn:remove-timezone-from-dateTime
        8.6.3 fn:add-timezone-to-date
        8.6.4 fn:add-timezone-to-time
        8.6.5 fn:remove-timezone-from-time
    8.7 Adding and Subtracting Durations From dateTime, date and time
        8.7.1 fn:subtract-dateTimes-yielding-yearMonthDuration
        8.7.2 fn:subtract-dateTimes-yielding-dayTimeDuration
        8.7.3 op:subtract-dates
        8.7.4 op:subtract-times
        8.7.5 op:add-yearMonthDuration-to-dateTime
        8.7.6 op:add-dayTimeDuration-to-dateTime
        8.7.7 op:subtract-yearMonthDuration-from-dateTime
        8.7.8 op:subtract-dayTimeDuration-from-dateTime
        8.7.9 op:add-yearMonthDuration-to-date
        8.7.10 op:add-dayTimeDuration-to-date
        8.7.11 op:subtract-yearMonthDuration-from-date
        8.7.12 op:subtract-dayTimeDuration-from-date
        8.7.13 op:add-dayTimeDuration-to-time
        8.7.14 op:subtract-dayTimeDuration-from-time
9 Functions Related to QNames
    9.1 Constructor Functions for QNames
        9.1.1 fn:QName-in-context
    9.2 Functions Related to QNames
        9.2.1 op:QName-equal
        9.2.2 fn:get-local-name-from-QName
        9.2.3 fn:get-namespace-from-QName
        9.2.4 fn:get-namespace-uri-for-prefix
        9.2.5 fn:get-in-scope-namespaces
10 Functions and Operators for anyURI
    10.1 Constructor Functions for anyURI
        10.1.1 fn:resolve-uri
    10.2 Functions on anyURI
        10.2.1 op:anyURI-equal
11 Functions and Operators on base64Binary and hexBinary
    11.1 Comparisons of base64Binary and hexBinary Values
        11.1.1 op:hexBinary-equal
        11.1.2 op:base64Binary-equal
12 Functions and Operators on NOTATION
    12.1 Functions on NOTATION
        12.1.1 op:NOTATION-equal
13 Functions and Operators on Nodes
    13.1 Functions and Operators on Nodes
        13.1.1 fn:name
        13.1.2 fn:local-name
        13.1.3 fn:namespace-uri
        13.1.4 fn:number
        13.1.5 fn:lang
        13.1.6 op:node-equal
        13.1.7 fn:deep-equal
        13.1.8 op:node-before
        13.1.9 op:node-after
        13.1.10 fn:root
14 Functions and Operators on Sequences
    14.1 Constructor Functions on Sequences
        14.1.1 op:to
    14.2 Functions and Operators on Sequences
        14.2.1 fn:boolean
        14.2.2 op:concatenate
        14.2.3 fn:item-at
        14.2.4 fn:index-of
        14.2.5 fn:empty
        14.2.6 fn:exists
        14.2.7 fn:distinct-nodes
        14.2.8 fn:distinct-values
        14.2.9 fn:insert
        14.2.10 fn:remove
        14.2.11 fn:subsequence
        14.2.12 fn:unordered
    14.3 Equals, Union, Intersection and Except
        14.3.1 fn:sequence-deep-equal
        14.3.2 fn:sequence-node-equal
        14.3.3 op:union
        14.3.4 op:intersect
        14.3.5 op:except
    14.4 Aggregate Functions
        14.4.1 fn:count
        14.4.2 fn:avg
        14.4.3 fn:max
        14.4.4 fn:min
        14.4.5 fn:sum
    14.5 Functions that Generate Sequences
        14.5.1 fn:id
        14.5.2 fn:idref
        14.5.3 fn:document
        14.5.4 fn:collection
        14.5.5 fn:input
15 Context Functions
    15.1 fn:context-item
    15.2 fn:position
    15.3 fn:last
    15.4 fn:current-dateTime
        15.4.1 Examples
    15.5 fn:current-date
        15.5.1 Examples
    15.6 fn:current-time
        15.6.1 Examples
    15.7 fn:default-collation
    15.8 fn:implicit-timezone
16 Casting Functions
    16.1 Casting from primitive types to primitive types
    16.2 Casting to derived types
    16.3 Casting from derived types to parent types
    16.4 Casting within a branch of the type hierarchy
    16.5 Casting across the type hierarchy
    16.6 Casting from string and anySimpleType
    16.7 Casting to string and anySimpleType
    16.8 Casting to numeric types
    16.9 Casting to duration and date and time types
    16.10 Casting to boolean
    16.11 Casting to base64Binary and hexBinary
    16.12 Casting to anyURI
    16.13 Casting to NOTATION

Appendices

A References
    A.1 Normative
    A.2 Non-normative
B Compatibility with XPath 1.0 (Non-Normative)
C Illustrative User-written Functions (Non-Normative)
    C.1 eg:if-empty() and eg:if-absent()
        C.1.1 eg:if-empty()
        C.1.2 eg:if-absent()
    C.2 union, intersect and except on sequences of values
        C.2.1 eg:value-union()
        C.2.2 eg:value-intersect()
        C.2.3 eg:value-except()
D Functions and Operators Issues List (Non-Normative)
E ChangeLog since Last Public Version on 2002-08-16 (Non-Normative)
F Function and Operator Quick Reference (Non-Normative)
    F.1 Functions and Operators by Section
    F.2 Functions and Operators Alphabetically


1 Introduction

[XML Schema Part 2: Datatypes] defines a number of primitive and derived datatypes, collectively known as built-in datatypes. This document defines operations on those datatypes for use in XQuery, XPath, XSLT and related XML standards. This document also discusses operators and functions on nodes and node sequences as defined in the [XQuery 1.0 and XPath 2.0 Data Model] for use in XQuery, XPath, XSLT and other related XML standards.

1.1 Terminology

The terminology used to describe the functions and operators on [XML Schema Part 2: Datatypes] is defined in the body of this specification. The terms defined in the following list are used in building those definitions:

[Definition] for compatibility

A feature of this specification included to ensure that implementations that use this feature remain compatible with [XPath 1.0]

[Definition] may

Conforming documents and processors are permitted to but need not behave as described.

[Definition] must

Conforming documents and processors are required to behave as described; otherwise, they are non-conformant or in error.

[Definition] implementation defined

Possibly differing between implementations, but specified by the implementor for each particular implementation.

[Definition] implementation dependent

Possibly differing between implementations, but not specified by this or other W3C specification, and not required to be specified by the implementor for any particular implementation.

1.2 Datatypes

The diagram below shows the built-in [XML Schema Part 2: Datatypes]. Solid lines connect a base datatype above to a derived datatype below. Dashed lines connect a datatype created as a list of an item type above.

Type hierarchy graphic

Diagram courtesy Asir Vedamuthu, webMethods

1.3 Syntax

The purpose of this document is to catalog the functions and operators required for XPath 2.0, XML Query 1.0, and XSLT 2.0. The exact syntax used to invoke these functions and operators is specified in [XPath 2.0], [XQuery 1.0: An XML Query Language], and [XSLT 2.0].

In general, the above specifications do not support function overloading. Consequently, there are no overloaded functions in this document except for legacy [XPath 1.0] functions such as string() which takes a single argument of a variety of types and concat() which takes a variable number of string arguments. This does not apply to operators such as "+" which may be overloaded. Functions with optional arguments are allowed. If optional arguments are omitted, omissions are assumed to begin from the right.

1.4 Notations

This document defines, among other things, constructor functions and other functions that apply to one or more data types. Each function is defined by specifying its signature, a description of each of its arguments, and its semantics. For many functions, examples are included to illustrate their use.

Each function's signature is presented in a form like this:

fn:function-name($parameter-name as parameter-type, ...) as return-type

In this notation, function-name is the name of the function whose signature is being specified. If the function takes no parameters, then the name is followed by an empty set of parentheses: (); otherwise, the name is followed by a parenthesized list of parameter declarations, each declaration specifying the static type of the parameter and a non-normative name used to reference the parameter when the function's semantics are specified. If there are two or more parameter declarations, they are separated by a comma. The return-type specifies the static type of the value returned by the function. In most cases, the dynamic type returned by the function is the same as its static type. The few exceptions to this rule are documented in specific functions.

The function name is a QName as defined in [XML 1.0 Recommendation (Second Edition)] and must adhere to its syntactic conventions. Following [XPath 1.0], function names are composed of English words separated by hyphens,"-". If a function name contains a [XML Schema Part 2: Datatypes] datatype name, this may have intercapitalized spelling and is used in the function name as such. For example, fn:get-timezone-from-dateTime.

As is customary, the parameter type name indicates that the function accepts arguments of that type, or types derived from it, in that position. This is called subtype substitution. Details of the semantics of passing parameters to functions are discussed in Appendix B of [XQuery 1.0: An XML Query Language].

Some functions accept the empty sequence as an argument and some may return the empty sequence. This is indicated in the function signature by following the parameter type name with a question mark:

fn:function-name($parameter-name as parameter-type?) as return-type?

1.5 Namespace Prefix

The functions and operators discussed in this document are contained in three namespaces (see [Namespaces in XML]) and referenced using a QName. Constructor functions for the built-in datatypes defined in [XML Schema Part 2: Datatypes] discussed in 4 Constructor Functions are in the XML Schema namespace and named in this document using the xs: prefix. The namespace prefix used in this document is fn: for the user functions and op: for the operator functions. The namespace prefix for these functions can vary, as long as the prefix is bound to the correct URI.

The URIs of the namespaces are:

  • http://www.w3.org/2001/XMLSchema-datatypes for constructors

  • http://www.w3.org/2002/11/xquery-operators for operators

  • http://www.w3.org/2002/11/xquery-functions for functions.

The functions defined with an fn: prefix are callable by the user. Functions defined with the op: prefix are described here to underpin the definitions of the operators in [XPath 2.0], [XQuery 1.0: An XML Query Language], and [XSLT 2.0]. These functions are not available directly to users, and there is no requirement that implementations should actually provide these functions. For example, multiplication is generally associated with the * operator, but it is described as a function in this document. For example:

op:multiply($operand1 as numeric, $operand2 as numeric) as numeric

2 Accessors

The [XQuery 1.0 and XPath 2.0 Data Model] describes accessors on different types of nodes and defines their semantics. Some of these accessors are exposed to the user through the functions described below.

FunctionAccessorAcceptsReturns
fn:node-kind node-kind any kind of nodestring
fn:node-name name any kind of nodezero or one QName
fn:string string-value itemstring
fn:data typed-value any kind of nodea sequence of atomic values
fn:base-uri base-uri Element or Document node or no argumentzero or one anyURI
fn:unique-ID unique-ID Element nodezero or one ID

2.1 fn:node-kind

fn:node-kind($srcval as node) as string

This function returns a string value representing the node's kind: either "document", "element", "attribute", "text", "namespace", "processing-instruction", or "comment".

2.2 fn:node-name

fn:node-name($srcval as node) as QName?

This function returns an expanded-QName for node kinds that can have names. For other node kinds, it returns the empty sequence. Expanded-QName is defined in [XQuery 1.0 and XPath 2.0 Data Model], and consists of a namespace URI and a local name.

2.3 fn:string

fn:string() as string
fn:string($srcval as item?) as string

Returns the value of $srcval represented as a string. If no argument is supplied, $srcval defaults to the context item (.).

If $srcval is the empty sequence, the zero-length string is returned.

If $srcval is a node, the function returns the string value of the node, as obtained using the string-value accessor defined in the [XQuery 1.0 and XPath 2.0 Data Model].

If $srcval is an atomic value, then the function returns the same string as is returned by the expression cast as xs:string ($srcval), except in the cases listed below:

  • If the type of $srcval is xs:anyURI, the URI is converted to a string without any escaping of special characters.

NOTE: The reason for the special rule for xs:anyURI is that, although XML Schema strongly discourages the use of spaces within URI values, the escaping of spaces can cause problems with legacy applications (for example, this applies to spaces within fragment identifiers in many HTML browsers), and should therefore be under user control.

NOTE: The string representation of double values is not backwards-compatible with the representation of number values in [XPath 1.0]. Ordinary double values are now represented using scientific notation; the representations of positive and negative infinity are now 'INF' and '-INF' rather than 'Infinity' and '-Infinity'. (It should be observed that '+INF' is not supported as a lexical form of infinity in [XML Schema Part 2: Datatypes] and is thus not supported by this specification; if that lexical form is added in a future version of [XML Schema Part 2: Datatypes], then it will be supported by a future version of this specification that aligns with that future version of [XML Schema Part 2: Datatypes].) However, most expressions that would have produced a number in [XPath 1.0] will produce a decimal (or integer) in [XPath 2.0], so unless there is a loss of precision caused by numeric approximation, the result of the expression will in most simple cases be the same after conversion to a string.

2.4 fn:data

fn:data($srcval as node) as atomic value*

If $srcval is a text node, an element node, or an attribute node, fn:data returns the typed value of $srcval, as defined by the accessor function dm:typed-value defined for that kind of node in [XQuery 1.0 and XPath 2.0 Data Model].

Specifically:

If $srcval is a text node, then its typed value is equal to its string value, as an instance of xs:anySimpleType.

If $srcval is an attribute node with type annotation xs:anySimpleType, then its typed value is equal to its string value, as an instance of xs:anySimpleType. The typed value of any other attribute node is derived from its string value and type annotation in a way that is consistent with XML Schema validation, as described in [XQuery 1.0 and XPath 2.0 Data Model].

If $srcval is an element node with type annotation xs:anyType, then its typed value is equal to its string value, as an instance of xs:anySimpleType. The typed value of an element node with a type annotation other than xs:anyType is derived from its string value and type annotation in a way that is consistent with XML Schema validation, as described in [XQuery 1.0 and XPath 2.0 Data Model]. If $srcval is an element node whose type annotation denotes a type with complex content (i.e., a type that permits subelements), fn:data raises an error (Node has complex content).

If $srcval is not a text node, an attribute node, or an element node, then fn:data causes a type error.

2.5 fn:base-uri

fn:base-uri($srcval as node) as anyURI?

For document and element nodes, this function returns the value of the base-uri property. For other kinds of node, it returns the empty sequence.

fn:base-uri() as anyURI?

This version of the function returns the base-uri from the static context.

2.6 fn:unique-ID

fn:unique-ID($srcval as node) as ID?

This function accepts an element node and returns the identifier (ID) which may have been assigned by the user. It corresponds to the normalized value property of the attribute information item in the attributes property that has a type ID, if one exists. If no ID attribute exists the empty sequence is returned.

3 The Error Function

In this document, as well as in [XQuery 1.0: An XML Query Language], [XPath 2.0],and [XQuery 1.0 and XPath 2.0 Formal Semantics], the phrase "an error is raised" is used whenever the semantics being described encounter an error other than a static type error. The occurrence of that phrase implicitly causes the invocation of the fn:error function defined in this section. Whenever the raising of an error is accompanied by a specific error, the phrase "an error is raised (name-of-error)" is used, and the value name-of-error is passed as an argument to the fn:error function invocation.

The fn:error function may also be invoked from XQuery and XPath 2.0 applications.

fn:error() as none
fn:error($srcval as item?) as none

The fn:error function accepts any item (e.g., an atomic value or an element) as an argument. An alternate version of the function takes no arguments. The fn:error function never returns a value.

Note that "none" is a special type defined in the [XQuery 1.0 and XPath 2.0 Formal Semantics] and is not available to the user. It indicates that the function never returns and ensures that it has the correct static type.

[Issue 181: What are the semantics of fn:error?]

3.1 Examples

  • fn:error()

  • fn:error("Invalid argument")

  • fn:error(<a>Really <emph>dumb</emph> decision!</a>)

4 Constructor Functions

4.1 Constructor Functions for XML Schema Built-in Types

Every built-in type that is defined in [XML Schema Part 2: Datatypes], except xs:QName, as well as each of the two derived types fn:yearMonthDuration and fn:dayTimeDuration defined in this specification, has an associated constructor function. The form of that function for a type TYP is:

xs:TYP($srcval as item) as TYP

For example, the signature of the constructor function corresponding to the unsignedInt type defined in [XML Schema Part 2: Datatypes] is:

xs:unsignedInt($srcval as item) as unsignedInt

Invoking the constructor function xs:unsignedInt(12) returns the unsignedInt value 12. Another invocation of that constructor function that returns the same unsignedInt value is xs:unsignedInt("12").

If the argument to a constructor function is a string literal, the literal must be a valid lexical form for its type, as specified in [XML Schema Part 2: Datatypes] and the semantics of the function are identical to XML Schema validation. Whitespace normalization is applied before validation as indicated by the value of the whitespace facet for the datatype.

If the argument to a constructor function is a literal, the result of the function may be evaluated statically; if an error is found during such evaluation, it may be reported as a static error.

The semantics of the constructor function xs:TYP(item) are identical to the semantics of "cast as xs:TYP (item)". See 16 Casting Functions

The following constructor functions for the built-in types are supported:

  • xs:string($srcval as item) as string
  • xs:boolean($srcval as item) as boolean
  • xs:decimal($srcval as item) as decimal
  • xs:float($srcval as item) as float
  • xs:double($srcval as item) as double
  • xs:duration($srcval as item) as duration
  • xs:dateTime($srcval as item) as dateTime
  • xs:time($srcval as item) as time
  • xs:date($srcval as item) as date
  • xs:gYearMonth($srcval as item) as gYearMonth
  • xs:gYear($srcval as item) as gYear
  • xs:gMonthDay($srcval as item) as gMonthDay
  • xs:gDay($srcval as item) as gDay
  • xs:gMonth($srcval as item) as gMonth
  • xs:hexBinary($srcval as item) as hexBinary
  • xs:base64Binary($srcval as item) as base64Binary
  • xs:anyURI($srcval as item) as anyURI
  • xs:NOTATION($srcval as item) as NOTATION
  • xs:normalizedString($srcval as item) as normalizedString
  • xs:token($srcval as item) as token
  • xs:language($srcval as item) as language
  • xs:NMTOKEN($srcval as item) as NMTOKEN
  • xs:NMTOKENS($srcval as item) as NMTOKENS
  • xs:Name($srcval as item) as Name
  • xs:NCName($srcval as item) as NCName
  • xs:ID($srcval as item) as ID
  • xs:IDREF($srcval as item) as IDREF
  • xs:IDREFS($srcval as item) as IDREFS
  • xs:ENTITY($srcval as item) as ENTITY
  • xs:ENTITIES($srcval as item) as ENTITIES
  • xs:integer($srcval as item) as integer
  • xs:nonPositiveInteger($srcval as item) as nonPositiveInteger
  • xs:negativeInteger($srcval as item) as negativeInteger
  • xs:long($srcval as item) as long
  • xs:int($srcval as item) as int
  • xs:short($srcval as item) as short
  • xs:byte($srcval as item) as byte
  • xs:nonNegativeInteger($srcval as item) as nonNegativeInteger
  • xs:unsignedLong($srcval as item) as unsignedLong
  • xs:unsignedInt($srcval as item) as unsignedInt
  • xs:unsignedShort($srcval as item) as unsignedShort
  • xs:unsignedByte($srcval as item) as unsignedByte
  • xs:positiveInteger($srcval as item) as positiveInteger
  • fn:yearMonthDuration($srcval as item) as yearMonthDuration
  • fn:dayTimeDuration($srcval as item) as dayTimeDuration

4.2 Constructor Functions for User-Defined Types

For every type defined in the static query context (See [XQuery 1.0: An XML Query Language] that is derived by restriction from a primitive type, there is a construction function (whose name is the same as the type name), whose effect is to create a value of that type from the supplied argument. The rules are defined in the same way as for built-in derived types 4.1 Constructor Functions for XML Schema Built-in Types.

Consider a situation where the static context contains a type called hatSize defined in a schema that is bound to the prefix my. In such a case the constructor function:

my:hatSize($srcval as item) as hatSize

is available to users.

5 Functions and Operators on Numerics

This section discusses arithmetic operators on the numeric datatypes defined in [XML Schema Part 2: Datatypes]. It uses an approach that permits lightweight operations whenever possible.

5.1 Numeric Types

The operators described in this section are defined on the following numeric types. Each type whose name is indented is derived from the type whose name appears nearest above with one less level of indent.

decimal
integer
float
double

They also apply to types derived by restriction from these types.

5.2 Operators on Numeric Values

The following functions are defined to back up operators defined in [XQuery 1.0: An XML Query Language] and [XPath 2.0] on these numeric types.

OperatorsMeaningSource
op:numeric-add AdditionXPath 1.0
op:numeric-subtract SubtractionXPath 1.0
op:numeric-multiply MultiplicationXPath 1.0
op:numeric-divide DivisionXPath 1.0
op:numeric-integer-divide Integer divisionXPath 1.0
op:numeric-mod ModulusXPath 1.0
op:numeric-unary-plus Unary plusXPath 2.0 Req 1.7 Should
op:numeric-unary-minus Unary minus (negation)XPath 1.0

The arguments and return types for the arithmetic operators are the basic numeric types: integer, decimal, float, and double, and types derived from them. For simplicity, each operator is defined to operate on operands of the same datatype and to return the same datatype. (The one exception is op:numeric-divide, which returns a double if called with two integer operands.) If the two operands are not of the same type, subtype substitution and type promotion may be used to obtain two operands of the same type. Appendix B of [XQuery 1.0: An XML Query Language] describes the semantics of these operations in detail.

  1. Subtype substitution: A derived type may substitute for its base type. In particular, integer may be used where decimal is expected.

  2. Type promotion: decimal may be promoted to float, and float may be promoted to double.

The result type of operations depends on their argument datatypes and is defined in the following table:

OperatorReturns
op:operation(integer, integer) integer (except for op:numeric-divide(integer, integer), which returns a double)
op:operation(decimal, decimal) decimal
op:operation(float, float) float
op:operation(double, double) double
op:operation(integer) integer
op:operation(decimal) decimal
op:operation(float) float
op:operation(double) double

These rules define any operation on any pair of arithmetic types. Consider the following example:

op:operation(int, double) => op:operation(double, double)

For this operation, int must be converted to double. This can be done, since by the rules above: int can be substitutued for integer, integer can be promoted to decimal, decimal can be promoted to float, and float can be promoted to double. As far as possible, the promotions should be done in a single step. Specifically, when a decimal is promoted to a double, it must not be converted to a float and then to double as this will lose precision.

As another example, a user may define height as a derived type of integer with a minimum value of 20 and a maximum value of 100. He may then derive oddHeight using a pattern to restrict the value to odd integers.

op:operation(oddHeight, integer) => op:operation(integer, integer)

oddHeight can be substituted for its base type height and height can be substitutued for its base type integer.

[Issue 177: Must overflow and underflow always be reported?]

Overflow and underflow behavior is ·implementation-defined·. See [ISO 10967]. That is, implementations may determine that, when overflow or underflow is detected in any of the above operations, an error is raised ("overflow or underflow error"). However, implementations are not required to catch or report such errors.

Finally, consider some examples involving special IEEE 754 numerics.

  1. If either argument is "NaN", the result is "NaN".

  2. If neither argument is "NaN", but either argument is "INF", the result is "INF".

  3. If neither argument is "NaN" or "INF", but either argument is "-INF", the result is "-INF".

Note that in some cases such as subtraction, multiplication and division, "INF" may become "-INF", and vice versa, as appropriate.

The functions op:numeric-add, op:numeric-subtract, op:numeric-multiply, op:numeric-divide, op:numeric-integer-divide, and op:numeric-mod are each defined for pairs of numeric operands, each of which has the same type: integer, decimal, float, or double. The functions op:numeric-unary-plus and op:numeric-unary-minus are defined for a single operand whose type is one of those same numeric types.

5.2.1 op:numeric-add

op:numeric-add($operand1 as numeric, $operand2 as numeric) as numeric

Backs up the "+" operator and returns the arithmetic sum of its operands: ($operand1 + $operand2).

5.2.2 op:numeric-subtract

op:numeric-subtract($operand1 as numeric, $operand2 as numeric) as numeric

Backs up the "-" operator and returns the arithmetic difference of its operands: ($operand1 - $operand2).

5.2.3 op:numeric-multiply

op:numeric-multiply($operand1 as numeric, $operand2 as numeric) as numeric

Backs up the "*" operator and returns the arithmetic product of its operands: ($operand1 * $operand2).

5.2.4 op:numeric-divide

op:numeric-divide($operand1 as numeric, $operand2 as numeric) as numeric

Backs up the "div" operator and returns the arithmetic quotient of its operands: ($operand1 div $operand2).

Note:

For compatibility with [XPath 1.0], if the types of both $operand1 and $operand2 are xs:integer, then the return type is xs:double.

For xs:decimal and xs:integer operands, if the divisor is 0, then an error is raised ("Division by zero"). For xs:float and xs:double operands, performs floating point division as specified in [IEEE 754-1985].

5.2.5 op:numeric-integer-divide

op:numeric-integer-divide($operand1 as integer,
$operand2 as integer) as integer

Backs up the "idiv" operator and returns the arithmetic quotient of its operands: ($operand1 idiv $operand2). If the numerator is not evenly divided by the divisor, then the quotient is the integer value obtained, ignoring any remainder that results from the division (that is, no rounding is performed).

If the divisor is 0, then an error is raised ("Division by zero").

5.2.6 op:numeric-mod

op:numeric-mod($operand1 as numeric, $operand2 as numeric) as numeric

Backs up the "mod" operator and returns the remainder after dividing the first operand by the second operand: ($operand1 mod $operand2). The result is of the same type as the operands after type promotion. The following rules apply:

  • For xs:decimal and xs:integer operands, if the divisor is 0, then an error is raised ("Division by zero").

  • For xs:float and xs:double operands:

    • If either operand is NaN, the result is NaN.

    • If the dividend is positive or negative infinity, or the divisor is positive or negative zero (0), or both, the result is NaN.

    • If not NaN, the sign of the result equals the sign of the dividend.

    • If the dividend is finite and the divisor is an infinity, the result equals the dividend.

    • If the dividend is positive or negative zero and the divisor is finite, the result is the same as the dividend.

    • In the remaining cases, where neither positive or negative infinity, nor positive or negative zero, nor NaN is involved, the float or double remainder r from a dividend n and a divisor d is defined by the mathematical relation r = n-(d * q) where q is an integer that is negative only if n/d is negative and positive only if n/d is positive, and whose magnitude is as large as possible without exceeding the magnitude of the true mathematical quotient of n and d. This is truncating division, analogous to integer division, not [IEEE 754-1985] rounding division.

5.2.6.1 Examples
  • op:numeric-mod(10,3) returns 1.

  • op:numeric-mod(6,2) returns 0.

  • op:numeric-mod(4.5,1.2) returns 0.9.

  • op:numeric-mod(1.23E2, 0.6E1) returns 3.0E0.

5.2.7 op:numeric-unary-plus

op:numeric-unary-plus($operand as numeric) as numeric

Backs up the unary "+" operator and returns its operand with the sign unchanged: (+ $operand). Semantically, this operation performs no operation.

5.2.8 op:numeric-unary-minus

op:numeric-unary-minus($operand as numeric) as numeric

Backs up the unary "-" operator and returns its operand with the sign reversed: (- $operand). If $operand is positive, its negative is returned; if it is negative, its positive is returned.

5.3 Comparison of Numeric Values

We define the following comparison operators on numeric values. Comparisons take two arguments of the same type. If the arguments are of different types, one argument is promoted to the type of the other. Each comparison operator returns a boolean value. If either, or both, operands are "NaN", false is returned.

OperatorMeaningSource
op:numeric-equal Equality comparisonXPath 1.0
op:numeric-less-than Less-than comparisonXPath 1.0
op:numeric-greater-than Greater-than comparisonXPath 1.0

5.3.1 op:numeric-equal

op:numeric-equal($operand1 as numeric, $operand2 as numeric) as boolean

Returns true if and only if $operand1 is exactly equal to $operand2. For xs:float and xs:double values, 0 (zero), +0 (positive zero), and -0 (negative zero) all compare equal. NaN does not equal itself.

This function backs up the "eq" and "ne" operators on numeric values.

5.3.2 op:numeric-less-than

op:numeric-less-than($operand1 as numeric, $operand2 as numeric) as boolean

Returns true if and only if $operand1 is less than $operand2. For xs:float and xs:double values, positive infinity is greater than all other non-NaN values; negative infinity is less than all other non-NaN values. NaN is not comparable with (neither greater than nor less than) any other value including itself.

This function backs up the "lt" and "ge" operators on numeric values.

5.3.3 op:numeric-greater-than

op:numeric-greater-than($operand1 as numeric, $operand2 as numeric) as boolean

Returns true if and only if $operand1 is greater than $operand2. For xs:float and xs:double values, positive infinity is greater than all other non-NaN values; negative infinity is less than all other non-NaN values. NaN is not comparable with (neither greater than nor less than) any other value including itself.

This function backs up the "gt" and "le" operators on numeric values.

5.4 Functions on Numeric Values

The following functions are defined on these numeric types. Each function returns an integer except:

  • If the argument is the empty sequence, the empty sequence is returned.

  • If the argument is "NaN", "NaN" is returned.

  • If the argument is positive or negative infinity, positive or negative infinity is returned.

FunctionMeaningSource
fn:floor Returns the largest integer less than or equal to the argumentXPath 1.0
fn:ceiling Returns the smallest integer greater than or equal to the argumentXPath 1.0
fn:round Rounds to the nearest integerXPath 1.0

[Issue 79: How many digits of precision (etc.) are returned from certain functions?]

[Issue 142: Should floor ceiling and round return the same type as their argument? ]

[Issue 179: What is the appropriate return type for fn:floor, fn:celing, and fn:round?]

5.4.1 fn:floor

fn:floor($srcval as double?) as double?

Returns the largest (closest to positive infinity) integer that is not greater than the value of $srcval. If the argument is the empty sequence, returns the empty sequence.

5.4.1.1 Examples
  • fn:floor(10.5) returns 10.

  • fn:floor(-10.5) returns -11.

5.4.2 fn:ceiling

fn:ceiling($srcval as double?) as double?

Returns the smallest (closest to negative infinity) integer that is not smaller than the value of $srcval. If the argument is the empty sequence, returns the empty sequence.

5.4.2.1 Examples
  • fn:ceiling(10.5) returns 11.

  • fn:ceiling(-10.5) returns -10.

5.4.3 fn:round

fn:round($srcval as double?) as double?

Returns the number that is closest to the argument. If there are two such numbers, then the one that is closest to positive infinity is returned. More formally, fn:round(x) produces the same result as fn:floor(x+0.5). If the argument is NaN, then NaN is returned. If the argument is positive infinity, then positive infinity is returned. If the argument is negative infinity, then negative infinity is returned. If the argument is positive zero (+0), then positive zero (+0) is returned. If the argument is negative zero (-0), then negative zero (-0) is returned. If the argument is less than zero (0), but greater than or equal to -0.5, then negative zero (-0) is returned. If the argument is the empty sequence, then the empty sequence is returned.

5.4.3.1 Examples
  • fn:round(2.5) returns 3.

  • fn:round(2.4999) returns 2.

  • fn:round(-2.5) returns -2 (not the possible alternative, -3).

6 Functions on Strings

This section discusses functions and operators on the [XML Schema Part 2: Datatypes] string datatype and the datatypes derived from string.

6.1 String Types

The operators described in this section are defined on the following string types. Each type whose name is indented is derived from the type whose name appears nearest above with one less level of indent.

string
normalizedString
token
language
NMTOKEN
Name
NCName
ID
IDREF
ENTITY

They also apply to user-defined types derived by restriction from these types.

NOTE: This document uses the term "code point" as a synonym for "Unicode scalar value". [The Unicode Standard] sometimes spells this term "codepoint". Code points range from #x0000 to #x10FFFF inclusive. The use of the word 'character' in this document is in the sense of production [2] of [XML 1.0 Recommendation (Second Edition)].

6.2 Functions to Assemble and Disassemble Strings

FunctionMeaning
fn:codepoints-to-string Creates a string from a sequence of codepoints.
fn:string-to-codepoints Returns the sequence of codepoints that constitute a string.

6.2.1 fn:codepoints-to-string

fn:codepoints-to-string($srcval as integer*) as string

Creates a string from a sequence of codepoints.

6.2.2 fn:string-to-codepoints

fn:string-to-codepoints($srcval as string) as integer*

Returns the sequence of codepoints that constitute a string.

6.3 Equality and Comparison of Strings

When values whose type is string or some type derived from string are compared (or, equivalently, sorted), the comparisons are inherently performed according to some collation (even if that collation is defined entirely on code point values or on the binary representations of the characters of the string). The [Character Model for the World Wide Web 1.0] observes that some applications may require different comparison and ordering behaviors than other applications. Similarly, some users having particular linguistic expectations may require different behaviors than other users. Consequently, the collation must be taken into account when comparing strings in any context. Several functions in this and the following section make use of a collation.

Collations can indicate that some characters that are rendered differently are, in fact equal for collation purpose (e.g., "uve" and "uwe" are considered equivalent in some European languages). Strings can be compared character-by-character or in a logical manner, as defined by the collation.

Some collations, especially those based on the [Unicode Collation Algorithm] can be "tailored" for various purposes. This document does not discuss such tailoring. Instead, it assumes that the collation argument to the various functions below is a tailored and named collation. A specific collation with a distinguished name, http://www.w3.org/2002/11/query-operators/collation/codepoint, provides the ability to compare strings based on code point values. Every implementation of XQuery must support the collation based on code point values.

While the [Character Model for the World Wide Web 1.0] recommends that all strings be subjected to early Unicode normalization, it is not possible to guarantee that all strings in all XML documents are, in fact, normalized, or that they are normalized in the same manner. In order to maximize interoperable results of operations on XML documents in general, there may be collations that operate on unnormalized strings, other collations that raise runtime errors when unnormalized strings are encountered, and still other collations that implicitly normalize strings for the purposes of collating them. For alignment with the [Character Model for the World Wide Web 1.0], applications may choose collations that treat unnormalized strings as though they were normalized (that is, that implicitly normalize the strings). Note that collations based on the Unicode collation algorithm produce equivalent results regardless of a string's normalization.

This document assumes that collations are named and that the collation name may be provided as an argument to string comparison functions. Functions that allow specification of a collation do so with an argument whose type is anyURI. This document also defines the manner in which a default collation is determined when the collation argument is not specified in invocations of the functions that allow it to be omitted.

The XQuery/XPath static context includes provision for a default collation that can be used for string comparisons (including ordering operations). However, the static context is not required to have a default collation specified; an implementation might choose to provide a default collation only under certain circumstances, or not at all. The static context default collation, if provided, is determined by ·implementation-defined· means. Such means might include determination from the host operating system environment, determination during XQuery/XPath installation, determination when the XQuery/XPath implementation was created, determination from the locale of some user environment, or even ·implementation-defined· language through which the user can specify that collation.

The decision of what collation to use for a given comparison or ordering operation is determined by the following algorithm:

  1. If the operation specifies an explicit collation CollationA (e.g., if the optional collation argument is specified in an invocation of the fn:compare() function), then:

    • If CollationA is supported by the implementation, then CollationA is used.

    • Otherwise, an error is raised ("Unsupported collation").

  2. If no collation is explicitly specified for the operation and the XQuery/XPath static context specifies a collation CollationB, then:

    • If CollationB is supported by the implementation, then CollationB is used.

    • Otherwise, an error is raised ("Unsupported collation").

    NOTE: There might be several ways in which a collation might be specified in the XQuery/XPath static context. For example, XQuery might provide syntax that specifies a default collation as part of the query prolog.

  3. Otherwise, the Unicode codepoint collation (http://www.w3.org/2002/11/query-operators/collation/codepoint) is used.

XML allows elements to specify the xml:lang attribute to indicate the language associated with the content of such an element. This specification does not use xml:lang to identify the default collation, in part because collations should be determined by the user of the data, not (normally) the data itself, and because using xml:lang does not produce desired effects when the two strings to be compared have different xml:lang values or when a string is multilingual.

NOTE: Some data management environments allow collations to be associated with the definition of string items (that is, with the metadata that describes items whose type is string). While such association may be appropriate for use in environments in which data is held in a repository tightly bound to its descriptive metadata, it is not appropriate in the XML environment in which different documents being processed by a single query may be described by differing schemas.

NOTE: all collations support the capability of deciding whether two strings are considered equal, and if not, which of the strings should be regarded as preceding the other. For functions such as fn:compare(), this is all that is required. For other functions, such as fn:contains() and fn:starts-with(), the collation needs to support an additional property: it must decompose the string into a sequence of units, each unit consisting of one or more characters, such that two strings can be compared by pairwise comparison of these units. The functions fn:contains(), fn:starts-with(), and fn:ends-with() first use the collation to decompose the string provided in the first argument into such a sequence of units, then examine this sequence to determine if there is a subsequence that matches the units obtained by decomposing the string supplied in the second argument. Substrings might not be matched if they start or end in the middle of a unit. For example, if a collation treats "Jaeger" and "Jäger" [ed note, second character is "a with umlaut"] as equal, then it is probably treating "ae" as one unit, which means that under this collation, the expression contains("Jaeger", "eg") is likely to return false.

It is possible to define collations that do not have this property, for example a collation that attempts to sort "ISO 8859" before "ISO 10646", or "January" before "February". Such collations may fail, or give unexpected results, when used with functions such as fn:contains().

[Issue 44: Collations: URIs and URI references or short names?]

FunctionMeaningSource
fn:compare Compares two character strings; a collation may optionally be specifiedXSLT 2.0, Req. 2.13 (Could)

[Issue 73: Is a "between" function needed?]

6.3.1 fn:compare

fn:compare($comparand1 as string?, $comparand2 as string?) as integer?
fn:compare($comparand1 as string?,
$comparand2 as string?,
$collationLiteral as anyURI) as integer?

Returns -1, 0, or 1, depending on whether the value of the $comparand1 is respectively less than, equal to, or greater than the value of $comparand2, according to the rules of the collation that is used.

The collation used by the invocation of this function is determined according to the rules in 6.3 Equality and Comparison of Strings.

If the value of $comparand2 begins with a string that is equal to the value of $comparand1 (according to the collation that is used) and has additional characters following that beginning string, then the result is -1. If the value of $comparand1 begins with a string that is equal to the value of $comparand2 (according to the collation that is used) and has additional characters following that beginning string, then the result is 1.

If either argument is the empty sequence, the result is the empty sequence.

This function backs up the "eq", "ne", "gt", "lt", "le" and "ge" operators on string values.

6.3.1.1 Examples
  • fn:compare('abc', 'abc') returns 0.

  • fn:compare('Strasse', 'Straße') returns 0 if and only if the default collation includes provisions that equate "ss" and the (German) character "ß" ("sharp-s"). (Otherwise, the returned value depends on the semantics of the default collation.)

  • fn:compare('Strasse', 'Straße', anyURI('deutsch')) returns 0 if and only if the collation identified by the relative URI constructed from the string value "deutsch" includes provisions that equate "ss" and the (German) character "ß" ("sharp-s"). (Otherwise, the returned value depends on the semantics of that collation.)

  • fn:compare('Strassen', 'Straße') returns 1 if and only if the default collation includes provisions that equate "ss" and the (German) character "ß" ("sharp-s"). (Since the value of $comparand1 has an additional character, an "n", following the string that is equal to "Straße", it is greater than the value of $comparand2.)

6.4 Functions on String Values

The following functions are defined on these string types. Several of these function use a collation. See 6.3 Equality and Comparison of Strings for a discussion of collations.

FunctionMeaningSource
fn:concat Concatenates two or more character strings. XPath 1.0
fn:string-join Accepts a sequence of strings and returns the strings concatenated together with an optional separator. XPath 1.0
fn:starts-with Indicates whether the value of one string begins with the characters of the value of another string. XPath 1.0
fn:ends-with Indicates whether the value of one string ends with the characters of the value of another string. XPath 1.0
fn:contains Indicates whether the value of one string contains the characters of the value of another string. A collation may optionally be specified. XPath 1.0
fn:substring Returns a string located at a specified place in the value of a string. XPath 1.0
fn:string-length Returns the length of the argument. XPath 1.0
fn:substring-before Returns the characters of one string that precede in that string the characters in the value of another string. A collation may optionally be specified. XPath 1.0
fn:substring-after Returns the characters of one string that precede in that string the characters in the value of another string. A collation may optionally be specified. XPath 1.0
fn:normalize-space Returns the whitespace-normalized value of the argument.XPath 1.0
fn:normalize-unicode Returns the normalized value of the first argument in the normalization form specified by the second argument.XPath 2.0 Req 2.9 (Should)
fn:upper-case Returns the upper-cased value of the argument. XPath 2.0 Req 2.4.3 (Should)
fn:lower-case Returns the lower-cased value of the argument. XPath 2.0 Req 2.4.3 (Should)
fn:translate Returns the first argument string with occurrences of characters in the second argument replaced by the character at the corresponding position in the third string. XPath 1.0
fn:string-pad Returns a string composed of as many copies of its first argument as specified in its second argument.XPath 2.0 Req 2.4.2, 4.4 (Should)
fn:matches Returns a boolean value that indicates whether the value of the first argument is matched by the regular expression that is the value of the second argument. XPath 2.0 Req 3. (Must)
fn:replace Returns the value of the first argument with every substring matched by the regular expression that is the value of the second argument replaced by the replacement string that is the value of the third argument. XPath 2.0 Req 2.4.1. (Should)
fn:tokenize Returns a sequence of zero or more strings whose values are substrings of the value of the first argument separated by substrings that match the regular expression that is the value of the second argument. XSLT 2.0 Req (Should)
fn:escape-uri Returns the string representing a URI value with certain characters escaped as specified in [RFC 2396] and [RFC 2732].

[Issue 21: What is the precise type returned by each function?]

Note also that when the above operators and functions are applied to datatypes derived from string, they are guaranteeed to return legal strings, but they may not return legal value for the particular subtype to which they were applied.

6.4.1 fn:concat

fn:concat() as string
fn:concat($op1 as string?) as string
fn:concat($op1 as string?, $op2 as string?, ...) as string

Accepts zero or more strings as arguments. Returns the string that is the concatenation of the values of its arguments. The resulting string might not be normalized in any Unicode or W3C normalization. If called with no arguments, returns the zero-length string. If any of the arguments is the empty sequence, it is treated as the zero-length string.

The concat() function is specified to allow an arbitrary number of string arguments that are concatenated together. This capability is retained for compatibility with [XPath 1.0] and is the only function specified in this document that has that characteristic.

[Issue 144: Should the concat function accept sequences as arguments?]

6.4.1.1 Examples
  • fn:concat('abc', 'def') returns " abcdef ".

  • fn:concat('abc') returns " abc ".

  • fn:concat('abc', 'def', 'ghi', 'jkl', 'mno') returns " abcdefghijklmno ".

  • fn:concat(()) returns "".

6.4.2 fn:string-join

fn:string-join($operand1 as string*, $operand2 as string*) as string

Returns a (possibly empty) string created by concatenating the members of the $operand1 sequence using $operand2 as a separator. If the value of $operand2 is the zero-length string, then the members of $operand1 are concatenated without a separator.

If the value of $operand1 is the empty sequence, the empty string is returned.

6.4.2.1 Examples
  • fn:string-join({'Now', 'is', 'the', 'time', '...'}, " ") returns " Now is the time ... ".

  • fn:string-join({abra, cadabra}, "") returns " abracadabra ".

  • fn:string-join((), "separator") returns " ".

6.4.3 fn:starts-with

fn:starts-with($operand1 as string?, $operand2 as string?) as boolean?
fn:starts-with($operand1 as string?,
$operand2 as string?,
$collationLiteral as anyURI) as boolean?

Returns a boolean indicating whether or not the value of $operand1 starts with a string that is equal to the value of $operand2 according to the collation that is used.

If the value of $operand2 is the zero-length string, then the function returns true. If the value of $operand1 is the zero-length string and the value of $operand2 is not the zero-length string, then the function returns false.

If the value of $operand1 or $operand2 is the empty sequence, the empty sequence is returned.

The collation used by the invocation of this function is determined according to the rules in 6.3 Equality and Comparison of Strings.

6.4.3.1 Examples
  • fn:starts-with("goldenrod", "gold") returns true.

  • fn:starts-with("goldenrod", "") returns true.

  • fn:starts-with("goldenrod", "rod") returns false.

6.4.4 fn:ends-with

fn:ends-with($operand1