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:
- Named key lookup:
$map?nameextracts the entry associated with the string key'name'. - String literal lookup:
$map?'first-name'retrieves keys containing hyphens or special characters. - Variable/dynamic key lookup:
$map?($keyVariable)evaluates the expression in parentheses to resolve the key dynamically. - Wildcard lookup:
$map?*returns a sequence of all values stored in the map.
Using ? with Arrays
For arrays (ordered sequences of members), the question-mark operator accesses items by 1-based integer positions or extracts all members:
- Positional lookup:
$array?1extracts the first member of the array. - Wildcard flattening:
$array?*unboxes the array, returning all its members as a flat XPath sequence.
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:
- Dynamic map lookup:
$map($dynamicKey)invokes the map as a function, returning the corresponding value. - Dynamic array indexing:
$array($index)invokes the array as a function, returning the member at that integer index. - Combined with sequence predicates:
$map?items[1]uses the question mark to access theitemssequence and square brackets[1]as a standard XPath predicate filter to select the first sequence item.
Key Differences
Between ? and Square Brackets
While both syntaxes access nested data, they differ significantly in handling sequences and unboxing:
- Sequence iteration: The lookup operator
automatically maps over sequences on its left-hand side. If
$listOfMapsis a sequence of five maps,$listOfMaps?titlereturns a sequence of thetitlevalues from all five maps. - Member flattening: The
$array?*syntax unpacks an array into a regular XPath sequence, whereas standard square bracket predicates[ ... ]filter sequences without unboxing underlying array wrappers.