<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet href="../../make-menu.xsl" type="text/xsl"?><html>
   <head>
      <this-is section="schema-processing" page="schema11" subpage="simpleassert"/>
      <!--
           Generated at 2010-10-29T18:20:48.427+01:00--><title>Saxonica: XSLT and XQuery Processing: Assertions on Simple Types</title>
      <meta name="coverage" content="Worldwide"/>
      <meta name="copyright" content="Copyright Saxonica Ltd"/>
      <meta name="title"
            content="Saxonica: XSLT and XQuery Processing: Assertions on Simple Types"/>
      <meta name="robots" content="noindex,nofollow"/>
      <link rel="stylesheet" href="../../saxondocs.css" type="text/css"/>
   </head>
   <body class="main">
      <h1>Assertions on Simple Types</h1>
      <p>Saxon allows assertions on simple types to be defined. The mechanism is to define an <code>xs:assert</code>
      element as a child of the <code>xs:restriction</code> child of the <code>xs:simpleType</code> element (that is, it acts
      as an additional facet). The type must
      be an atomic type. The value of the <code>test</code> attribute of <code>xs:assert</code> is an XPath expression. .</p>
      <p>The expression is evaluated with the value being validated supplied as the value of the variable <code>$value</code>. 
      This will be an instance of the
      base type: for example, if you are restricting from <code>xs:string</code>, it will be a string; if you are
      restricting from <code>xs:date</code>, it will be an <code>xs:date</code>; if you are validating a list of integers,
    then <code>$value</code> will be a sequence of integers.</p>
      <p>If the effective boolean value of the expression is true, the value is valid. If the effective boolean value is
      false, or if a dynamic error occurs while evaluating the expression, the value is invalid. Currently no diagnostics
      are produced to indicate why the value is deemed invalid, other than a statement that the <code>xs:assert</code>
      facet is violated. You can supply a message in a <code>saxon:message</code> attribute.</p>
      <p>The XPath expression has no access to any part of the document being validated, other than the atomic value of the
      actual element or attribute node. So the validation cannot be context-sensitive.</p>
      <p>The XPath expression may make calls on Java extension functions in the normal way: see
      <a class="bodylink" href="../../extensibility/functions.xml">Writing extension functions (Java)</a>. This facility has not
      been tested on .NET but there is no intrinsic reason why it should not work. Allowing call-out to procedural 
      programming languages means that you can perform arbitrary procedural validation of element and attribute values.
      Take care to disable use of extension functions if validating against a schema that is untrusted.</p>
      <p>The following example validates that a date is in the past:</p>
      <div class="codeblock"
           style="border: solid thin; background-color: #B1CCC7; padding: 2px">
         <pre>
            <code>
  &lt;xs:element name="date"&gt;
    &lt;xs:simpleType&gt;
       &lt;xs:restriction base="xs:date"&gt;
         &lt;xs:assert test="$value lt current-date()"/&gt;
       &lt;/xs:restriction&gt;   
    &lt;/xs:simpleType&gt;
  &lt;/xs:element&gt;
</code>
         </pre>
      </div>
      <p>The following example validates that a string is a legal XPath expression. This relies
      on the fact that a failure evaluating the assertion is treated as
      "false":</p>
      <div class="codeblock"
           style="border: solid thin; background-color: #B1CCC7; padding: 2px">
         <pre>
            <code>
  &lt;xs:element name="xpath"&gt;
    &lt;xs:simpleType&gt;
       &lt;xs:restriction base="xs:string"&gt;
         &lt;xs:assert test="exists(saxon:expression($value))" xmlns:saxon="http://saxon.sf.net/"/&gt;
       &lt;/xs:restriction&gt;   
    &lt;/xs:simpleType&gt;
  &lt;/xs:element&gt;
</code>
         </pre>
      </div>
      <p>Note how the in-scope namespaces for the XPath expression are taken from the in-scope namespaces of the
      containing <code>xs:assert</code> element.</p>
      <table width="100%">
         <tr>
            <td>
               <p align="right"><a class="nav" href="cta.xml">Next</a></p>
            </td>
         </tr>
      </table>
   </body>
</html>