XSD Tutorials - Herong's Tutorial Examples - v5.23, by Herong Yang
"anyType" Built-in Datatype Values and Representations
This section provides a tutorial example to show built-in datatype 'anyType' valid values and representations. Everything, including XML structure, is a valid 'anyType' lexical representation.
To fully understand built-in datatypes defined in XSD 1.1, we need to spend time to read the specification and writing test XSD documents and XML documents for each datatype. Let's start with the first special built-in datatype "anyType" in this section.
"anyType" is a built-in datatype designed to be a wildcard datatype to represent any simple or complex values with these rules:
To verify these rules, I wrote this simple XSD document:
<?xml version="1.1"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- anyType_datatype_test.xsd - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved. --> <xs:element name="AnyType_Datatype_Test"> <xs:complexType> <xs:sequence> <xs:element name="AnyType" type="xs:anyType" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
With <AnyType> element is declared as "anyType", anything can be placed inside the element. Here is an XML document with some examples:
<?xml version="1.1"?> <AnyType_Datatype_Test> <!-- anyType_datatype_test.xml - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved. --> <!-- Everything is valid in "AnyType" elements --> <AnyType> Everything is valid! </AnyType> <AnyType> 1971-05-16T00:00:01 </AnyType> <AnyType> 头 </AnyType> <AnyType> Hello <b>Herong</b>! </AnyType> <AnyType> Hello <b size="9">Herong</b>! </AnyType> <AnyType> <AnyType> Hello <b size="9">Herong</b>! </AnyType> </AnyType> </AnyType_Datatype_Test>
If you want validate this XML document, you can try it the Java XSD validation tool I wrote earlier in this book. You will get no errors:
herong> javac XsdSchemaValidator.java herong> java XsdSchemaValidator ^^^anytype_datatype_test.xsd anytype_datatype_test.xml Passed.
To show you that "anyType" is not allowed for declaring XML attributes, I wrote this invalid XSD document:
<?xml version="1.1"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- anyType_datatype_invalid_test.xsd - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved. --> <xs:element name="AnyType_Datatype_Test"> <xs:complexType> <xs:sequence> <xs:element name="AnyType" type="xs:anyType" maxOccurs="unbounded"/> <xs:element name="Attribute_Test" maxOccurs="unbounded"> <xs:complexType> <!-- "anyType" is a complex type. It can not be used for attributes --> <xs:attribute name="AnyType" type="xs:anyType"/> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
If you validate the same example XML document with this XSD document, you will see this error:
herong> java XsdSchemaValidator ^^^ anyType_datatype_invalid_test.xsd anytype_datatype_test.xml org.xml.sax.SAXParseException; systemId: anyType_datatype_invalid_test.xsd; lineNumber: 16; columnNumber: 59; src-resolve: Cannot resolve the name 'xs:anyType' to a(n) 'simpleType definition' component. java.lang.NullPointerException
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
Overview of XSD 1.1 Built-in Datatypes
Datatypes, Values and Representations
Datatypes, Values and Representations - Example
Built-in Datatypes Lexical Representation Examples
Declaring Elements and Attributes with Built-in Datatypes
Defining New Datatypes with Built-in Datatypes
►"anyType" Built-in Datatype Values and Representations
"anySimpleType" Built-in Datatype Values and Representations
"anyAtomicType" Built-in Datatype Values and Representations
"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