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

Declaring Complex Elements with Sub Elements Only

This section describes a tutorial example on how to declare an element that accepts only sub (child) elements without attributes and text content using a user define complexType datatype.

A complex element with sub elements only is an element with sub (child) elements, but no text content and no attribute. This type of element can be declared with a "complexType" datatype with "complexContent" containing some "element"s.

A good example of complex elements with attributes only is the <ul> element in HTML documents. It accepts only sub element of <li>. But it should not have any text content, or any attributes.

Rule 1. The "complexType" component defines a datatype for a complex element.

Rule 2. The "complexContent" component used inside "complexType" specifies that the complex element could have sub (child) elements and attributes.

Rule 3. The "restriction" component inside "complexContent" restricts the specified base datatype for the content to be more restrictive.

Rule 4. When the built-in datatype "anyType" is used as the base datatype of a "restriction" component inside "complexContent", it provides an empty datatype definition.

Rule 5. The "sequence" component inside "restriction" defines a sequence model group, which can be used to specify a list of sub elements.

Putting all these rules together, I wrote this sample schema, complexType_sub_element_only.xsd, that defines <ul> to accept only sub elements:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- complexType_sub_element_only.xsd
 - Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
-->

 <xs:element name="ul">
 <xs:complexType>
  <xs:complexContent>
   <xs:restriction base="xs:anyType">
    <xs:sequence>
    <xs:element name="li" type="xs:string" maxOccurs="unbounded"/>
    </xs:sequence>
   </xs:restriction>
  </xs:complexContent>
 </xs:complexType>
 </xs:element>

</xs:schema>

Let's try this schema on a sample XML document, complexType_sub_element_only.xml, with some errors:

>type complexType_sub_element_only.xml

<?xml version="1.0"?>
<ul note="Key Words">
 <li>XSD</li>
 <li>Tutorial</li>
 Example
</ul>


>java XsdSchemaValidator complexType_sub_element_only.xsd 
complexType_sub_element_only.xml

Error:
   Line number: 2
   Column number: 22
   Message: cvc-complex-type.3.2.2: Attribute 'note' is not allowed
   to appear in element 'ul'.

Error:
   Line number: 6
   Column number: 6
   Message: cvc-complex-type.2.3: Element 'ul' cannot have 
   character [children], because the type's content type is 
   element-only.

Failed with errors: 2

Sections in This Chapter

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 and Text Content

Using Shorthand for "complexContent" with "restriction"

Dr. Herong Yang, updated in 2007
Declaring Complex Elements with Sub Elements Only