Search code examples
stringtypesenumerationrelaxng

How to define a type with enumerated strings in RELAX NG?


Still learning RELAX NG (wondering whether I should learn XSD instead), I got my element hierarchy mostly right, so I'm working on contents:

One element (Unit) uses an enumeration type (a fixed set of strings), like this

Byte | KiloByte | MegaByte | GigaByte | TeraByte

So I'd like to define the corresponding type in RELAX NG, but fail to apply the rules from datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes". I have this so far:

  <define name="DT.Unit">
    <a:documentation>Data Type Unit: A fixed set of strings</a:documentation>
    <data datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"
          type="normalizedString">
      <!-- how to define the set of strings allowed? -->
    </data>
  </define>

Should I use a pattern, or is it possible to enumerate the strings in another way?

I don't know how to apply the XSD definition to RELAX NG.


Solution

  • As I couldn't get an enumeration based on normalizedString working, I droppend the idea of using <data datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes" type="normalizedString">.

    Instead I used <choice> and a list of <value type="string"> elements. This is the solution that worked for me:

    <define name="C.Size">
      <a:documentation>Choice: Size, a fixed set of strings</a:documentation>
      <choice>
        <value type="string">Byte</value>
        <value type="string">KiloByte</value>
        <value type="string">MegaByte</value>
        <value type="string">GigaByte</value>
        <value type="string">TeraByte</value>
      </choice>
    </define>