XML technologies / XSLT / XSLT and XPath functions / XPath 2.0 constructor functions

(Excerpt from "XSLT 2.0 & XPath 2.0" by Frank Bongers, chapter 5.)

XPath 2.0 constructor functions create typed data corresponding to types defined in XML Schema. This description uses the working draft of XML Schema 1.1 Part 2: Datatypes dated 17 February 2006:

Constructors use prefix xs, which refers to the XML Schema namespace xmlns:xs="http://www.w3.org/2001/XMLSchema".

A constructor resembles a function call and has the following formal structure:

prefix:ZIELTYP($srcval as xs:anyAtomicType) as pref:ZIELTYP

The input value $srcval, usually an XPath expression, is generalized here as xs:anyAtomicType. This means that the evaluated argument must already be atomic or at least capable of atomization.

The argument can therefore be a string, number or Boolean, or be obtained by atomizing a node to extract its value. If atomization fails, an error is reported.

Examples:

Constructor for type xs:string:

xs:string($srcval as xs:anyAtomicType)                       as xs:string

Constructor for type xs:yearMonthDuration:

xs:yearMonthDuration($srcval as xs:anyAtomicType)                       as xs:yearMonthDuration

Constructors use implicit type conversion. A string input must satisfy the lexical requirements of the desired result type.

The following constructor calls therefore have identical results:

xs:Integer("15") and xs:Integer(15)

The atomic input is a string in the first case and a number in the second. Both return the number 15 typed as xs:integer.

Constructors for XML Schema data types

Constructors exist for all the following data types. They return any atomic or atomizable input as the desired data type.

  • Numeric types (numeric):
    xs:byte, xs:decimal, xs:integer, xs:float, xs:double, xs:nonPositiveInteger, xs:negativeInteger, xs:nonNega­tiveInteger, xs:unsignedLong, xs:unsignedInt, xs:positiveInteger, xs:short, xs:unsignedShort

  • Boolean data type:
    xs:boolean

  • String data types:
    xs:string, xs:normalizedString, xs:token, xs:language, xs:Name

  • XML data types, also string types:
    xs:ENTITY, xs:ID, xs:IDREF, xs:NCName, xs:NOTATION, xs:NMTOKEN

  • Date, time and duration types (XML Schema):
    xs:date, xs:dateTime, xs:dayTimeDuration, xs:duration, xs:yearMonthDuration, xs:time,

  • Gregorian-calendar date types:
    xs:gYearMonth, xs:gYear, xs:gMonthDay, xs:gMonth, xs:gDay

  • Data-format types, hexadecimally or binary encoded:
    xs:hex­Binary, xs:base64Binary

  • URI data formats
    xs:anyURI

No constructor functions exist for the general, formally defined base types xs:anyType and xs:anySimpleType or for the general type originally introduced in XPath 2.0, xs:anyAtomicType.

The types xs:ENTITIES, xs:NMTOKENS and xs:IDREFS also have no constructor functions of their own. Use the individual constructors for xs:ENTITY, xs:NMTOKEN and xs:IDREF as needed.

The XML Schema data types xs:yearMonthDuration and xs:dayTime­Duration and xs:untypedAtomic have their own constructors.

Note: no more xdt data types
The three data types originally defined in the XPath 2.0 working draft, xdt:yearMonthDura­tion, xdt:dayTimeDuration and xdt:untypedAtomic, were removed and replaced by schema types xs:yearMonthDura­tion, xs:dayTimeDuration and the new base type xs:untypedAtomic. The prefix xdt referred to the now-obsolete namespace "http://www.w3.org/2003/11/xpath-datatypes".

Constructors for user-defined data types

If an XML Schema defines a user type derived from a primitive type, a corresponding constructor function exists automatically. It has the same name and namespace as that user-defined type. Here, the custom type represents a catalog number:

meine:katNummer($srcval as xs:anyAtomicType)              as meine:katNummer

   

back to top