Literals and variables
XPath and XQuery support two types of literals. These are numeric literals and string literals. There are three types of numeric literals: integer, decimal, and double. Which one of these three a numeric literal ends up being depends on the format of the literal.
A numeric literal will return a value typed as
xs:integer when it does not contain a "." and an "e" or "E". If the literal contains a "." but no "e" or "E" it will return a value typed as
xs:decimal. If the literal contains an "e" or "E", it will return a value typed as
Numeric literal resulting in xs:integer
Numeric literal resulting in xs:decimal
Numeric literal resulting in xs:double
String literals are enclosed by either two single quotes or two double quotes. String literals in XQuery may contain predefined entity references. These are short sequences of characters, beginning with an ampersand, that represent a single character. You may have seen these before in XML or HTML documents. The following five references are supported:
Strings may also contain a character reference. These references look very much like the predefined entity references, but the character references refer to an Unicode character. For example, the Euro (€) symbol can be represented by the character reference
String literal using double quotes
String literal using single quotes
String literal with character references
"< > & " ' €"
Character references can be "used" in XPath too, as in, nothing bad will happen, but the references will not be converted to the referenced characters. Because the Fonto Editor usually runs any XPath or XQuery script like it is XQuery, this does not apply.
Constructing other data types
Other values can be constructed in a similar way, using the constructor function the desired data type. This also works for data types that can be constructed using literals.
Constructor functions can also be used to create special values that have no literal representation.
Float constructor creating a NaN value
Double constructor creating an infinity value
Basic "let" example
let $someValue := "Some value." return $someValue
A let expression starts with the keyword "let". After that, one or more variables may be declared. Variable names start with a dollar sign ("$"). After the variable name comes the assignment operator (":="). Then follows an expression of which the result is bound to the variable. After the variable declarations follows another keyword: "return". This keyword is followed by another expression. The value returned by this expression is returned by the let expression.
let $x := 1, $y := 2 return $x + $y
Internally, the previous example is parsed as if it were multiple let statements. The following example works the same, but only declares one variable per let expression.
Multiple variables (nested)
let $x := 1 return let $y := 2 return $x + $y
As you can see, the
$x is already in scope when
$y is being declared. This allows for using the value of
$x while declaring
let $x := 1, $y := 1 + $x return $x + $y