# 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 also allows testing of the type annotation of an element or attribute node using tests of the form `element(*, T)`, `attribute(*, T)`. This is primarily useful with a schema-aware query or stylesheet, since the only way a node can acquire a type annotation (other than the special values `xs:untyped` and `xs:untypedAtomic`) is by validating a document against a schema.

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.

Next