ZVON > Tutorials > XML Schema and Relax NG Tutorial
Index | >> Example 5 / 8 << | Prev | Next |
Contents > Substitutions > XML Schema - blockDefault attribute

XML Schema - blockDefault attribute

  1. blockDefault set to "#all"
  2. blockDefault set to "restriction"
  3. blockDefault set to "extension"
XML Schema keys: blockDefault

1. blockDefault set to "#all"

The "blockDefault" attribute is set to "#all" (thus forbidding substitution, extension, restriction), the document is not valid.

Valid document


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

Invalid document


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

Invalid document


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

Invalid document


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

Correct XML Schema (correct_0.xsd)


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

  <xsd:element name="root">
    <xsd:complexType>
      <xsd:sequence minOccurs="1">
        <xsd:element ref="C"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <xsd:complexType name="AAA">
    <xsd:simpleContent>
      <xsd:extension base="xsd:integer"/>
    </xsd:simpleContent>
  </xsd:complexType>

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

  <xsd:element name="D" substitutionGroup="C">
    <xsd:complexType>
      <xsd:simpleContent>
        <xsd:extension base="AAA">
          <xsd:attribute name="bbb" type="xsd:string"/>
        </xsd:extension>
      </xsd:simpleContent>
    </xsd:complexType>
  </xsd:element>

  <xsd:element name="E" type="AAA" substitutionGroup="C"/>

  <xsd:element name="F" substitutionGroup="C">
    <xsd:complexType>
      <xsd:simpleContent>
        <xsd:restriction base="AAA">
          <xsd:minInclusive value="0"/>
        </xsd:restriction>
      </xsd:simpleContent>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

2. blockDefault set to "restriction"

The "blockDefault" attribute is set to "restriction". Because the type of element "F" is derived by a restriction from the type "AAA" (type "AAA" is the type of the substituted C element) and this would be in conflict with the "blockDefault" attribute, we can't use the element "F".

Valid document


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

Valid document


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

Valid document


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

Invalid document


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

Correct XML Schema (correct_0.xsd)


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

  <xsd:element name="root">
    <xsd:complexType>
      <xsd:sequence minOccurs="1">
        <xsd:element ref="C"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <xsd:complexType name="AAA">
    <xsd:simpleContent>
      <xsd:extension base="xsd:integer"/>
    </xsd:simpleContent>
  </xsd:complexType>

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

  <xsd:element name="D" substitutionGroup="C">
    <xsd:complexType>
      <xsd:simpleContent>
        <xsd:extension base="AAA">
          <xsd:attribute name="bbb" type="xsd:string"/>
        </xsd:extension>
      </xsd:simpleContent>
    </xsd:complexType>
  </xsd:element>

  <xsd:element name="E" type="AAA" substitutionGroup="C"/>

  <xsd:element name="F" substitutionGroup="C">
    <xsd:complexType>
      <xsd:simpleContent>
        <xsd:restriction base="AAA">
          <xsd:minInclusive value="0"/>
        </xsd:restriction>
      </xsd:simpleContent>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

3. blockDefault set to "extension"

The "blockDefault" attribute is set to "extension". Because the type of element "D" is derived by an extension from the type "AAA" (type "AAA" is the type of the substituted C element) and this would be in conflict with the "blockDefault" attribute, we can't use the element "D".

Valid document


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

Invalid document


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

Valid document


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

Valid document


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

Correct XML Schema (correct_0.xsd)


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

  <xsd:element name="root">
    <xsd:complexType>
      <xsd:sequence minOccurs="1">
        <xsd:element ref="C"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <xsd:complexType name="AAA">
    <xsd:simpleContent>
      <xsd:extension base="xsd:integer"/>
    </xsd:simpleContent>
  </xsd:complexType>

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

  <xsd:element name="D" substitutionGroup="C">
    <xsd:complexType>
      <xsd:simpleContent>
        <xsd:extension base="AAA">
          <xsd:attribute name="bbb" type="xsd:string"/>
        </xsd:extension>
      </xsd:simpleContent>
    </xsd:complexType>
  </xsd:element>

  <xsd:element name="E" type="AAA" substitutionGroup="C"/>

  <xsd:element name="F" substitutionGroup="C">
    <xsd:complexType>
      <xsd:simpleContent>
        <xsd:restriction base="AAA">
          <xsd:minInclusive value="0"/>
        </xsd:restriction>
      </xsd:simpleContent>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>