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) 2002-2013 HerongYang.com. 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:

herong> type complexType_simpleContent.xml

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


herong> 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

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

 Xerces2 Java Parser - Java API of XML Parsers

 Using Xerces2 Java APIs

 XML Schema Language - Basics

 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 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"

 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

 Linking Multiple Schema Documents Together

 Glossary

 Archived Tutorials

 References

 Full Version in PDF/EPUB