Using Shorthand for "complexContent" with "restriction"

This section describes a tutorial example on how to use the shorthand format for a complextType definition using a complexContent with a restriction on anyType.

As you can see from previous tutorials, many complex elements are defined as "complexContent" with "restriction" on "anyType". XML Schema offers a shorthand format of:

 <xs:complexType ...>
    (all, choice or sequence component)
    <xs:attribute .../>

for the following "complexType" definition:

  <xs:complexContent ...>
   <xs:restriction base="xs:anyType">
    (all, choice or sequence component)
    <xs:attribute .../>

Now we can rewrite the schema from the previous section complexType_everything.xsd using the shorthand format as complexType_everything_shorthand.xsd

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

 <xs:element name="form">
 <xs:complexType mixed="true">
    <xs:element name="input" type="xs:anyType" maxOccurs="unbounded"/>
    <xs:attribute name="action" type="xs:string"/>


Let's try this schema on the sample XML document, complexType_no_text_content.xml, to see if it works in the same way as complexType_everything.xsd

herong> type complexType_everything.xml

<?xml version="1.0"?>
<form action="" note="Tutorials">
 Your Name: <input type="text"/>
 <input type="submit"/>

herong> java XsdSchemaValidator complexType_everything_shorthand.xsd
^^^ complexType_everything.xml

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

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


 Archived Tutorials


 Full Version in PDF/EPUB