Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DTD required elements ordering

Tags:

xml

dtd

I want to have list of required elements in any order defined in dtd, but have no idea how I could do this.

For example, I have following definition:

<!ELEMENT parent (child1, child2, child3)>

This dtd declaration will successfully validate following piece of xml:

<parent>
   <child1></child1>
   <child2></child2>
   <child3></child3>
</parent>

But in following case xml will not pass validation:

<parent>
   <child2></child2>
   <child1></child1>
   <child3></child3>
</parent>

One of the possible solution is to declare

<!ELEMENT parent (child1 | child2 | child3)>

But in this case one of the childs might be missing though validation will be successful.

I need correct dtd element declaration for the case when list of required elements can be present in any order.

like image 696
altern Avatar asked Jun 11 '10 12:06

altern


People also ask

What is DTD explain various elements and attributes in DTD?

The DTD attributes start with <! ATTLIST keyword if the element contains the attribute. element-name specifies the name of the element to which the attribute applies. attribute-name specifies the name of the attribute which is included with the element-name.

How elements and attributes are declared in a DTD?

In a DTD, attributes are declared with an ATTLIST declaration.

What is the need of DTD?

The purpose of a DTD is to define the legal building blocks of an XML document. It defines the document structure with a list of legal elements. A DTD can be declared inline in your XML document, or as an external reference.


1 Answers

ChrisF is wrong to say you can't do this (but kudos for checking the spec!); DevNull [now known as Daniel Haley] is right to say you can.

There is, however, one complication: the content model given by DevNull for parent violates the determinism rules of XML. Informally, these rules say the parser must know, without lookahead, which token in the content model each element in the document matches. If in a parent element the parser sees a child1, it can't know without lookahead whether it has just matched the first or second occurrence of child1 in DevNull's content model. This is an error (but as it happens it's an error that processors are not obligated to report -- it's an error that means "all bets are off, the processor may do anything here, including fail to notice anything is wrong").

A content model for parent that obeys the determinism rules can be formed by factoring out common prefixes, thus:

<!ELEMENT parent ( 
                   (child1, ( (child2,child3)
                            | (child3,child2)))
                 | (child2, ( (child1,child3)
                            | (child3,child1)))
                 | (child3, ( (child1,child2)
                            | (child2,child1)))
                 ) >

This is less convenient than declarations for this model can be in other schema languages, and for more than three elements it's error prone and extremely tedious to do by hand. But it's not impossible to declare the content model you want with DTDs. Or perhaps I should say it's impossible only for DTD authors who are incapable of putting up with any inconvenience.

Note also that unless the sequence of children carries some information, some design authorities argue strongly that it's better to fix a sequence.

like image 134
C. M. Sperberg-McQueen Avatar answered Sep 17 '22 18:09

C. M. Sperberg-McQueen