ZVON > Tutorials > XML Schema and Relax NG Tutorial
Index | >> Example 2 / 2 << | Prev | Next |
Contents > Overriding schema > xsi:type


  1. XML Schema
  2. Relax NG

1. XML Schema

We can change the type of the element by setting the value of "xsi:type" attribute. The new type must be validly derived from the overridden type. Validation Rule: Element Locally Valid (Element) .

Valid document
Element "e" is of the type "xsd:decimal".

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

Valid document
This document will be also valid, because the type "myInteger" is an integer type, which is derived from decimal type. XML Schema Part 2: Datatypes, 3.3.13 integer

<root xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
  <e xsi:type="myInteger">10</e>

Invalid document
This document is not valid, because myFloat is float-type and floats are not derived from decimals.

<root xsi:noNamespaceSchemaLocation="correct_0.xsd" xsi:nil="true" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <e1 xsi:type="myFloat">1.2e+3</e1>

Correct XML Schema (correct_0.xsd)

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

  <xsd:element name="root">
        <xsd:element name="e" type="xsd:decimal"/>

  <xsd:simpleType name="myInteger">
    <xsd:restriction base="xsd:integer"/>

  <xsd:simpleType name="myFloat">
    <xsd:restriction base="xsd:float"/>

2. Relax NG

There is no such mechanism in Relax NG.