# Instance of and Castable as

The expression `E instance of T` tests whether the value of expression `E` is an instance of type T, or of a subtype of T. For example, `$p instance of attribute+` is true if the value of `$p` is a sequence of one or more attribute nodes. It returns false if the sequence is empty or if it contains an item that is not an attribute node. The detailed rules for defining types, and for matching values against a type, are given in the XPath 2.0 specification.

Saxon 7.3 implements only a subset of this syntax. It allows testing of a value against any built-in simple type defined in XML Schema, except that some of the types are not yet implemented: see conformance.html. The type can also be a node-kind such as `element`, `attribute`, etc; or it can be one of the keywords `item` or `node`. The type can be optionally followed by the occurrence indicator `*`, `+`, or `?`.

Saxon also allows testing of the type annotation of an element or attribute node using tests of the form `element(*, T)`, `attribute(*, T)`. This is of limited value at this release, however, since the only way a node can acquire a type annotation is (a) if the node is part of a temporary tree created within the stylesheet itself, or (b) if the node is an attribute with a DTD-based type, for example ID.

The expression `E castable as T` tests whether the expression `E cast as T` would succeed. It is useful, for example, for testing whether a string contains a valid date before attempting to cast it to a date. This is because XPath and XSLT currently provide no way of trapping the error if the cast is attempted and fails.

Expand | Next