Enumerations with DTD's

N

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
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
 
P

Peter Flynn

Norm said:
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>
<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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top