Norm wrote:
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>
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>
]>
<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/