[DTD] defining elements depending on attribute

Discussion in 'XML' started by Ekim, Dec 1, 2004.

  1. Ekim

    Ekim Guest

    hello,

    I wanna create a new DTD for a protocol - therefore I have one question:

    is there a way to define elements depending on a special attribute-value?
    I guess I've confused you so far, so that's what I really mean:

    I have got an element "sensor":
    <!ELEMENT sensor ((longitude, latitude) | (direction, speed))>

    This element has an attribute "type":
    <!ATTLIST sensor type (GPS | WIND) "GPS">

    My question now is, if it is possible that the sub-elements of "sensor" are
    "longitude, latitude" only in the case when the type-attribute = "GPS".
    When the type-attribute = "WIND", the sub-elements shall exactly be
    "direction, speed".

    Currently it doesn't matter which value the type-attribute actually has -
    the subelements can either be (longitude, latitude) OR (direction, speed).
    But I want them only when the attribute is set accordingly.

    Do you have an idea how to do that? Is there a way in DTD to specify such
    things?

    I appreciate any help,

    ekim
     
    Ekim, Dec 1, 2004
    #1
    1. Advertising

  2. On Wed, 1 Dec 2004 21:57:06 +0100, "Ekim" <>
    wrote:

    >I wanna create a new DTD for a protocol - therefore I have one question:
    >is there a way to define elements depending on a special attribute-value?
    >I guess I've confused you so far, so that's what I really mean:
    >
    >I have got an element "sensor":
    ><!ELEMENT sensor ((longitude, latitude) | (direction, speed))>
    >
    >This element has an attribute "type":
    ><!ATTLIST sensor type (GPS | WIND) "GPS">
    >
    >My question now is, if it is possible that the sub-elements of "sensor" are
    >"longitude, latitude" only in the case when the type-attribute = "GPS".
    >When the type-attribute = "WIND", the sub-elements shall exactly be
    >"direction, speed".


    No. An attribute specification can not be directly used to specify the
    content model of an element that the attribute belongs to.

    Chose a different approach to your problem. (Architectural processing
    comes to mind but I have not investigated that approach any further at
    this time)

    --
    Rex
     
    Jan Roland Eriksson, Dec 1, 2004
    #2
    1. Advertising

  3. Ekim

    Arjun Ray Guest

    On Wed, 01 Dec 2004 21:57:06 +0100, Ekim wrote:

    > I have got an element "sensor":
    > <!ELEMENT sensor ((longitude, latitude) | (direction, speed))>
    >
    > This element has an attribute "type": <!ATTLIST sensor type (GPS | WIND)
    > "GPS">
    >
    > My question now is, if it is possible that the sub-elements of "sensor"
    > are "longitude, latitude" only in the case when the type-attribute =
    > "GPS". When the type-attribute = "WIND", the sub-elements shall exactly be
    > "direction, speed".


    No, it is not possible.

    A lot of the time, a "type" attribute is a good indication of a flawed
    design, typically where the element type - hey, note that word! - is too
    general for the intended structural purpose.

    You can have what you want by replacing the "sensor" element type, which
    is too general, with two more specific element types, e.g.

    <!ELEMENT gps-sensor (longitude, latitude) >
    <!ELEMENT wind-sensor (direction, speed) >

    And wherever 'sensor' appears in a content model, replace it with

    sensor => (gps-sensor | wind-sensor)

    Note also that you can dispense with the 'type' attribute, too.
     
    Arjun Ray, Dec 12, 2004
    #3
  4. Ekim

    Peter Flynn Guest

    Arjun Ray wrote:

    > On Wed, 01 Dec 2004 21:57:06 +0100, Ekim wrote:
    >
    >> I have got an element "sensor":
    >> <!ELEMENT sensor ((longitude, latitude) | (direction, speed))>
    >>
    >> This element has an attribute "type": <!ATTLIST sensor type (GPS | WIND)
    >> "GPS">
    >>
    >> My question now is, if it is possible that the sub-elements of "sensor"
    >> are "longitude, latitude" only in the case when the type-attribute =
    >> "GPS". When the type-attribute = "WIND", the sub-elements shall exactly
    >> be "direction, speed".

    >
    > No, it is not possible.
    >
    > A lot of the time, a "type" attribute is a good indication of a flawed
    > design, typically where the element type - hey, note that word! - is too
    > general for the intended structural purpose.
    >
    > You can have what you want by replacing the "sensor" element type, which
    > is too general, with two more specific element types, e.g.
    >
    > <!ELEMENT gps-sensor (longitude, latitude) >
    > <!ELEMENT wind-sensor (direction, speed) >
    >
    > And wherever 'sensor' appears in a content model, replace it with
    >
    > sensor => (gps-sensor | wind-sensor)
    >
    > Note also that you can dispense with the 'type' attribute, too.


    Even easier,

    <!ELEMENT sensor (wind|gps)>
    <!ELEMENT wind (direction|speed)>
    <!ELEMENT gps (longitude|latitude)>

    ///Peter
    --
    "The cat in the box is both a wave and a particle"
    -- Terry Pratchett, introducing quantum physics in _The Authentic Cat_
     
    Peter Flynn, Dec 18, 2004
    #4
    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. Joseph Tilian
    Replies:
    0
    Views:
    356
    Joseph Tilian
    Dec 21, 2004
  2. Ronald Fischer
    Replies:
    4
    Views:
    1,763
    Ronald Fischer
    Mar 17, 2005
  3. Replies:
    2
    Views:
    1,081
    Henry S. Thompson
    Mar 6, 2006
  4. johny smith
    Replies:
    8
    Views:
    422
    Peter Koch Larsen
    Jul 2, 2004
  5. test
    Replies:
    2
    Views:
    2,051
    Oliver Wong
    Jul 28, 2006
Loading...

Share This Page