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

Assertion as the Forth Level Constraints

This section describes validation rules specified as assertions, which can be viewed as the forth level of constraints that you can force the XML document author to comply.

Validation rules specified as assertions can be viewed as the forth level of constraints that you can force the XML document author to comply:

  • First Level Constraints - XSD built-in datatypes applied on attributes and simple element contents.
  • Second Level Constraints - User-defined datatypes applied on complex elements.
  • Third Level Constraints - Identify constraints applied on repeating sub elements.
  • Forth Level Constraints - Assertion validation rules applied across attributes and/or elements.

For example, I receive an order XML message, order.xml, from a client system asking to price the order:

<?xml version="1.0"?>
<!-- order.xml
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
-->
<order currency="USD">
 <line sku="XMLBOOK" quantity="1.0"/>
 <billto country="CA"/>
 <line sku="XMLBOOK" quantity="2"/>
</order>

Obviously, there are a number of areas in this order XML message that we can use XSD schema to improve the quality:

  • quantity="1.0" - The quantity should be a positive integer number. We can use first level constraints like the XSD built-in datatype "positiveInteger" to enforce this rule.
  • billto country="CA" - The "billto" sub element should be placed before all "line" sub elements. We can use second level constraints like defining our own complex element type to enforce the sequence of sub elements.
  • sku="XMLBOOK" - Any two order lines should not have the same SKU number. We can use third level constraints like defining a key identify constraint to enforce "sku" values to be unique.
  • currency="USD" and country="CA" - The currency "USD" should not be used in bill-to country "CA. We can use forth level constraints like defining a validation assertion to enforce "currency" values to be valid in the bill-to country.

The validation assertion could be expressed in XPath as "(billto/@country eq 'CA' and @currency eq 'CAD') or (billto/@country eq 'US' and @currency eq 'USD')". See the next section for the full XSD schema.

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

Assertion as the Forth Level Constraints - Updated in 2014, by Dr. Herong Yang