How Do You Access XSLT 3.0 Maps and Arrays?

XPath 3.1 and XSLT 3.0 introduce native support for structured data items, specifically maps and arrays, alongside specialized operators like square brackets and the question mark (lookup operator) to access their contents. While square brackets serve as general-purpose dynamic indexers and function call shorthands, the question-mark lookup operator provides a concise, path-friendly way to drill down into composite key-value pairs and ordered sequences.

Understanding the Question-Mark Lookup Operator

The question mark (?) functions as the unary or binary lookup operator in XPath 3.1. When placed after an expression evaluating to a map or array, it navigates into the structure without requiring standard function calls like map:get() or array:get().

Using ? with Maps

For maps (key-value pairs), the question-mark operator retrieves values using string keys, named specifiers, or wildcards:

Using ? with Arrays

For arrays (ordered sequences of members), the question-mark operator accesses items by 1-based integer positions or extracts all members:

Understanding the Square-Bracket Syntax

Square brackets [...] serve two distinct roles when working with XSLT 3.0 maps and arrays: as predicate filters and as function invocation arguments.

Function Invocation and Array Indexing

In XPath 3.1, maps and arrays are instances of functions. Maps act as functions from keys to values, and arrays act as functions from integer positions to values. Because of this, square brackets can be used directly for dynamic lookups:

Key Differences Between ? and Square Brackets

While both syntaxes access nested data, they differ significantly in handling sequences and unboxing: