Declaring Complex Elements with Attributes and Sub Elements

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

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

A good example of complex elements with attributes only is the <table> element in HTML documents. It accepts only attributes like "border" and sub elements like <tr>. But it should not have any text content.

Combine the rules described in the previous two sections, we should be able to define a complex element with sub elements and attributes only by putting the "sequence" of "element" and "attribute" inside "restriction".

Rule 1. The "sequence" of "element" component must be placed before and "attribute" component, if both of them are included in "restriction".

Here is a sample schema, complexType_no_text_content.xsd, that defines <table> to accept sub elements and attributes only, and no text content:

<?xml version="1.0"?>
<xs:schema xmlns:xs="">
<!-- complexType_no_text_content.xsd
 - Copyright (c) 2013,, All Rights Reserved.

 <xs:element name="table">
   <xs:restriction base="xs:anyType">
    <xs:element name="tr" type="xs:anyType" maxOccurs="unbounded"/>
    <xs:attribute name="border" type="xs:string"/>


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

>type complexType_no_text_content.xml

<?xml version="1.0"?>
<table border="1" note="Tutorials">

>java XsdSchemaValidator complexType_no_text_content.xsd 

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

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

Failed with errors: 2

Last update: 2013.

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 API

 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



 PDF Printing Version