PDA

View Full Version : XML tag nesting: repeat names ok?


Erich
11-05-2004, 11:42 PM
can I have a tag nested in a tag of the same name?


<year id="2001">
<show id="20010507">
<date id="05/07/01">
<month>May</month>
<day>07</day>
<year>2001</year>
</date>
<artist solo="Acoustic">Tim Solo</artist>
<location>
<venue>The Wetlands</venue>
<city>New York City</city>
<state>NY</state>
</location>
</show>
<show> ... </show>
<show> ... </show>
</year>


or is it better formed to changed the second year tag to something like "y"

gsnedders
11-05-2004, 11:58 PM
You could, but it could get confusing when changing the XML...

Erich
11-06-2004, 12:10 AM
You could, but it could get confusing when changing the XML...

thanks, i'll switch it to m d and y for date

liorean
11-06-2004, 12:12 AM
Well, the correct term is element. An element consists of the start tag, the content and the end tag; or alternatively for empty elements only a self terminating tag (or more commonly called empty tag).

Yes, you can nest an element within the same element. Take e.g. XHTML: the div element may be nested freely withing other div elements without problems. However, the p element may not be nested within p elements. This is controlled on a per-element basis in the DTD, which determines among other things what content model an element has.

However, on the other hand, we have the head and body elements of XHTML for describing the document head and body, as well as the thead and tbody elements for describing the table row groups. Now, the reason for these having different names is that they have different semantics and grammar - the content model, attribute list and default styling and behavior is different; and they have radically different meaning. In XML elements with a certain name have the same properties indifferent of where they are in the syntax tree.

Thus you should, judging by your example, probably consider giving the nested element another name instead.