Declaring Complex Elements with Attributes Only

This section describes a tutorial example on how to declare an element that accepts only attributes using a user defined complexType datatype.

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

A good example of complex elements with attributes only is the <img> element in HTML documents. It accepts only attributes like "src", and "alt". But it should not accept any sub elements or any text content.

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. Each "attribute" component inside "restriction" defines an additional attribute for the base datatype.

Putting all these rules together, I wrote this sample schema, complexType_attribute_only.xsd, that defines <img> to accept only 2 attributes:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- complexType_attribute_only.xsd
 - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved.
-->

 <xs:element name="img">
 <xs:complexType>
  <xs:complexContent>
   <xs:restriction base="xs:anyType">
    <xs:attribute name="src" type="xs:string"/>
    <xs:attribute name="alt" type="xs:string"/>
   </xs:restriction>
  </xs:complexContent>
 </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_attribute_only.xml

<?xml version="1.0"?>
<img src="picture.jpg" alt="Picture"><b>Nice</b> Picture!</img>


herong> java XsdSchemaValidator complexType_attribute_only.xsd
complexType_attribute_only.xml

Error:
   Line number: 2
   Column number: 64
   Message: cvc-complex-type.2.1: Element 'img' 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

 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