XSD Tutorials - Herong's Tutorial Examples - v5.23, by Herong Yang
What Is Simple Datatype?
This section describes simple datatypes. What is a simple datatype? How many built-in simple datatypes provided in XSD 1.1? Can I define my own simple datatypes?
What Is Simple Datatype? Simple Datatype is a datatype who's values are simple and have no XML structure. Simple datatypes can be used to declare:
There are 2 sources of simple datatypes:
For example, the following XSD document uses a built-in simple datatype and a user-defined simple datatype to declare XML elements:
<?xml version="1.1"?> <!-- simple_datatype_test.xsd - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved. --> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Simple_Datatype_Test"> <xs:complexType> <xs:sequence> <!-- Using built-in simple datatype --> <xs:element name="Street" type="xs:string" maxOccurs="unbounded"/> <!-- Using user-defined simple datatype --> <xs:element name="ZIP" maxOccurs="unbounded"> <xs:simpleType> <xs:restriction base="xs:token"> <xs:pattern value="\d\d\d\d\d"/> </xs:restriction> </xs:simpleType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Here is a sample XML document to test the above XSD document:
<?xml version="1.1"?> <!-- simple_datatype_test.xml - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved. --> <Simple_Datatype_Test> <!-- Valid "Street" values --> <Street>500 Main Street</Street> <!-- Invalid "Street" values --> <Street>500 <b>Main</b> Street</Street> <!-- Valid "ZIP" values --> <ZIP>01210</ZIP> <!-- Invalid "ZIP" values --> <ZIP>01210<br/></ZIP> </Simple_Datatype_Test>
When validating this XML document with my XsdSchemaValidator.java program presented earlier in the book, I get these errors:
herong> java XsdSchemaValidator ^^^ simple_datatype_test.xsd simple_datatype_test.xml Error: Line number: 11 Column number: 43 Message: cvc-type.3.1.2: Element 'Street' is a simple type, so it must have no element information item [children]. (Child element is not allowed) Error: Line number: 17 Column number: 25 Message: cvc-type.3.1.2: Element 'ZIP' is a simple type, so it must have no element information item [children]. (Child element is not allowed) Error: Line number: 17 Column number: 25 Message: cvc-pattern-valid: Value '' is not facet-valid with respect to pattern '\d\d\d\d\d' for type '#AnonType_ZIPSimple_Datatype_Test'. Error: Line number: 17 Column number: 25 Message: cvc-type.3.1.3: The value '' of element 'ZIP' is not valid. Failed with errors: 4
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
"simpleType" Components - User-Defined Simple Datatypes
"simpleType" Components with "restriction" Child Components
"simpleType" Components with "union" Child Components
"simpleType" Components with "list" Child Components
Atomic, List, Atomic Union and List Union Datatypes
Constraining Facets on List Datatypes
Constraining Facets on Union Datatypes
Nested List Datatypes - Not Allowed
Nested Atomic and List Union Datatypes
Identity-Constraints: unique, key and keyref
Assertion as Custom Validation Rules
XML Schema Location and Namespace in XML Documents
Overriding Element Types in XML Documents