ZVON > Tutorials > XML Schema and Relax NG Tutorial
Index | >> Example 7 / 8 << | Prev | Next |
Contents > Substitutions > Attribute block and xsi:type

Attribute block and xsi:type

  1. XML Schema
XML Schema keys: block

1. XML Schema

The "block" attribute can be useful to prevent overriding of our schema with "xsi:type" attribute in the document.

Valid document

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

Invalid document
The document below is not valid, because: we have tried to change the type of the element "root" from "AAA" to "BBB", the type "BBB" is created by an extension from the "AAA" type and that's not allowed (the attribute "block" is set to "extension").

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

Correct XML Schema (correct_0.xsd)

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

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

  <xsd:complexType name="AAA">
    <xsd:attribute name="bbb" type="xsd:string" use="required"/>

  <xsd:complexType name="BBB">
      <xsd:extension base="AAA">
        <xsd:attribute name="ccc" type="xsd:string" use="required"/>