View Old View New View Both View Only Previous Next

This draft contains only sections that have differences from the version that it modified.

W3C

XPath and XQuery Functions and Operators 4.0

W3C Editor's Draft 23 February 2026

This version:
https://qt4cg.org/specifications/xpath-functions-40/
Latest version of XPath and XQuery Functions and Operators 4.0:
https://qt4cg.org/specifications/xpath-functions-40/
Most recent Recommendation of XPath and XQuery Functions and Operators:
https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/
Editor:
Michael Kay, Saxonica <http://www.saxonica.com/>

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.


Abstract

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.

Status of this Document

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.

Dedication

The publications of this community group are dedicated to our co-chair, Michael Sperberg-McQueen (1954–2024).


14 Processing sequences

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].

14.6 Functions giving access to external information

The functions in this section provide access to resources (such as files) in the external environment.

FunctionMeaning
fn:docRetrieves a document using a URI supplied as an xs:string, and returns the corresponding document node.
fn:doc-availableThe function returns true if and only if the function call fn:doc($source, $options) would return a document node.
fn:collectionReturns a sequence of items identified by a collection URI; or a default collection if no URI is supplied.
fn:uri-collectionReturns a sequence of xs:anyURI values representing the URIs in a URI collection.
fn:unparsed-textThe fn:unparsed-text function reads an external resource (for example, a file) and returns a string representation of the resource.
fn:unparsed-text-linesThe 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-availableAllows an application to determine whether a call on fn:unparsed-text with particular arguments would succeed.
fn:unparsed-binaryThe fn:unparsed-binary function reads an external resource (for example, a file) and returns its contents in binary.
fn:environment-variableReturns the value of a system environment variable, if it exists.
fn:available-environment-variablesReturns a list of environment variable names that are suitable for passing to fn:environment-variable, as a (possibly empty) sequence of strings.

14.6.8 fn:unparsed-binary

Changes in 4.0  

  1. New in 4.0  [Issue 557 PR 1587 18 November 2024]

Summary

The fn:unparsed-binary function reads an external resource (for example, a file) and returns its contents in binary.

Signature
fn:unparsed-binary(
$sourceas xs:string?
) as xs:base64Binary
Properties

This function is deterministic, context-dependent, and focus-independent. It depends on executable base URI.

Rules

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.

Error Conditions

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.

Notes

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.

Examples

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.