ZVON > Tutorials > XML Schema and Relax NG Tutorial
Index | >> Example 1 / 2 << | Prev | Next |
Contents > Extensions of complexTypes > Extension of a sequence

Extension of a sequence

  1. XML Schema
  2. Relax NG
XML Schema keys: sequence, extension
Relax NG keys: define, ref

1. XML Schema

When we extend the complexType, which contains a sequence A with a sequence B, then the sequence B will be appended to sequence A.

Valid document
This document is valid, the sequence of three "x" elements is extended by a sequence of three "y" elements - so they must occur after the "x" elements.


<root xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <x>1</x>
  <x>2</x>
  <x>3</x>
  <y>1</y>
  <y>2</y>
  <y>3</y>
</root>

Invalid document
This document is not valid, the sequence of three "x" elements must come first.


<root xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <y>1</y>
  <y>2</y>
  <y>3</y>
  <x>1</x>
  <x>2</x>
  <x>3</x>
</root>

Correct XML Schema (correct_0.xsd)


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >

  <xsd:element name="root" type="BBB"/>

  <xsd:complexType name="AAA">
    <xsd:sequence>
      <xsd:element name="x" type="xsd:string" minOccurs="3" maxOccurs="3"/>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name="BBB">
    <xsd:complexContent>
      <xsd:extension base="AAA">
        <xsd:sequence>
          <xsd:element name="y" type="xsd:string" minOccurs="3" maxOccurs="3"/>
        </xsd:sequence>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>
</xsd:schema>

2. Relax NG

We can simulate the same behaviour in Relax NG using the "define" and "ref" elements.

Valid document


<root xmlns="">
  <x>1</x>
  <x>2</x>
  <x>3</x>
  <y>1</y>
  <y>2</y>
  <y>3</y>
</root>

Invalid document
This document is not valid, the sequence of three "x" elements must come first.


<root xmlns="">
  <y>1</y>
  <y>2</y>
  <y>3</y>
  <x>1</x>
  <x>2</x>
  <x>3</x>
</root>

Correct Relax NG schema (correctRelax_0.rng)


<grammar ns="" xmlns="http://relaxng.org/ns/structure/1.0" >

  <start>
    <element name="root">
      <ref name="BBB"/>
    </element>
  </start>

  <define name="AAA">
    <element name="x">
      <text/>
    </element>
    <element name="x">
      <text/>
    </element>
    <element name="x">
      <text/>
    </element>
  </define>

  <define name="BBB">
    <ref name="AAA"/>
    <element name="y">
      <text/>
    </element>
    <element name="y">
      <text/>
    </element>
    <element name="y">
      <text/>
    </element>
  </define>
</grammar>