ZVON > Tutorials > XML Schema and Relax NG Tutorial
Index | >> Example 8 / 8 << | Prev | Next |
Contents > Substitutions > Abstract type

Abstract type

  1. XML Schema
  2. Relax NG

Please, send all comments, bug-reports, and contributions to Jiri.Jirat@systinet.com. Thank you very much.

XML Schema keys: complexType, abstract

1. XML Schema

The schema below defines an abstract complexType named "myAbstractType" (attribute "abstract" is set to "true"). The type "AAA" is an extension of this type - we add the requirement that attribute "bbb" must be present. Element declaration cannot use abstract type.

Valid document


<root xsi:noNamespaceSchemaLocation="correct_0.xsd" bbb="1" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <XXX>sdkhlg nkd</XXX>
</root>

Invalid document
This document is not valid, because the attribute "bbb" is missing.


<root xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <XXX>sdkhlg nkd</XXX>
</root>

Correct XML Schema (correct_0.xsd)


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

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

  <xsd:complexType name="myAbstractType" abstract="true">
    <xsd:sequence minOccurs="1">
      <xsd:element name="XXX" type="xsd:string"/>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name="AAA">
    <xsd:complexContent>
      <xsd:extension base="myAbstractType">
        <xsd:attribute name="bbb" type="xsd:string" use="required"/>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>
</xsd:schema>

2. Relax NG

Although Relax NG does not contain such features, we can achieve the same effect with the Relax NG schema below.

Valid document


<root bbb="1" xmlns="">
  <XXX>sdkhlg nkd</XXX>
</root>

Invalid document
This document is not valid, because the attribute "bbb" is missing.


<root xmlns="">
  <XXX>sdkhlg nkd</XXX>
</root>

Correct Relax NG schema (correctRelax_0.rng)


<grammar datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes" ns="" xmlns="http://relaxng.org/ns/structure/1.0" >

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

  <define name="myAbstractType">
    <oneOrMore>
      <element name="XXX">
        <text/>
      </element>
    </oneOrMore>
  </define>

  <define name="AAA">
    <attribute name="bbb">
      <text/>
    </attribute>
    <ref name="myAbstractType"/>
  </define>
</grammar>