XML Coding Style
I'm planning to use XML to configure a J2ee system, and I've come across what appears to be a coding style issue. The following are equivelent, yet it's normal to see the former, yet it would seem to me that the latter is better, shorter and clearer. I'm thinking of mandating the latter.
Would anybody care to comment ?
<section>
<section-name>Section Name</section-name>
<section-data>1234567890</section-data>
<section-data-format>PIC 9(10)</section-data-format>
</section>
<section name="Section Name">
<section-data format="PIC 9(10)">1234567890</section-data>
</section>
[792 byte] By [
MartinS.] at [2007-9-26 5:35:52]

The old XML question "Should I use attributes or elements?"
Entire articles could be (and probably have been) written on this topic. And there are probably people who have strong views one way or the other and are willing to flame people who hold different views. But just remember this: XML files are designed (in general) to be read by computer programs, not by people. And computers don't care whether it's shorter or clearer, just whether it's right. Given that, the answer is something close to "It doesn't matter."
It's not so much a coding style issue as it is storing data in element content or in element attribute. Which is better ? That all depends on your application - and more importantly on your future needs.
As a rule of thumb, data should be stored in elements, information about the data (metadata) should be stored in attributes.
Elliotte Harold's XML Bible says it best:
"When in doubt, put the information in the elements. To differentiate between data and metadata ask yourself whether someone reading the document would want to see a particular piece of information. If the answer is yes, then the information probably belongs in a child element. If the asnwer is no, then the information probably belongs in an attribute. (...) Attributes are good places to put ID numbers, URLs, references, and other information not directly or immediately relevant to the reader. However, there are many exception to the basic principle of storing metadata as attributes. Resons for making an exception include:
- attributes can't hold structure very well
- elements allow you to include meta-metadata
- elements are more extensible in the face of future changes.
(...)
Attributes are certainly convenient when you only need to convey one or two words of unstructured information. In these cases, there may genuinely be no current need for a child element. However, this doesn't preclude such a need in the future.
For instance, you may only need to store the name of the author of an article now, and you may not need to distinguish between the first and last names. However, in the future you may uncover a need to store first and last names, e-mail addresses, institutions, snail-mail addresses, URLs, and many more. If you've stored the authors of the article as elements, then it's easy to add child elements to include this additional information. (...) It's still much easier to change a simple element to a tree of elements than it is to make an attribute a tree of elements.
[end quote].
In short, elements are the safe way if you don't know what the future will bring, attributes are more closely tied to elements, save space in the XML file and can help to increase the readability of your XML file.
Good luck.
lk555 at 2007-7-1 13:45:32 >
