XSD Tutorials - Herong's Tutorial Examples - v5.23, by Herong Yang
Declaring Empty Elements
This section describes a tutorial example on how to declare an empty element by defining a special complexType datatype.
What is an empty element? An empty element is an element with no text content, no sub (child) element, and no attribute. There are ways to declare an empty element:
1. An empty element can be declared with a "simleType" datatype derived from "string" with length of "0". Here is a sample schema, empty_simple.xsd, that declares an empty element using "simpleType":
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- empty_simple.xsd - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved. --> <xs:element name="br"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:length value="0"/> </xs:restriction> </xs:simpleType> </xs:element> </xs:schema>
2. An empty element can be declared with a "complexType" datatype with "complexContent" containing no "element" and no "attribute". Here is a sample schema, empty_complex.xsd, that declares an empty element using "complexType":
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- empty_complex.xsd - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved. --> <xs:element name="br"> <xs:complexType> <xs:complexContent> <xs:restriction base="xs:anyType"/> </xs:complexContent> </xs:complexType> </xs:element> </xs:schema>
The second way seems to be better than the first way. It extends the generic built-in datatype "anyType" with no attribute and no sub element.
To test these 2 schema documents, I wrote the following XML document, empty_error.xml.
<?xml version="1.0"?> <br> </br>
Here are the errors generated from XsdSchemaValidator.java: They confirm that the second schema is better:
herong> java XsdSchemaValidator empty_simple.xsd empty_error.xml Error: Line number: 2 Column number: 11 Message: cvc-length-valid: Value ' ' with length = '1' is not facet-valid with respect to length '0' for type '#AnonType_br'. Error: Line number: 2 Column number: 11 Message: cvc-type.3.1.3: The value ' ' of element 'br' is not valid. Failed with errors: 2 herong> java XsdSchemaValidator empty_complex.xsd empty_error.xml Error: Line number: 2 Column number: 11 Message: cvc-complex-type.2.1: Element 'br' must have no character or element information item [children], because the type's content type is empty. Failed with errors: 1
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
Complex Element vs. Simple Element
Declaring Complex Elements with Simple Content
Declaring Complex Elements with Attributes Only
Declaring Complex Elements with Sub Elements Only
Declaring Complex Elements with Attributes and Sub Elements
Declaring Complex Elements with Attributes, Sub Elements & Text Content
Using Shorthand for "complexContent" with "restriction"
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