-
Notifications
You must be signed in to change notification settings - Fork 21
Description
This has already been discussed before at various places, I’d like to raise it again: What about generalizing the context item and allowing it to reference sequences? Are there definitive showstoppers?
The Context Item
As its name says, the context item is a container for a single item in the current context. A value that is bound to the context item is referenced with the Context Item Expression, the single dot: ..
The context item shares many similarities with variables. The main difference is that it currently cannot be used for sequences. I propose to generalize the semantics and introduce a “context value”:
- Items that have formerly been bound to the context item (via the Context Item Declaration, within predicates, the simple map operator, path expressions, the
transform withexpression, etc.) are now bound to the context value. - The revised Context Item Expression returns sequences instead of single items.
- We cannot drop context items completely – for example, we have a Context Item Declaration in the prolog of XQuery expressions, which uses the
itemkeyword – but we can treat it as a secondary concept.
Context Value Declaration
It has become a common pattern to use declare context item to bind a document to the context item and process queries on that item:
declare context item := doc('flowers');
.//flower[name = 'Tigridia']If data can be distributed across multiple documents (which is often, if not the standard case, in databases), this approach does not work. It would work if we could bind sequences:
declare context value := collection('flowers');
.//flower[name = 'Tigridia']External Bindings
Many processors allow users to bind external values to the context item. This approach is particularly restricting for databases, in which data is often distributed across multiple documents. With the generalized concept, it would get possible to bind sequences and collections to the context. Paths like the following one could be used, no matter if the contents are stored in a single document or in a collection:
//flower[name = 'Iridaceae']Focus Functions
The focus function provides a compact syntax for common arity-one functions. The single argument is bound it to the context item:
sort($flowers, (), function { @petals })With the generalization to values, we could easily enhance focus functions to accept arbitrary sequences:
array:sort($flower-species, (), function { count(.) })let $flowers := array:join(
for $flower in //flower
group by $_ := $flower/name
return [ $flower ]
)
(: some $p in petals satisfies $p gt 4 :)
return array:filter($flowers, function { petals > 4 })Use Case: Arrow Expressions
The arrow expression provides an intuitive syntax for performing multiple subsequent operations on a given input. With the context value generalization, we could also process chained sequences:
//flower[name = 'Psychotria']
=> function { count(.) || ' flower(s) found' }()