ZVON > Tutorials > XML Schema and Relax NG Tutorial
Index | >> Example 4 / 4 << | Prev | Next |
Contents > Importing and combining multiple schemas > Redefine element

Redefine element

  1. XML Schema
XML Schema keys: redefine

1. XML Schema

In this example, we redefine the type "myString" from an arbitrary string to a string which cannot be more than 5 characters long. We will do this using redefine element.

Valid document
The file is valid, the string is not longer than 5 characters.


<foo:root xsi:schemaLocation="http://foo correct_1.xsd" xmlns:foo="http://foo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bar="http://bar" >abcde</foo:root>

Invalid document
The file is not valid, because the string is longer than 5 characters.


<foo:root xsi:schemaLocation="http://foo correct_1.xsd" xmlns:foo="http://foo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bar="http://bar" >abcdef</foo:root>

Correct XML Schema (correct_0.xsd)


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

  <xsd:element name="root" type="foo:myString"/>

  <xsd:simpleType name="myString">
    <xsd:restriction base="xsd:string"/>
  </xsd:simpleType>
</xsd:schema>

Correct XML Schema (correct_1.xsd)
In the redefining schema, the type definitions must use themselves as their base type definition. Thus the base type for the definition of "myString" must be "myString" itself.


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

  <xsd:redefine schemaLocation="correct_0.xsd">
    <xsd:simpleType name="myString">
      <xsd:restriction base="foo:myString">
        <xsd:maxLength value="5"/>
      </xsd:restriction>
    </xsd:simpleType>
  </xsd:redefine>
</xsd:schema>