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 H 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).


17 Higher-order functions

17.3 Dynamic Evaluation

The following functions allow dynamic loading and evaluation of XQuery queries, XSLT stylesheets, and XPath binary operators.

FunctionMeaning
fn:load-xquery-moduleProvides access to the public functions and global variables of a dynamically loaded XQuery library module.
fn:transformInvokes a transformation using a dynamically loaded XSLT stylesheet.
fn:opReturns a function whose effect is to apply a supplied binary operator to two arguments.

17.3.4 fn:op

Changes in 4.0  

  1. New in 4.0  [Issue 83 PR 173 11 October 2022]

Summary

Returns a function whose effect is to apply a supplied binary operator to two arguments.

Signature
fn:op(
$operatoras xs:string
) as fn(item()*, item()*) as item()*
Properties

This function is deterministic, context-independent, and focus-independent.

Rules

The supplied operator must be one of:

",", "and", "or", "+", "-", "*", "div", "idiv", "mod", "=", "<", "<=", ">", ">=", "!=", "eq", "lt", "le", "gt", "ge", "ne", "<<", ">>", "precedes", "follows", "is", "is-not", "||", "|", "union", "except", "intersect", "to", "otherwise".

The result of calling fn:op("⊙"), where is one of the above operators, is the function represented by the XPath expression:

fn($x, $y) { $x ⊙ $y }

For example, op("+") returns fn($x, $y) { $x + $y }.

Error Conditions

A type error is raised [err:XPTY0004]XP if the supplied argument is not one of the supported operators.

Notes

The function is useful in contexts where an arity-2 callback function needs to be supplied, and a standard operator meets the requirement.

For example, the XSLT xsl:map instruction has an on-duplicates attribute that expects such a function. Specifying on-duplicates="op(',')" is equivalent to specifying on-duplicates="fn($x, $y) { $x, $y }

The function is also useful in cases where the choice of operator to apply is made dynamically.

Some operators (such as and, or, and otherwise) have custom error handling semantics, with the effect that evaluating one of the operands cannot cause an error unless the other operand has a particular value (see Section 2.4.5 Guarded ExpressionsXP). Although implementations are free to make optimizations, it should be assumed that a function call such as op('and')(X, Y) will have the normal semantics of a dynamic function call, where the arguments are evaluated in any order, and a failure evaluating any argument may cause the function call as a whole to fail.

Examples
ExpressionResult
for-each-pair(21 to 25, 1 to 5, op("+"))
22, 24, 26, 28, 30
for-each-pair(21 to 25, 1 to 5, op("-"))
20, 20, 20, 20, 20