XML attributes are pointless?
Now I'm not going to make a thorough analysis of when to use elements and when to use attributes, because that has already been done on a great number of occasions. What I wish to do, is to give a simple example of a case in which an attribute makes the information that is to be conveyed easier to understand:
NB. After H!ghGuy's first response I realized I forgot to give an important piece of information: my intention is to store responses to a questionnaire. This is part of a larger piece if XML, storing, for instance, a name, address, etc.
1 2 3 4 5
<questionnaire> <answer question="1">D</answer> <answer question="3b">Cow</answer> <answer question="last">144</answer> </questionnaire>
If the 'question' reference needs to be an element, you get either
1 2 3 4 5 6 7
<questionnaire> <answer> <question>1</question> <answer_contents>D</answer_content> </answer ... </questionnaire>
in which case the fact that the element requires an awkward name is indication enough that something is wrong, or
1 2 3 4 5 6
<questionnaire> <answer> <question>1</question> D </answer ...
Here D is not actually an 'element' in the way 'element' is used above, but it is an obvious alternatieve.
In the last case, the question seems to be part of the answer element, just like the actual answer is part of the answer element. That just doesn't sit right with me, as they have a decidedly different relationship to the concept of an 'answer'. In JSON and YAML the same solutions appear and I think all are equally unsatisfying.
In short: child elements together constitute the parent element. If you need to provide a piece of meta-information or want to 'annotate' an element, using an attribute is a clear way in which to distinguish this kind of information from the constitutive kind.
A questionnaire contains questions, not answers. Questions have associated answers.
1 2 3 4 5 6
<questionnaire> <question> <description>What's your favorite colour?</description> <answer>Gold</answer> </question> </questionnaire>
If you really want to discuss attributes, take the following example
1 2 3 4 5 6 7 8 9
<!-- Question file --> <questionnaire> <question id="1">What's your favorite colour?</question> </questionnaire> <!-- Answer file --> <answers> <answer questionid="1">Gold</answer> </answers>
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<!-- Question file --> <questionnaire> <question> <id>1</id> <description>What's your favorite colour?</description> </questionnaire> <!-- Answer file --> <answers> <answer> <id>1</id> Gold </answer> </answers>
Note that I embodied both your examples in just 1 here
Secondly in your second example, D is in fact an element, more specifically a text element.
Considering the fact that D is a text element: yes, but I was distinguishing between 'attributes' and 'elements', I was not using 'element' to refer to text elements; in that more general sense of 'element', attributes are also elements, namely: attribute elements.
Therefore, I think we agree .
[Comment edited on Thursday 4 September 2008 22:17]
To answer the title of this post: no, XML attributes are not pointless. They provide a means to store metadata:
1 2 3 4 5 6 7 8 9 10 11 12
<ikea xmlns="http://www.ikea.com" xml:lang="en"> <tables> <table> <name xml:lang="sv">Björn</name> <dimensions> <width unit="cm">100</width> <height unit="dm">6</height> <depth unit="m">1</depth> </dimensions> </table> </tables> </ikea>
As you can see, all the data is stored in elements. However, some of this data requires some further information, or metadata, which is stored in the attributes. Without the unit attribute, the dimension values would be meaningless. Without the xmlns attribute, we wouldn't have known whether the table element is describing a piece of furniture or a table in a relational database
IMHO it's just a matter of aesthetics. Attributes are less useful (they can't have children), but they can "look nice". And aesthetics are important for readability.
Look it up: http://en.wikipedia.org/wiki/MetadataBut what is data, and what is meta-data? It's all equally important. Everything you put in attributes, you can also put in a child element.
The aesthetics argument is rubbish. Although XML is human readable, that's just a bonus, not its purpose.
By this statement, attributes are clearly inadequate. You can't have multiple levels of meta-data in your attributes.The hierarchy of metadata descriptions can go on forever, but usually context or semantic understanding makes extensively detailed explanations unnecessary.
You use 'element' as it is used in the XML specification. However, the XML spec doesn't recognize the term 'text element'. What people mean by that term, is called 'character data' in the spec. What I stated in my original post, is that character data is not an element. H!ghGuy said that it is a 'text element'. Instead of dragging the spec in, I asserted that in that sense of 'element', an attribute is also an element. You use 'node' where he used 'element', which is less confusing, but still besides the point.
You could use an attribute to reference an element containing the meta-data. That element could be annotated with another attribute referring to another element ..., etc. Not practical, but XML isn't the practical solution for every problem
Comments are closed