Variables
Variables in XQuery are identified by names that are preceded by a dollar sign ($).[*] The names (not including the dollar sign) must conform to the definition of an XML-qualified name. This means that they can be prefixed, in which case they are associated with the namespace mapped to that prefix. If they are not prefixed, they are not associated with any namespace.
When a query is evaluated, a variable is bound to a particular value. That value may be any sequence, including a single node, a single atomic value, the empty sequence, or multiple nodes and/or atomic values. Once the variable is bound to a value, its value does not change. One consequence of this is that you cannot assign a new value to the variable as you can in most procedural languages. Instead, you must use a new variable.
Variables can be bound in several kinds of expressions: in global variable declarations,
for
or let
clauses of a FLWOR, quantified expressions, or typeswitch expressions. For example, evaluation of the FLWOR:
for$prod
in doc("catalog.xml")/catalog/product return$prod
/number
binds the $prod
variable to a node returned by the path expression doc("catalog.xml")/catalog/product
. The variable is then referenced in the return
clause. Function declarations also bind variables to values. For example, the function declaration:
declare function local:addTwo ($value
as xs:integer) as xs:integer {$value
+ 2 };
binds the $value
variable to the value of the argument passed to it. In this case, the ...
Get XQuery now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.