enumeration

This should cover W3C XML Schema, Relax NG and DTD related problems.
plebel
Posts: 3
Joined: Sun Mar 20, 2011 8:06 pm

enumeration

Post by plebel »

I'm an XML newbie! So I appreciate any help you can give me.

I want to know if there's a way to populate an attribute enumeration
from a list - perhaps from a "keyref"? from some other element.

Pierre
sorin_ristache
Posts: 4144
Joined: Fri Mar 28, 2003 2:12 pm

Re: enumeration

Post by sorin_ristache »

Hello,

An attribute value can be an enumeration of values that are extracted from other element. Can you give more details about the problem, maybe an example of an attribute and the location of the enumeration of values that you need to put in the attribute?


Regards,
Sorin
plebel
Posts: 3
Joined: Sun Mar 20, 2011 8:06 pm

Re: enumeration

Post by plebel »

Sorin, thanks for your interest,
here's an example where I would want the enumeration for the attribute "Atype_Name" to contain a list of what the user has input for "NameA":

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="Base">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="A">
<xs:complexType>
<xs:attribute name="NameA" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element maxOccurs="unbounded" name="B">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Atype">
<xs:complexType>
<xs:attribute name="Atype_Name">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="ChooseAnames"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="NameB"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
sorin_ristache
Posts: 4144
Joined: Fri Mar 28, 2003 2:12 pm

Re: enumeration

Post by sorin_ristache »

Do you want to restrict the values that the user is allowed to enter as value for the NameA attribute and keep a list of the values entered by the user in the Atype_Name attribute? In this case you should have a schema like the following. Otherwise please give an example of XML document that is valid and another one that is invalid.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="Base">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="A">
<xs:complexType>
<xs:attribute name="NameA" type="EnumerationOfNames"/>
</xs:complexType>
</xs:element>
<xs:element maxOccurs="unbounded" name="B">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Atype">
<xs:complexType>
<xs:attribute name="Atype_Name">
<xs:simpleType>
<xs:list itemType="EnumerationOfNames"/>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="NameB"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:simpleType name="EnumerationOfNames">
<xs:restriction base="xs:string">
<xs:enumeration value="allowed_value_1"/>
<xs:enumeration value="allowed_value_2"/>
<xs:enumeration value="allowed_value_3"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>

Regards,
Sorin
plebel
Posts: 3
Joined: Sun Mar 20, 2011 8:06 pm

Re: enumeration

Post by plebel »

Soren,
thanks for looking at this.
I didn't want to restrict the contents of NameA.
I did want to restrict the contents of "Atype_Name" to any of the values that actually got used for the NameA.
sorin_ristache
Posts: 4144
Joined: Fri Mar 28, 2003 2:12 pm

Re: enumeration

Post by sorin_ristache »

plebel wrote:I did want to restrict the contents of "Atype_Name" to any of the values that actually got used for the NameA.
Please give one or two examples of valid and invalid XML documents for the restriction that you want to encode in the XML Schema.

If you to set some values in a restriction in an XML Schema you have to know/decide these values in advance, before creating any XML instance documents for that schema. If you want to allow in "Atype_Name" any value that is used in "NameA" what is it that you want to restrict? I think some examples of XML documents will clarify that. Let's start from a few examples of XML instance documents and based on them we can create an XML Schema that can validate such XML documents.


Regards,
Sorin
Post Reply