XML Schema enumeration question

J

Justin Wright

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
 
J

Jens M. Felderhoff

Justin Wright said:
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

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

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>

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top