Please check the errata for any errors or issues reported since publication.
See also translations.
This document is also available in these non-normative formats: Specification in XML format and XML function catalog.
Copyright © 2000 W3C® (MIT, ERCIM, Keio, Beihang). W3C liability, trademark and document use rules apply.
This document defines constructor functions, operators, and functions on the datatypes defined in [XML Schema Part 2: Datatypes Second Edition] and the datatypes defined in [XQuery and XPath Data Model (XDM) 3.1]. It also defines functions and operators on nodes and node sequences as defined in the [XQuery and XPath Data Model (XDM) 3.1]. These functions and operators are defined for use in [XML Path Language (XPath) 4.0] and [XQuery 4.0: An XML Query Language] and [XSL Transformations (XSLT) Version 4.0] and other related XML standards. The signatures and summaries of functions defined in this document are available at: http://www.w3.org/2005/xpath-functions/.
A summary of changes since version 3.1 is provided at G Changes since 3.1.
This version of the specification is work in progress. It is produced by the QT4 Working Group, officially the W3C XSLT 4.0 Extensions Community Group. Individual functions specified in the document may be at different stages of review, reflected in their History notes. Comments are invited, in the form of GitHub issues at https://github.com/qt4cg/qtspecs.
The publications of this community group are dedicated to our co-chair, Michael Sperberg-McQueen (1954–2024).
A sequence is an ordered collection of zero or more items. An item is a node, an atomic item, or a function, such as a map or an array. The terms sequence and item are defined formally in [XQuery 4.0: An XML Query Language] and [XML Path Language (XPath) 4.0].
The functions in this section provide access to resources (such as files) in the external environment.
| Function | Meaning |
|---|---|
fn:doc | Retrieves a document using a URI supplied as an xs:string, and returns the corresponding document node. |
fn:doc-available | The function returns true if and only if the function call fn:doc($source, $options) would return a document node. |
fn:collection | Returns a sequence of items identified by a collection URI; or a default collection if no URI is supplied. |
fn:uri-collection | Returns a sequence of xs:anyURI values representing the URIs in a URI collection. |
fn:unparsed-text | The fn:unparsed-text function reads an external resource (for example, a file) and returns a string representation of the resource. |
fn:unparsed-text-lines | The fn:unparsed-text-lines function reads an external resource (for example, a file) and returns its contents as a sequence of strings, one for each line of text in the string representation of the resource. |
fn:unparsed-text-available | Allows an application to determine whether a call on fn:unparsed-text with particular arguments would succeed. |
fn:unparsed-binary | The fn:unparsed-binary function reads an external resource (for example, a file) and returns its contents in binary. |
fn:environment-variable | Returns the value of a system environment variable, if it exists. |
fn:available-environment-variables | Returns a list of environment variable names that are suitable for passing to fn:environment-variable, as a (possibly empty) sequence of strings. |
The fn:unparsed-binary function reads an external resource (for example, a file) and returns its contents in binary.
fn:unparsed-binary( | ||
$source | as | |
) as | ||
This function is deterministic, context-dependent, and focus-independent. It depends on executable base URI.
The $source argument must be a string in the form of a URI reference, which must contain no fragment identifier, and must identify a resource for which a binary representation is available.
If $source is a relative URI reference, it is resolved relative to the value of the executable base URIXP property from the dynamic context of the caller. The resulting absolute URI is promoted to an xs:string.
The mapping of URIs to the binary representation of a resource is the mapping defined in the available binary resourcesXP component of the dynamic context.
If the $source argument is an empty sequence, the function returns an empty sequence.
The result of the function is an atomic item of type xs:base64Binary containing the binary representation of the resource retrieved using the URI.
A dynamic error is raised [err:FOUT1170] if the $source argument contains a fragment identifier, or if it cannot be resolved to an absolute URI (for example, because the base-URI property in the static context is absent), or if it cannot be used to retrieve the binary representation of a resource.
If it is appropriate to use a base URI other than the executable base URIXP (for example, when resolving a relative URI reference read from a source document) then it is advisable to resolve the relative URI reference using the fn:resolve-uri function before passing it to the fn:unparsed-text function.
There is no essential relationship between the sets of URIs accepted by the function fn:binary-resourcefn:unparsed-binary and other functions such as fn:doc and fn:unparsed-text (a URI accepted by one may or may not be accepted by the others), and if a URI is accepted by more than one of these functions then there is no essential relationship between the results (different resource representations are permitted by the architecture of the web).
There are no constraints on the MIME type of the resource.
The fact that the resolution of URIs is defined by a mapping in the dynamic context means that in effect, various aspects of the behavior of this function are implementation-defined. Implementations may provide external configuration options that allow any aspect of the processing to be controlled by the user. In particular:
The set of URI schemes that the implementation recognizes is implementation-defined. Implementations may allow the mapping of URIs to resources to be configured by the user, using mechanisms such as catalogs or user-written URI handlers.
The handling of media types is implementation-defined.
Implementations may provide user options that relax the requirement for the function to return deterministic results.
Implementations may provide user-defined error handling options that allow processing to continue following an error in retrieving a resource, or in reading its content. When errors have been handled in this way, the function may return a fallback document provided by the error handler.
There is no function (analogous to fn:doc-available or fn:unparsed-text-available) to determine whether a suitable resource is available. In XQuery and XSLT, try/catch constructs are available to catch the error.
The choice of xs:base64Binary rather than xs:hexBinary for the result is arbitrary. The two types have the same value space and are interchangeable for nearly all purposes, the notable exception being conversion to xs:string.
A comprehensive set of functions for manipulating binary data is available in the EXPath binary module: see [EXPath]. In addition, the EXPath file module provides a function file:read-binary with similar functionality to fn:unparsed-binary, the notable differences being (a) that it takes a file name rather than a URI, and (b) that it is defined to be nondeterministic.
The following XQuery, adapted from an example in the EXPath binary module [EXPath], reads a JPEG image and determines its size in pixels: | |
declare namespace bin = "http://expath.org/ns/binary";
let $content := fn:binary-resource("image.jpeg")
let $int16-at := bin:unpack-unsigned-integer(
$content, ?, 2, 'most-significant-first')
let $loc := bin:find($content, 0, bin:hex('FFC0'))
return { "width": $int16-at($loc + 5),
"height": $int16-at($loc + 7) }declare namespace bin = "http://expath.org/ns/binary";
let $content := fn:unparsed-binary("image.jpeg")
let $int16-at := bin:unpack-unsigned-integer(
$content, ?, 2, 'most-significant-first')
let $loc := bin:find($content, 0, bin:hex('FFC0'))
return { "width": $int16-at($loc + 5),
"height": $int16-at($loc + 7) } | |
The example assumes that the functions in the EXPath binary module are available. |