Enumerations with DTD's

Discussion in 'XML' started by Norm, Oct 28, 2005.

  1. Norm

    Norm Guest

    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
    Norm, Oct 28, 2005
    #1
    1. Advertising

  2. Norm

    Peter Flynn Guest

    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/
    Peter Flynn, Oct 29, 2005
    #2
    1. Advertising

  3. Norm

    Norm Guest

    Thank you! That is what I needed to know.

    -Norm
    Norm, Oct 31, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. clintonG

    HttpStatusCode Enumerations...

    clintonG, May 5, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    3,894
    clintonG
    May 6, 2004
  2. Joseph Tilian
    Replies:
    0
    Views:
    333
    Joseph Tilian
    Dec 21, 2004
  3. exquisitus
    Replies:
    12
    Views:
    22,296
    Tilman Bohn
    Feb 19, 2005
  4. Ronald Fischer
    Replies:
    4
    Views:
    1,737
    Ronald Fischer
    Mar 17, 2005
  5. test
    Replies:
    2
    Views:
    1,976
    Oliver Wong
    Jul 28, 2006
Loading...

Share This Page