XML Schema: Create Variable Content Container Using Abstract Type

Implementation

* Define an abstract base type (PublicationType) to include common elements:

<xsd:complexType name="PublicationType" abstract="true">
  <xsd:sequence>
    <xsd:element name="Title" type="xsd:string"/>
    <xsd:element name="Author" type="xsd:string"
      minOccurs="0" maxOccurs="unbounded"/>
    <xsd:element name="Date" type="xsd:gYear"/>
  </xsd:sequence>
</xsd:complexType>

* Define concrete types(BookType, MagazineType) to extend abstract type

<xsd:complexType name="BookType">
  <xsd:complexContent>
    <xsd:extension base="PublicationType">
      <xsd:sequence>
        <xsd:element name="ISBN" type="xsd:string"/>
        <xsd:element name="Publisher" type="xsd:string"/>
      </xsd:sequence>
    </xsd:extension>
  </xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="MagazineType">
  <xsd:complexContent>
    <xsd:restriction base="PublicationType">
      <xsd:sequence>
        <xsd:element name="Title" type="xsd:string"/>
        <xsd:element name="Author" type="xsd:string"
          minOccurs="0" maxOccurs="0"/>
        <xsd:element name="Date" type="xsd:gYear"/>
      </xsd:sequence>
    </xsd:restriction>
  </xsd:complexContent>
</xsd:complexType>

* Define container element (Catalogue) to contain an element (Publication) of the abstract type (PublicationType):

<xsd:element name="Catalogue">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="Publication"
        type="PublicationType"
        maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>

* Example

<?xml version="1.0"?>
<Catalogue xmlns="http://www.catalogue.org"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation=
  "http://www.catalogue.org
  Catalogue.xsd">
  <Book>
    <Title>Illusions The Adventures of a Reluctant Messiah</Title>
    <Author>Richard Bach</Author>
    <Date>1977</Date>
    <ISBN>0-440-34319-4</ISBN>
    <Publisher>Dell Publishing Co.</Publisher>
  </Book>
  <Magazine>
    <Title>Natural Health</Title>
    <Date>1999</Date>
  </Magazine>
  <Book>
    <Title>The First and Last Freedom</Title>
    <Author>J. Krishnamurti</Author>
    <Date>1954</Date>
    <ISBN>0-06-064831-7</ISBN>
    <Publisher>Harper &amp; Row</Publisher>
  </Book>
</Catalogue>

Extend the schema

* Add a new concrete type (CDType)

<include schemaLocation="Catalogue.xsd"/>
<complexType name="CDType">
  <complexContent>
    <extension base="PublicationType" >
      <sequence>
        <element name="RecordingCompany" type="string"/>
      </sequence>
    </extension>
  </complexContent>
</complexType>

* Example

<?xml version="1.0"?>
<Catalogue xmlns="http://www.catalogue.org"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation=
  "http://www.catalogue.org
  Catalogue.xsd">
  <Book>
    <Title>Illusions The Adventures of a Reluctant Messiah</Title>
    <Author>Richard Bach</Author>
    <Date>1977</Date>
    <ISBN>0-440-34319-4</ISBN>
    <Publisher>Dell Publishing Co.</Publisher>
  </Book>
  <Magazine>
    <Title>Natural Health</Title>
    <Date>1999</Date>
  </Magazine>
  <Book>
    <Title>The First and Last Freedom</Title>
    <Author>J. Krishnamurti</Author>
    <Date>1954</Date>
    <ISBN>0-06-064831-7</ISBN>
    <Publisher>Harper &amp; Row</Publisher>
  </Book>
  <CD>
    <Title>Timeless Serenity</Title>
    <Author>Dyveke Spino</Author>
    <Date>1984</Date>
    <RecordingCompany>Dyveke Spino Productions</RecordingCompany>
  </CD>
</Catalogue>

Reference

* http://www.xfront.com/VariableContentContainers.pdf

This entry was posted in xml. Bookmark the permalink.