XSD Tutorials - Herong's Tutorial Examples - v5.23, by Herong Yang
Using XML Schema Built-in Datatypes Incorrectly
This section describes a tutorial example of using built-in datatypes incorrectly.
The namespace rule on built-in datatypes is very important. If you don't follow it, you will get errors we shown in the following schema examples, holiday_error.xsd:
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema-datatypes"> <!-- holiday_error.xsd - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved. --> <!-- Examples of incorrect built-in datatype "date" --> <xs:element name="holiday1" type="date"/> <xs:element name="holiday2" type="w3c:date"/> <xs:element name="holiday3" type="xsd:date"/> </xs:schema>
Here is the output of XsdSchemaChecker.java on this schema:
herong> java XsdSchemaChecker holiday_error.xsd Error: Line number: 10 Column number: 43 Message: src-resolve: Cannot resolve the name 'date' to a(n) 'type definition' component. Error: Line number: 12 Column number: 47 Message: s4s-att-invalid-value: Invalid attribute value for 'type' in element 'element'. Recorded reason: UndeclaredPrefix: Cannot resolve 'w3c:date' as a QName: the prefix 'w3c' is not declared. Error: Line number: 14 Column number: 47 Message: src-resolve.4.2: Error resolving component 'xsd:date'. It was detected that 'xsd:date' is in namespace 'http://www.w3.org/2001/XMLSchema-datatypes', but components from this namespace are not referenceable from schema document 'file:/C:/herong/holiday_error.xsd'. If this is the incorrect namespace, perhaps the prefix of 'xsd:date' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'file:/C:/herong/holiday_error.xsd'. Error: Line number: 14 Column number: 47 Message: src-resolve: Cannot resolve the name 'xsd:date' to a(n) 'type definition' component. Schema File: holiday_error.xsd Parser Class: com.sun.org.apache.xerces.internal.jaxp.validation.SimpleXMLSchema
The error messages are pretty clear.
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
Schema and Schema XML Representation
Checking Schema Documents - XsdSchemaChecker.java
Creating Schema Documents - "schema" Element
Declaring Root Elements - "element" Element
Specifying Element Datatype - "type" Attribute
Using XML Schema Built-in Datatypes
►Using XML Schema Built-in Datatypes Incorrectly
Validating XML Documents against Schema Documents
Deriving New Simple Datatypes - "simpleType" Element
Defining Complex Datatypes - "complexType" Element
Validation Error Examples on Complex Datatypes
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
XML Schema Location and Namespace in XML Documents
Overriding Element Types in XML Documents