What Is an Assertion?

This section describes assertion introduced in XSD 1.1. It you to specify any validation rules represented as XPath expressions on XML documents.

What Is an Assertion? An assertion is a new feature introduced in XSD 1.1 that allows you to specify any validation rules represented as XPath expressions on XML documents.

Here is the syntax on how an assertion rule should be defined in XSD schema:

 <xs:complexType name="type" ...>
  ...
  <xs:assert test="xpath_expression"/>
 </xs:complexType>

 <xs:simpleType name="type" ...>
  </xs:restriction>
   ...
   <xs:assertion test="xpath_expression"/>
  </xs:restriction>
 </xs:simpleType>

Note that:

For example, the following assertion applies to a "complexType" datatype. It specifies that the "min" attribute value must be less or equal to the "max" attribute value:

<xs:complexType name="intRange">
 <xs:attribute name="min" type="xs:int"/>
 <xs:attribute name="max" type="xs:int"/>
 <xs:assert test="@min le @max"/>
</xs:complexType>

For example, the following assertion applies to a "simpleType" datatype. It specifies that the value must starts with number "4":

<xs:simpleType name="visaNumber">
  <xs:restriction base="xs:string">
    <xs:maxLength value="16"/>
    <xs:assert test="fn:starts-with($value, '4')"/>
  </xs:restriction>
</xs:simpleType>

Last update: 2014.

Table of Contents

 About This Book

 Introduction to XML Schema

 XML Editor and Schema Processor - XMLPad

 Java API for XML Processing - JAXP

 JAXP - XML Schema (XSD) Validation

 Xerces2 Java Parser - Java API of XML Parsers

 Using Xerces2 Java API

 XML Schema Language - Basics

 Introduction of XSD Built-in Datatypes

 "string" and Its Derived Datatypes

 "decimal" and Its Derived Datatypes

 "dateTime" and Its Related Datatypes

 Miscellaneous Built-in Datatypes

 Facets, Constraining Facets and Restriction Datatypes

 "simpleType" - Defining Your Own Simple Datatypes

 Complex Element Declaration

 Identity-Constraints: unique, key and keyref

Assertion as Custom Validation Rules

What Is an Assertion?

 Assertion as the Forth Level Constraints

 "assert" Statements for "complexType" Datatype

 "assertion" Statements for "simpleType" Datatype

 XML Schema Location and Namespace in XML Documents

 Overriding Element Types in XML Documents

 Linking Multiple Schema Documents Together

 Glossary

 References

 PDF Printing Version