XSD Tutorials - Herong's Tutorial Examples - Version 5.11, 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:
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:sequence> <xs:element name="number" type="visaNumber"/> <xs:element name="name" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:simpleType name="visaNumber"> <xs:restriction base="xs:string"> <xs:maxLength value="16"/> <xs:assertion test="starts-with($value, '4')"/> </xs:restriction> </xs:simpleType> </xs:schema>
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. --> <visaCard> <number>5454545454545454</number> <name>John Gates</name> </visaCard>
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... startDocument() startElement(name="visaCard",type="visaCardType",attributes={}) startElement(name="number",type="visaNumber",attributes={}) [Error] visa_card.xml:6:35: cvc-assertion.3.13.4.1: Assertion ^^^ evaluation ('starts-with($value, '4')') for element 'number' with ^^^ type 'visaNumber' did not succeed. endElement(name="number") startElement(name="name",type="{http://www.w3.org/2001/XMLSchema... endElement(name="name") endElement(name="visaCard") endDocument()
The error message confirms that the "assertion" statement in visa_card.xsd is working.
Last update: 2014.
Table of Contents
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
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
Identity-Constraints: unique, key and keyref
►Assertion as Custom Validation Rules
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