When specifying XML formats that contain a list of items, there is often a choice of at least two distinct styles. One uses a container element for the list, the other doesn't. As an example: if specifying a document with multiple pages, one could do this:
<document>
<title>...</title>
<pages>
<page>...</page>
<page>...</page>
</pages>
</document>
Or just this:
<document>
<title>...</title>
<page>...</page>
<page>...</page>
</document>
What are the pros and cons of each approach?
Some I can think of are:
EDIT
To clarify: I am assuming there is no meaning attached to the pages
element. There are no other elements inside, no attributes attached and it is hard to find any other name than 'pages', 'pageList' or similar for it.
EDIT 2
I found another entry about the same question. While the answer are all for the container/parent element, it seems to come down to treating the container as actual object or the assumption that it is easier to model a schema by having that extra container element (which I tend to disagree with).
The container element is the document element for the META-INF/container. xml file. The container describes the starting point for the MusicXML version of the file, as well as alternate renditions such as PDF and audio versions of the musical score.
Because the Container element allows for placement of child elements it is often used to provide the structure of a template. For example, you can build a template with two columns by instantiating two different Container elements, one for each column.
The Container element is one of the primary CommonSpot elements, especially for use in templates. Container Elements can hold one or more “child'' elements, including other Containers. ... Because the Container element allows for placement of child elements it is often used to provide the structure of a template.
XML elements can be defined as building blocks of an XML. Elements can behave as containers to hold text, elements, attributes, media objects or all of these. Each XML document contains one or more elements, the scope of which are either delimited by start and end tags, or for empty elements, by an empty-element tag.
In the example you have given the difference is subtle, however the two examples actually represent completely different things:
Like I said, in your example the difference is superfluous and so it is easy to miss, so instead consider the following slight variation:
<document>
<title>...</title>
<contents>
<page>...</page>
<page>...</page>
</contents>
<chapter name="chapterName">
<page>...</page>
<page>...</page>
</chapter>
<index>
<page>...</page>
<page>...</page>
</index>
</document>
In this case the document has many collections of pages. (Of course some might argue that you could equally represent this differently):
<document>
<title>...</title>
<page section="contents">...</page>
<page section="chapter1">...</page>
<page section="index">...</page>
</document>
However you would have had to change the page
element, in the above example I'd argue for the worse (why should page
have to know about what it is contained in?)
Another subtle consideration is that often the ordering of elements means something:
<document>
<page>...</page>
<title>...</title>
<page>...</page>
<page>...</page>
</document>
In this example we have (for whatever reason) a page before the title - obviously not possible when using collections. Another consideration is that each collection might also (for example) have a title
.
My point is that they are representing different things - it's not really a case of pro's vs con's so much of a case of choosing the format that most closely matches your data model.
It is really a matter of personal choice although the first form is more powerful, IMO. t makes multiple pages
, e.g. pages with different name possible. It just depends on your requirement.
<document>
<title>...</title>
<pages name="page1">
<page>...</page>
<page>...</page>
</pages>
<pages name="page2">
<page>...</page>
<page>...</page>
</pages>
</document>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With