Chapter 23. Function Items and Higher-Order Functions

Starting in version 3.0, the XQuery data model treats functions as full-fledged items in the data model, where previously only nodes and atomic values had this status. This opens up a lot of new functionality, allowing for example functions to be bound to variables, or passed as arguments to other functions. This chapter explores the possibilities allowed by function items and the associated new XQuery syntax.

All of the functionality described in this chapter is only supported by implementations that implement the optional Higher Order Function Feature.

Why Higher-Order Functions?

The inclusion of functions as items into the data model allows them to be passed to and from other functions. This enables higher-order functions, which are functions that take other functions as parameters, and/or return other functions as their result. For more complex applications, higher-order functions can significantly simplify XQuery code and make it more flexible.

Higher-order functions are particularly important in XQuery because there is no other convenient way of parameterizing the code that gets executed. There is no dynamic dispatch based on the type of an object, as in object-oriented languages, and there is no dynamic selection of template rules based on pattern matching as in XSLT. If you want to write a function that does something generic, like visiting all the nodes on the tree and doing some action on each one, and you don’t want to ...

Get XQuery, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.