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

Declaring Complex Elements with Simple Content

This section describes a tutorial example on how to define an element that accepts attributes and text content without sub elements using complexType with simpleContent.

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

A good example of complex elements with simple content is the <textarea> element in HTML documents. It accepts a text string as the content with no sub (child) element and some attributes.

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

Rule 2. The "simpleContent" component used inside "complexType" specifies that the complex element should not have any sub (child) element.

Rule 3. The "extension" component inside "simpleContent" extends the specified base datatype for the content to include "attribute" declarations.

Rule 4. Each "attribute" component inside "extension" defines an additional attribute for the base datatype.

Here is an sample schema, complexType_simpleContent.xsd, that defines <textarea> to accept simple content and 3 attributes:

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

 <xs:element name="textarea">
 <xs:complexType>
  <xs:simpleContent>
   <xs:extension base="xs:string">
    <xs:attribute name="name" type="xs:string"/>
    <xs:attribute name="cols" type="xs:positiveInteger"/>
    <xs:attribute name="rows" type="xs:positiveInteger"/>
   </xs:extension>
  </xs:simpleContent>
 </xs:complexType>
 </xs:element>

</xs:schema>

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

>type complexType_simpleContent.xml

<?xml version="1.0"?>
<textarea name="comment" cols="50" rows="l">
This book is <b>really</b> helpful...
</textarea>


>java XsdSchemaValidator complexType_simpleContent.xsd 
   complexType_simpleContent.xml

Error:
   Line number: 2
   Column number: 45
   Message: cvc-datatype-valid.1.2.1: 'l' is not a valid value 
   for 'integer'.

Error:
   Line number: 2
   Column number: 45
   Message: cvc-attribute.3: The value 'l' of attribute 'rows' on 
   element 'textarea' is not valid with respect to its type, 
   'positiveInteger'.

Error:
   Line number: 4
   Column number: 12
   Message: cvc-complex-type.2.2: Element 'textarea' must have no 
   element [children], and the value must be valid.

Failed with errors: 3

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 Simple Content