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

"assert" Statements for "complexType" Datatype

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

In order to enforce the client system generating better order XML messages, we can use the following XSD schema:

<?xml version="1.0"?>
<!-- order.xsd
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="order" type="orderType">
  <xs:key name="skuKey">
   <xs:selector xpath="./line"/>
   <xs:field xpath="@sku"/>
  </xs:key> 
 </xs:element>

 <xs:complexType name="orderType">
  <xs:sequence>
   <xs:element name="billto" type="billtoType" maxOccurs="1"/>
   <xs:element name="line" type="lineType" maxOccurs="100"/>
  </xs:sequence>
  <xs:attribute name="currency" type="xs:string"/>
  <xs:assert test="(billto/@country eq 'CA' and @currency eq 'CAD')
      or (billto/@country eq 'US' and @currency eq 'USD')"/>
 </xs:complexType>

 <xs:complexType name="billtoType">
  <xs:attribute name="country" type="xs:string"/>
 </xs:complexType>
 <xs:complexType name="lineType">
  <xs:attribute name="sku" type="xs:string"/>
  <xs:attribute name="quantity" type="xs:positiveInteger"/>
 </xs:complexType>
</xs:schema>

Note where the "assert" statement is located and how attribute of sub element, billto/@country, is used in the XPath expression.

Let's use the "jaxp.TypeInfoWriter" sample program from Xerces2 to validate the XML document to see if the assertion validation rule works or not:

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

setDocumentLocator(systemId="file:///C:/herong/order.xml", publicI...
startDocument()
 startElement(name="order",type="orderType",attributes={{name="cur...

[Error] order.xml:6:38: cvc-complex-type.2.4.a: Invalid content was 
^^^ found starting with element 'line'. One of '{billto}' is expected.

[Error] order.xml:6:38: cvc-datatype-valid.1.2.1: '1.0' is not a valid
^^^ value for 'integer'.

[Error] order.xml:6:38: cvc-attribute.3: The value '1.0' of attribute
^^^ 'quantity' on element 'line' is not valid with respect to its
^^^ type, 'positiveInteger'.

  startElement(name="line",type="lineType",attributes={{name="sku"...
  endElement(name="line")
  startElement(name="billto",type="billtoType",attributes={{name="...
  endElement(name="billto")

[Error] order.xml:8:36: cvc-identity-constraint.4.2.2: Duplicate key 
^^^ value [XMLBOOK] declared for identity constraint "skuKey" of 
^^^ element "order".

  startElement(name="line",type="lineType",attributes={{name="sku"...
  endElement(name="line")

[Error] order.xml:9:9: cvc-assertion.3.13.4.1: Assertion evaluation 
^^^ ('(billto/@country eq 'CA' and @currency eq 'CAD')       or 
^^^ (billto/@country eq 'US' and @currency eq 'USD')') for element 
^^^ 'order' with type 'orderType' did not succeed.

 endElement(name="order")
endDocument()

I think the assertion validation rule worked ok. It gives us a good example of how to use "assert" statements for "complexType" custom defined datatypes".

Exercise: Modify order.xml to respect all constraints defined in order.xsd.

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

"assert" Statements for "complexType" Datatype - Updated in 2014, by Dr. Herong Yang