XML Schema Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.11

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, the declare an empty element using "simpleType":

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- empty_simple.xsd
 - Copyright (c) 2007 by Dr. Herong Yang. 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) 2007 by Dr. Herong Yang. 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:

>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


>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

 About This Book

 Introduction to XML Schema

 XML Editor and Schema Processor - XMLPad

 Java API for XML Processing - JAXP

 JAXP - XML Schema (XSD) Validation

 XML Parser API - Xerces2 Java Parser

 XML Schema Language - Basics

 XML Schema Built-in Datatypes

Complex Element Declaration

 Complex Element vs. Simple Element

Declaring Empty Elements

 Declaring Simple Elements

 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"

 XML Schema Location and Namespace in XML Documents

 Overriding Element Types in XML Documents

 Linking Multiple Schema Documents Together

 Glossary

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2009
Declaring Empty Elements