XSD Tutorials - Herong's Tutorial Examples - Version 5.10, by Dr. Herong Yang

"assertion" Statements for "simpleType" Datatype

This section provides a tutorial example on how to use 'assertion' statements for 'simpleType' custom defined datatypes.

When using assertion validation rules on "simpleType" custom defined datatypes, we need to remember several things:

  • Use "assertion", not "assert", statements inside the "restriction" statement at the end.
  • Use "$value" in the XPath expression to represent the value of the node that is being validated.
  • Use "xxx()" in the XPath expression to invoke built-in functions to construct complex expressions.

Here is an example of an XSD 1.1 schema, visa_card, that defines an assertion validation rule for a "simpleType" custom defined datatype:

<?xml version="1.0"?>
<!-- visa_card.xsd
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="visaCard" type="visaCardType"/>

 <xs:complexType name="visaCardType">
   <xs:element name="number" type="visaNumber"/>
   <xs:element name="name" type="xs:string"/>

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


This XSD schema enforces the XML document to provide a Visa number starting with digit "4". Here is a sample XML document, visa_card.xml, that should fail the XSD schema validation:

<?xml version="1.0"?>
<!-- visa_card.xml
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 <name>John Gates</name>

Again, let's use the "jaxp.TypeInfoWriter" sample program from Xerces2 to validate the XML document:

>jdk8x2r jaxp.TypeInfoWriter -xsd11 -a visa_card.xsd -i visa_card.xml

setDocumentLocator(systemId="file:///C:/herong/visa_card.xml", pub...
[Error] visa_card.xml:6:35: cvc-assertion. Assertion
^^^ evaluation ('starts-with($value, '4')') for element 'number' with
^^^ type 'visaNumber' did not succeed.


The error message confirms that the "assertion" statement in visa_card.xsd is working.

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



 PDF Printing Version

"assertion" Statements for "simpleType" Datatype - Updated in 2014, by Dr. Herong Yang