ZVON > Tutorials > XML Schema and Relax NG Tutorial
Index | >> Example 5 / 10 << | Prev | Next |
Contents > Simple types > Element can contain a string from an enumerated set

Element can contain a string from an enumerated set

  1. XML Schema
  2. Relax NG
XML Schema keys: enumeration
Relax NG keys: choice

1. XML Schema

Now, we want the element "root" to have a value "N/A" or "#REF!".

Valid document


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

Valid document


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

Invalid document


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

Correct XML Schema (correct_0.xsd)


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

  <xsd:element name="root">
    <xsd:simpleType>
      <xsd:restriction base="xsd:string">
        <xsd:enumeration value="N/A"/>
        <xsd:enumeration value="#REF!"/>
      </xsd:restriction>
    </xsd:simpleType>
  </xsd:element>
</xsd:schema>

2. Relax NG

As in previous example, we will use the "choice" element.

Valid document


<root xmlns="">N/A</root>

Valid document


<root xmlns="">#REF!</root>

Invalid document


<root xmlns="">10</root>

Correct Relax NG schema (correctRelax_0.rng)


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

  <start>
    <element>
      <name ns="">root</name>
      <choice>
        <value>N/A</value>
        <value>#REF!</value>
      </choice>
    </element>
  </start>
</grammar>