<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet href="../make-menu.xsl" type="text/xsl"?><html>
   <head>
      <this-is section="expressions" page="instanceof" subpage=""/>
      <!--
           Generated at 2010-10-29T18:20:48.427+01:00--><title>Saxonica: XSLT and XQuery Processing: Instance of and Castable as</title>
      <meta name="coverage" content="Worldwide"/>
      <meta name="copyright" content="Copyright Saxonica Ltd"/>
      <meta name="title"
            content="Saxonica: XSLT and XQuery Processing: Instance of and Castable as"/>
      <meta name="robots" content="noindex,nofollow"/>
      <link rel="stylesheet" href="../saxondocs.css" type="text/css"/>
   </head>
   <body class="main">
      <h1>Instance of and Castable as</h1>
      <p>The expression <code>E instance of T</code> tests whether the value of expression <code>E</code>
is an instance of type T, or of a subtype of T. For example, <code>$p instance of attribute+</code> is
true if the value of <code>$p</code> 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.</p>
      <p>Saxon also allows testing of the type annotation of an element or attribute node using tests of the
form <code>element(*, T)</code>, <code>attribute(*, T)</code>. 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 <code>xs:untyped</code>
and <code>xs:untypedAtomic</code>) is by validating a document against a schema.</p>
      <p>The expression <code>E castable as T</code> tests whether the expression <code>E cast as T</code>
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.</p>
      <table width="100%">
         <tr>
            <td>
               <p align="right"><a class="nav" href="conditionals.xml">Next</a></p>
            </td>
         </tr>
      </table>
   </body>
</html>