Types

"Type" is a word that gets used a lot in the XPath specifications and others related to them. The type system of the XQuery and XPath Data Model (XDM) is based on the Schema Component Model described in XML Schema (XSD). There are many pre-defined types, but in some contexts it is also possible for a user to define a custom type.

From 4.0, there are several options for a user to define a custom type:

  • a choice type, which is the union of 2 or more types
  • a record type, which can be used to contrain the structure and contents of a map
  • an enumeration type, which can be used to constrain string values
  • a node type (element, attribute or document), which can be used to constrain the contents of a sequence
  • a named item type, which enables a user to assign a name to a custom type, for easier re-use

As the type system is hierarchical, in some contexts it is necessary or useful for a sub-type to be recognised as belonging to its super-type or vice versa. The XDM defines coercion rules to assist with this being handled consistently across implementations.