Literals and Operators

Now that we know about values and types in XQuery, we describe how the static type system assigns types to expressions. The static type system checks that the type of an expression is compatible with the type required by the context in which the expression is used. If they are not compatible, the static type system raises a type error.

In the remaining sections, the term type refers to the static type associated with an XQuery expression. We use the phrase dynamic type whenever referring to the type associated with a value.

Literals are the simplest XQuery expressions. Every literal expression has one of the types xs:string , xs:integer , xs:decimal , or xs:double . Here are some examples:

 "hello"  has type  xs:string 42  has type  xs:integer 42.00  has type  xs:decimal 4.2e1  has type  xs:double 

(Note: XQuery could have given 42 the more specific type xs:positiveInteger , which is derived from xs:integer , but for simplicity the language specification does not distinguish between positive and negative integer literals.)

The type of an arithmetic operation depends on the types of its operands. Here are some examples:

 42 + 69  has type  xs:integer 42.00 + 69.00  has type  xs:decimal 4.2e1 + 6.9e1  has type  xs:double 

If the arguments are both numeric, but have different types, then one type is promoted to match the other (see Glossary, s.v. "type promotion"). The order of promotion follows : xs:integer , xs:decimal , xs:float , xs:double . Here are some examples of expressions whose types are promoted:

 42 + 69.00  has type  xs:decimal 4.2e1 + 69  has type  xs:double 42.00 + 6.9e1  has type  xs:double 

In the first line, the type of the first argument is promoted from xs:integer to match the type of the second argument, xs:decimal . These static type rules correspond to exactly the same rules that are used to promote values at evaluation time.

It is also possible to construct dates and durations by applying constructor functions to string literals (e.g., xs:date("1994-01-18") ) and to perform arithmetic on dates and durations when sensible (for example, one may add a date to a duration, or multiply a duration by a decimal, but not add a date to a date). The details are spelled out in the XQuery specification [XQ-LANG].

A static type error is raised if the operands do not have sensible types. For instance, addition does not apply to strings. Here are some examples:

 "one" + "two"  is a static type error  "42" + 69.00  is a static type error  

A comparison operation always yields a Boolean. The operation is well typed if the operands have types that can sensibly be compared. Here are some examples:

 "one" < "two"  has type  xs:boolean 1 < 2  has type  xs:boolean 1 < 2.0  has type  xs:boolean 

A static type error is raised if the operands cannot be sensibly compared. Here is an example that raises a type error because a string cannot be compared to an integer:

 "one" < 2  is a static type error  

The sequence operator concatenates two sequences. Here are some examples:

 (1, "two", 3.14e0)  has type  (xs:integer, xs:string, xs:double) (<th>value</th>,<td>1</td>)  has type  (element(th), element(td)) 

Note that the type of each sequence expression is a sequence type. Even though you cannot use sequence types explicitly in queries, such a type might appear in an error message.



XQuery from the Experts(c) A Guide to the W3C XML Query Language
Beginning ASP.NET Databases Using VB.NET
ISBN: N/A
EAN: 2147483647
Year: 2005
Pages: 102

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net