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

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 sections, 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 .../>
    ...
 </xs:complexType>

for the following "complexType" definition:

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

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="http://www.w3.org/2001/XMLSchema">
<!-- complexType_everything_shorthand.xsd
 - Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
-->

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

</xs:schema>

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

>type complexType_everything.xml

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


>java XsdSchemaValidator complexType_everything_shorthand.xsd 
   complexType_everything.xml

Error:
   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

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
Using Shorthand for "complexContent" with "restriction"