Unique XML sub-elements
November 13, 2006 9:04 AM   Subscribe

Do XML sub-elements (or children or sub tags) have to be unique?
posted by four panels to Technology (6 answers total)
 
No. This is fine:
<a>
<b/>
<b/>
</a>

Attributes do need to be unique. This is not fine:
<a b="" b="">
posted by cillit bang at 9:06 AM on November 13, 2006


(This is XML itself. Formats based on XML may impose additional rules that do require uniqueness)
posted by cillit bang at 9:08 AM on November 13, 2006


If the XML uses id attributes, then they must be unique. This is bad:


<a>
<b/ id="monkey">
<b/ id="monkey">
</a>

posted by MonkeySaltedNuts at 9:15 AM on November 13, 2006


Generally, no, but it if you are validating against a DTD/schema you can specify - e.g. for a DTD:

ELEMENT a (b) -- a contains only one b
ELEMENT a (b*) -- a contains 0-n b
ELEMENT a (b?) -- a contains 0 or 1 b
ELEMENT a (b+) -- a contains 1 or more b
posted by azlondon at 9:16 AM on November 13, 2006


I've used XML Schemas before and you can specify whether an element is required, optional and how many times it can be used... unfortunately it was a while ago, I've forgotten, and I can't find a useful reference at the moment.

But, in summary: basic XML makes no restrictions. A schema definition or DTD provides the structural definitions that restrict what's allowable in a specific type of XML document.
posted by GuyZero at 9:40 AM on November 13, 2006


Think about your normal usage, as "children" of the root node.
posted by cmiller at 11:34 AM on November 13, 2006


« Older i'm on my time with everyone; i have very bad...   |   Roommate/Co-worker issues Newer »
This thread is closed to new comments.