Discussion:
Enumerations with DTD's
(too old to reply)
Norm
2005-10-28 18:33:53 UTC
Permalink
I am creating an XML DTD, and I want to have an element that is
constrained to a set of enumerated values. My idea is to write my DTD
like this:

<!ELEMENT testEnum (mode)>
<!ELEMENT mode (mode_enabled | mode_disabled | mode_other)>
<!ELEMENT mode_enabled EMPTY>
<!ELEMENT mode_disabled EMPTY>
<!ELEMENT mode_other EMPTY>

Then an example XML document could look like this:

<?xml version="1.0" encoding="US-ASCII" standalone="yes"?>
<!DOCTYPE test>

<testEnum>
<mode>
<mode_disabled />
</mode>
</testEnum>

Is this a good way to do it? (Is there a standard way?) I welcome any
comments about it.

BTW, I am an XML newbie.

Thanks,
Norm
Peter Flynn
2005-10-29 21:27:42 UTC
Permalink
Post by Norm
I am creating an XML DTD, and I want to have an element that is
constrained to a set of enumerated values. My idea is to write my DTD
<!ELEMENT testEnum (mode)>
<!ELEMENT mode (mode_enabled | mode_disabled | mode_other)>
<!ELEMENT mode_enabled EMPTY>
<!ELEMENT mode_disabled EMPTY>
<!ELEMENT mode_other EMPTY>
<?xml version="1.0" encoding="US-ASCII" standalone="yes"?>
<!DOCTYPE test>
You would need to specify where the processor is to find this object
"test", either by giving a SYSTEM URI for the DTD file, or by
providing the DTD in an internal subset, eg

<!DOCTYPE testEnum [
<!ELEMENT testEnum (mode)>
<!ELEMENT mode (mode_enabled | mode_disabled | mode_other)>
<!ELEMENT mode_enabled EMPTY>
<!ELEMENT mode_disabled EMPTY>
<!ELEMENT mode_other EMPTY>
]>
Post by Norm
<testEnum>
<mode>
<mode_disabled />
</mode>
</testEnum>
Is this a good way to do it? (Is there a standard way?)
Yes, this works fine. There really isn't any other way to do this,
if you need the data structure to use elements (for example, if you
plan on giving mode_enabled etc some attributes or content of their
own).

An alternative is to use an attribute on the parent element type, eg

<!DOCTYPE testEnum [
<!ELEMENT testEnum (mode)>
<!ELEMENT mode EMPTY>
<!ATTLIST mode enabled (yes|no|other) #REQUIRED>
]>

This now lets you write

<testEnum>
<mode enabled="yes"/>
</testEnum>

You could make the #REQUIRED into a default value (eg "yes") if that
suits the data.

///Peter
--
XML FAQ: http://xml.silmaril.ie/
Norm
2005-10-31 13:50:10 UTC
Permalink
Thank you! That is what I needed to know.

-Norm

Loading...