Convention for multiple items in an XML file

It is common in XML files, when giving a list of items, to surround them in a multiple tag. For example:

<system>
    <version>2</version>
    <title>The system</system>
    <items>
        <item id="1">one</item>
        <item id="2">two</item>
        <item id="3">three</item>
        <item id="4">four</item>
    </items>
    <link>stackoverflow.com</link>
</system>

whereas it seems more simple to me to write

<system>
    <version>2</version>
    <title>The system</system>
    <item id="1">one</item>
    <item id="2">two</item>
    <item id="3">three</item>
    <item id="4">four</item>
    <link>stackoverflow.com</link>
</system>

Is there a good reason for extra verbage in what is already a verbose form?

Upvotes: 0

Views: 2053

Answers (2)

Michael Kay
Michael Kay

Reputation: 163352

Both forms are often encountered, and there's no overwhelming benefit in either. Adding the wrapper element makes processing a bit easier in some languages. The wrapper element is also sometimes an opportunity to name the relationship, e.g. (a bad example, but the one that comes to mind)

<person name="X">
  <sons>
    <person ref="e"/>
    <person ref="f"/>
  </sons>
  <daughters>
    <person ref="p"/>
    <person ref="q"/>
  </daughters>
</person>

Upvotes: 1

ericzundel
ericzundel

Reputation: 550

Good question. I think the answer lies in how you would map it into another language or access the XML programatically. In most languages, you can create a structure that contains multiple fields with different names, or an array that contains multiple instances of the same value. It is much easier to describe <system> as a struct type and items as an array type.

Also look at the APIs you use to pull data out of a parsed XML data struture. They usually allow you to access by either name or positionally. Getting things out positionally makes the most sense when you know all the elements are of the same type.

Upvotes: 1

Related Questions