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 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 instance of operator tests whether a value is marked or labelled as an instance of the relevant type, not whether it is convertible to that type. For example, 5 instance of xs:positiveInteger returns false, because the value 5 is labelled with the type xs:integer, not xs:positiveInteger.

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. So 5 castable as xs:positiveInteger succeeds. The construct is useful because XPath provides no way of trapping the error if the cast is attempted and fails. (Note however that XQuery 3.0 and XSLT 3.0 both introduce a try/catch mechanism.)