Reusing Schema Components

In this lesson of the XML Schema tutorial, you will learn...
  1. To define element groups.
  2. To define attribute groups.
  3. To reuse element and attribute groups.

Overview

We have already seen several methods of reusing schema parts.

  • Declaring elements globally.
  • Declaring attributes globally.
  • Defining global simple types.
  • Defining global complex types.

We will now look at some other methods of reuse.

Groups

Element and attribute groups can be used to create a set structure for reuse. To illustrate the benefit of groups, let's first look at a simple XML instance and its (rather long) schema that does not use groups.

Code Sample: ReusingComponents/Demos/WinnieThePooh.xml

<?xml version="1.0"?>
<Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="Book.xsd">
 <Title>Winnie the Pooh</Title>
 <Author Title="Mr." BirthYear="1882">
  <FirstName>A.</FirstName>
  <MiddleName>A.</MiddleName>
  <LastName>Milne</LastName>
  <Specialty>Childrens</Specialty>
 </Author>
 <Illustrator Title="Mr." BirthYear="1879">
  <FirstName>Ernest</FirstName>
  <MiddleName>H.</MiddleName>
  <LastName>Shepard</LastName>
 </Illustrator>
</Book>

Code Sample: ReusingComponents/Demos/Book.xsd

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="Book">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="Title" type="xs:string"/>
    <xs:element name="Author">
     <xs:complexType>
      <xs:sequence>
       <xs:element name="FirstName" type="xs:string"/>
       <xs:element name="MiddleName" type="xs:string" minOccurs="0"/>
       <xs:element name="LastName" type="xs:string"/>
       <xs:element name="Specialty">
        <xs:simpleType>
          <xs:restriction base="xs:string">
           <xs:enumeration value="Mystery"/>
           <xs:enumeration value="Humor"/>
           <xs:enumeration value="Horror"/>
           <xs:enumeration value="Childrens"/>
          </xs:restriction>
        </xs:simpleType>
       </xs:element>
      </xs:sequence>
      <xs:attribute name="Title">
       <xs:simpleType>
        <xs:restriction base="xs:string">
         <xs:enumeration value="Mr."/>
         <xs:enumeration value="Ms."/>
         <xs:enumeration value="Dr."/>
        </xs:restriction>
       </xs:simpleType>
      </xs:attribute>
      <xs:attribute name="BirthYear" type="xs:gYear"/>
     </xs:complexType>
    </xs:element>
    <xs:element name="Illustrator" minOccurs="0">
     <xs:complexType>
      <xs:sequence>
       <xs:element name="FirstName" type="xs:string"/>
       <xs:element name="MiddleName" type="xs:string" minOccurs="0"/>
       <xs:element name="LastName" type="xs:string"/>
      </xs:sequence>
      <xs:attribute name="Title">
       <xs:simpleType>
        <xs:restriction base="xs:string">
         <xs:enumeration value="Mr."/>
         <xs:enumeration value="Ms."/>
         <xs:enumeration value="Dr."/>
        </xs:restriction>
       </xs:simpleType>
      </xs:attribute>
      <xs:attribute name="BirthYear" type="xs:gYear"/>
     </xs:complexType>
    </xs:element>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
</xs:schema>
Code Explanation

The Author element and the Illustrator element have some elements and attributes in common. Let's see how we can make this code more modular.

Element Groups

First, we'll look at how we can group the FirstName, MiddleName, and LastName elements with xs:group to avoid rewriting the elements.

Code Sample: ReusingComponents/Demos/Book2.xsd

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:group name="GroupName">
  <xs:sequence>
   <xs:element name="FirstName" type="xs:string"/>
   <xs:element name="MiddleName" type="xs:string" minOccurs="0"/>
   <xs:element name="LastName" type="xs:string"/>
  </xs:sequence>
 </xs:group>
 <xs:element name="Book">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="Title" type="xs:string"/>
    <xs:element name="Author">
     <xs:complexType>
      <xs:sequence>
       <xs:group ref="GroupName"/>
---- Code Omitted ----
</xs:complexType> </xs:element> <xs:element name="Illustrator" minOccurs="0"> <xs:complexType> <xs:sequence> <xs:group ref="GroupName"/> </xs:sequence>
---- Code Omitted ----
</xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>

Attribute Groups

Now let's look at how we can use the xs:attributeGroup element to avoiding rewriting those attributes.

