Re: Element self-reference???

At 2000-10-06 14:15, Mike.Baker@ivans.com wrote:

>Hi All.  These are DTD syntax, but are the following constructs legal?
>
><!ELEMENT person (adr | bday | person | phone | photo)*>
>
><!ELEMENT person (adr, bday, person, phone, photo)*>
>
>In other words, is it OK for 'person' to refer to 'person' ?
>
>I would think the second one would be invalid, but how about the first one ?

Both are legal declarations in XML 1.0, and both have
legal equivalents in XML Schema 1.0.  And both have instances
which correspond to them (assuming suitable definitions for
the other element types).  This one, for example:

<!DOCTYPE test [
<!ELEMENT test (person*)>
<!ELEMENT person (adr | bday | person | phone | photo)*>
<!ELEMENT adr   (#PCDATA)>
<!ELEMENT bday  (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
<!ELEMENT photo EMPTY    >
]>
<test>
<person>
<adr>1940 West Taylor</adr>
</person>
<person>
<phone>736-5000</phone>
</person>
</test>

Or this one:

<!DOCTYPE test [
<!ELEMENT test (person*)>
<!ELEMENT person (adr, bday, person, phone, photo)*>
<!ELEMENT adr   (#PCDATA)>
<!ELEMENT bday  (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
<!ELEMENT photo EMPTY    >
]>
<test>
<person>
</person>
<person>
<adr>1600 Pennsylvania Ave.</adr>
<bday>1 January 1920</bday>
<person></person>
<phone>736-5000</phone>
<photo>
</person>
</test>


Recursion is sometimes alarming, and sometimes confusing,
and sometimes it is bad design.  But sometimes it is good
design (consider an element for representing sections of
text:  sections can nest within other sections, and one
of the most popular designs for such elements allows the
elements to self-nest), and in all cases it's perfectly legal.

The two examples you give have legal instances, because
in each case it is legal for a person element NOT to
contain another person element.  If you changed the
declaration to

  <!ELEMENT person (adr, bday, person, phone, photo)>

where the child 'person' is not optional, then the element
declaration would have no legal instances (at least, no
finite legal instances, and infinite legal instances are
probably not supported by any current processor).

But the declaration itself is still legal.  It just
declares an element which can never occur in a valid
document.  Some might wish that the language made such
constructs illegal, but there are two reasons for NOT
making them illegal:  (1) it is not always quite so
obvious as in the example above whether a declaration can
have legal instances, and (2) sometimes one might WANT to
declare such elements (a rather specialized need, but
potentially a real one).

I hope this helps.

-C. M. Sperberg-McQueen
  World Wide Web Consortium

Received on Friday, 6 October 2000 23:01:57 UTC