XML Schema enumeration question

Discussion in 'XML' started by Justin Wright, Jan 23, 2004.

  1. I know that I can set up an enumeration as follows ( just typed in quick so
    may have syntax errors ):

    <xsd:simpleType name="colors">
    <xsd:restriction base="xsd:string">
    <xsd:enumeration value="red"/>
    <xsd:enumeration value="yellow"/>
    <xsd:enumeration value="blue"/>
    </xsd:simpleType>

    However, what I need to do is associate each element of the "colors"
    enumeration with an integer as well. For example, a string value of "red"
    or an int of 10 should be treated the same:

    String int value
    red 10
    yellow 20
    blue 30

    Is there any way to assign the int value to the enumeration? Is the "id"
    attribute what I should be using? For example:

    <xsd:simpleType name="colors">
    <xsd:restriction base="xsd:string">
    <xsd:enumeration value="red" id="10"/>
    <xsd:enumeration value="yellow" id="20"/>
    <xsd:enumeration value="blue" id="30"/>
    </xsd:simpleType>

    Any help would be appreciated!

    Thanks,
    Justin
    Justin Wright, Jan 23, 2004
    #1
    1. Advertising

  2. "Justin Wright" <> wrote:

    > I know that I can set up an enumeration as follows ( just typed in quick so
    > may have syntax errors ):
    >
    > <xsd:simpleType name="colors">
    > <xsd:restriction base="xsd:string">
    > <xsd:enumeration value="red"/>
    > <xsd:enumeration value="yellow"/>
    > <xsd:enumeration value="blue"/>


    </xsd:restriction>

    > </xsd:simpleType>
    >
    > However, what I need to do is associate each element of the "colors"
    > enumeration with an integer as well. For example, a string value of "red"
    > or an int of 10 should be treated the same:
    >
    > String int value
    > red 10
    > yellow 20
    > blue 30


    because the "colors" simple type is a restriction of xsd:string, you
    can define the values of the enumerations like:

    <xsd:enumeration value="red:10"/>

    > Is there any way to assign the int value to the enumeration? Is the "id"
    > attribute what I should be using? For example:
    >
    > <xsd:simpleType name="colors">
    > <xsd:restriction base="xsd:string">
    > <xsd:enumeration value="red" id="10"/>
    > <xsd:enumeration value="yellow" id="20"/>
    > <xsd:enumeration value="blue" id="30"/>


    </xsd:restriction>

    > </xsd:simpleType>


    No, the value of the id attribute must match a NCName production
    (http://www.w3.org/TR/xmlschema-2/#ID), i.e., it must start with
    either a letter or an underscore.

    You may define a colormap and use xsd:key/xsd:keyref/xsd:unique:

    <xsd:simpleType name="colorname">
    <xsd:restriction base="xsd:string>
    <xsd:enumeration value="red"/>
    <xsd:enumeration value="yellow"/>
    <xsd:enumeration value="blue"/>
    </xsd:restriction>
    </xsd:simpleType>

    <xsd:element name="color">
    <xsd:complexType>
    <xsd:attribute name="name" type="colorname"/>
    <xsd:attribute name="value">
    <xsd:simpleType>
    <xsd:restriction base="xsd:int>
    <xsd:enumeration value="10"/>
    <xsd:enumeration value="20"/>
    <xsd:enumeration value="30"/>
    </xsd:restriction>
    </xsd:simpleType>
    </xsd:attribute>
    </xsd:complexType>
    </xsd:element>

    <xsd:element name="colormap">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element ref="color" maxOccurs="unbounded"/>
    </xsd:sequence
    </xsd:complexType>

    <!-- make sure that color values and names are unique -->
    <xsd:unique name="color-value">
    <xsd:selector xpath="color"/>
    <xsd:field xpath="@value"/>
    </xsd:unique>
    <xsd:unique name="color-name">
    <xsd:selector xpath="color"/>
    <xsd:field xpath="@name"/>
    </xsd:unique>
    <xsd:key name="colormap">
    <xsd:selector xpath="color"/>
    <xsd:field xpath="@name"/>
    <xsd:field xpath="@value"/>
    </xsd:key>
    </xsd:element>

    <xsd:element name="wrapper">
    ....
    <xsd:element ref="colormap"/>
    ....
    <xsd:element ref="item-using-colors"/>
    ....
    <xsd:keyref name="color-used" refer="color-name">
    <xsd:selector xpath="item-using-colors"/>
    <xsd:field xpath="@color"/>
    </xsd:keyref>
    </xsd:element>

    Cheers

    Jens
    --
    (Intentionally left blank.)
    Jens M. Felderhoff, Jan 24, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page