Code Sample: ReusingComponents/Demos/Book3.xsd

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:group name="GroupName">
  <xs:sequence>
   <xs:element name="FirstName" type="xs:string"/>
   <xs:element name="MiddleName" type="xs:string" minOccurs="0"/>
   <xs:element name="LastName" type="xs:string"/>
  </xs:sequence>
 </xs:group>
 <xs:attributeGroup name="AttGroupPerson">
  <xs:attribute name="Title">
   <xs:simpleType>
    <xs:restriction base="xs:string">
     <xs:enumeration value="Mr."/>
     <xs:enumeration value="Ms."/>
     <xs:enumeration value="Dr."/>
    </xs:restriction>
   </xs:simpleType>
  </xs:attribute>
  <xs:attribute name="BirthYear" type="xs:gYear"/>
 </xs:attributeGroup>
 <xs:element name="Book">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="Title" type="xs:string"/>
    <xs:element name="Author">
     <xs:complexType>
      <xs:sequence>
       <xs:group ref="GroupName"/>
       <xs:element name="Specialty">
        <xs:simpleType>
         <xs:restriction base="xs:string">
          <xs:enumeration value="Mystery"/>
          <xs:enumeration value="Humor"/>
          <xs:enumeration value="Horror"/>
          <xs:enumeration value="Childrens"/>
         </xs:restriction>
        </xs:simpleType>
       </xs:element>
      </xs:sequence>
      <xs:attributeGroup ref="AttGroupPerson"/>
     </xs:complexType>
    </xs:element>
    <xs:element name="Illustrator" minOccurs="0">
     <xs:complexType>
      <xs:sequence>
       <xs:group ref="GroupName"/>
      </xs:sequence>
      <xs:attributeGroup ref="AttGroupPerson"/>
     </xs:complexType>
    </xs:element>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
</xs:schema>

Extending Complex Types

New complex types can be derived by extending existing complex types. Both elements and attributes can be added in the new type, but nothing in the existing type can be overridden. New elements are appended to the content model, such that the original elements and new elements act as two groups that must appear in sequence.

The example below shows how the Person element can be extended.

Code Sample: ReusingComponents/Demos/Book4.xsd

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:complexType name="Person">
  <xs:sequence>
   <xs:element name="FirstName" type="xs:string"/>
   <xs:element name="MiddleName" type="xs:string" minOccurs="0"/>
   <xs:element name="LastName" type="xs:string"/>
  </xs:sequence>
  <xs:attributeGroup ref="AttGroupPerson"/>
 </xs:complexType>
 <xs:complexType name="PersonExtended">
  <xs:complexContent>
   <xs:extension base="Person">
    <xs:sequence>
     <xs:element name="Specialty">
      <xs:simpleType>
       <xs:restriction base="xs:string">
        <xs:enumeration value="Mystery"/>
        <xs:enumeration value="Humor"/>
        <xs:enumeration value="Horror"/>
        <xs:enumeration value="Childrens"/>
       </xs:restriction>
      </xs:simpleType>
     </xs:element>
    </xs:sequence>
   </xs:extension>
  </xs:complexContent>
 </xs:complexType>
---- Code Omitted ----
<xs:element name="Book"> <xs:complexType> <xs:sequence> <xs:element name="Title" type="xs:string"/> <xs:element name="Author" type="PersonExtended"/> <xs:element name="Illustrator" type="Person" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Code Explanation

No material changes have been made from Book.xsd to Book4.xsd. The WinnieThePooh.xml file would be valid according to all of these schemas; however the last one would be easier to maintain and build upon than the first.

Reusing Schema Components Conclusion

In this lesson of the XML Schema tutorial, you have learned to use element and attribute groups as a way of making your code more modular. In the simple examples in this lesson, using groups did not save us much, but as schemas become bigger and more complicated, groups become an excellent way of making your schema code more modular.

To continue to learn XML Schema go to the top of this page and click on the next lesson in this XML Schema Tutorial's Table of Contents.

Use of this website implies agreement to the following:

Copyright Information

All pages and graphics on this Web site are the property of Webucator, Inc. unless otherwise specified.

None of the content on this website may be redistributed or reproduced in any way, shape, or form without written permission from Webucator, Inc.

No Printing or saving of web pages

This content may not be printed or saved. It is for online use only.


Linking to this website

You may link to any of the pages on this website; however, you may not include the content in a frame or iframe without written permission from Webucator, Inc.


Warranties

This website is provided without warranty of any kind. There are no guarantees that use of the site will not be subject to interruptions. All direct or indirect risk related to use of the site is borne entirely by the user. All code and explanations provided on this site are provided without warranties to correctness, performance, fitness, merchantability, and/or any other warranty (whether expressed or implied).

For individual private use only

You agree not to use this online manual to deliver or receive training. If you are delivering or attending a class that is making use of this online manual, you are in violation of our terms of service. Please report any abuse to courseware@webucator.com. If you would like to deliver or receive training using this manual, please fill out the form at http://www.webucator.com/Contact.cfm.