Types, Nodes, and Atomic Values

Element and attribute nodes, as well as atomic values, all have types associated with them. Sequences don't technically have types, although they can be matched to sequence types, as described later in this chapter.

Nodes and Types

All element and attribute nodes have type annotations, which indicate the type of their content. An element or attribute can come to be annotated with a specific type when it is validated against a schema. This might occur when the document is first opened, or as the result of a validate expression. Schema validation is discussed further in Chapter 13.

If an element or attribute has not been validated and does not have a specific type, it is automatically assigned a generic type, namely xs:untyped (for elements) or xs:untypedAtomic (for attributes). Sometimes these nodes are referred to as untyped, despite the fact that they do have a type, albeit a generic one.

Attributes, and most elements, also have a typed value.[*] This typed value is an atomic value extracted from the node, taking into account the node's type annotation. For example, if the number element has been validated and given the type xs:integer, its typed value is 784 (type xs:integer). If the number element is untyped, its typed value is 784 (type xs:untypedAtomic). The data function allows you to retrieve the typed value of a node.

Atomic Values and Types

Every atomic value has a type. An atomic value might have a specific type because:

  • It is extracted from an element or attribute that has a type annotation. This can be done explicitly using the data function, or automatically using many functions and operators.

  • It is the result of a constructor function or a cast expression.

  • It is the value of a literal expression. Literals surrounded by single or double quotes are considered to have the type xs:string, whereas nonquoted numeric values have the type xs:integer, xs:decimal, or xs:double, depending on their format.

  • It is the result of an expression or function that returns a value of a particular type—for example, a comparison expression returns an xs:boolean value, and the count function returns an xs:integer.

A value might not have a specific type if it was extracted from an untyped element or attribute. In this case, it is automatically assigned the generic type xs:untypedAtomic. Untyped atomic values can be used wherever a typed value can be used, and they are usually cast to the required type automatically. This is because every function and expression has rules for casting untyped values to an appropriate type.



[*] Technically, other kinds of nodes have typed values, too, but they are less useful.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.70.38