1

How would you define a DTD statement from an XML document where a specific element has an attribute, and depending on this attribute, has differing child elements.

For example in this case:

<tv_program>
<!-- Various elements here -->
<program type = "live chat">
<presenter></presenter>
<guests></guests>
</program>
<program type = "documentary">
<short_descrip></short_descrip>
</program>
<program type = "film">
<title></title>
<genre></genre>
<rating></rating>
</program>
</tv_program> 

What would be the structure of declaring the child elements (presenter, guest, etc.) within each specific attribute type (live chat, documentary, film) in a DTD?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
st3lz
  • 39
  • 7

1 Answers1

0

What you're describing (now) goes by the name of Conditional Type Assignment. It is not possible to do in DTD. It is not even possible to do with XSD 1.0. You need XSD 1.1 to do Conditional Type Assignment, e.g. to let a content model depend upon the value of an attribute.

See this answer for an example of CTA in XSD 1.1.

If you're stuck with DTD or XSD 1.0, you'll have to

  1. Perform such checking outside of your schema, or
  2. Disambiguate your element types via their names rather than @type attributes.

Go with option #2 if possible; it's considered to be a best practice in XML to name different elements differently.

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Thank you very much! After much time spent on this I wondered if it was even possible myself. – st3lz May 16 '15 at 11:23