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.

Numeric literals

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 xs:double.

Numeric literal resulting in xs:integer

XQuery

12345

Open example in playground

Numeric literal resulting in xs:decimal

XQuery

12.345

Open example in playground

Numeric literal resulting in xs:double

XQuery

1234E5

Open example in playground

String literals

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:

  • &lt; ("<")

  • &gt; (">")

  • &amp; ("&")

  • &quot; ('"')

  • &apos; ("'")

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 &#8364;.

String literal using double quotes

XQuery

"String"

Open example in playground

String literal using single quotes

XQuery

'String'

Open example in playground

String literal with character references

XQuery

"&lt; &gt; &amp; &quot; &apos; &#8364"

Open example in playground

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 Fonto Editor usually runs any XPath or XQuery script like it is XQuery, this does not apply.

Constructing other data types

Boolean values can be constructed using the built-in functions fn:true and fn:false.

Other

true()

Open example in playground

Other

false()

Open example in playground

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.

Integer constructor

XQuery

xs:integer("12")

Open example in playground

String constructor

XQuery

xs:string("String")

Open example in playground

Date constructor

XQuery

xs:date("2019-12-20")

Open example in playground

Constructor functions can also be used to create special values that have no literal representation.

Float constructor creating a NaN value

XQuery

xs:float("NAN")

Open example in playground

Double constructor creating an infinity value

XQuery

xs:double("INF")

Open example in playground

Variables

A let expression (or let clause in XQuery) is used to declare a variable and bind a value to it.

Basic "let" example

XQuery

let $someValue := "Some value."
return $someValue

Open example in playground

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.

Multiple variables

XQuery

let $x := 1,
    $y := 2
return $x + $y

Open example in playground

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)

XQuery

let $x := 1
return
    let $y := 2
    return $x + $y

Open example in playground

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 $y.

Other

let $x := 1,
    $y := 1 + $x
return $x + $y

Open example in playground