XML attributes are pointless?

By Confusion on Thursday 4 September 2008 20:17 - Comments (7)
Categories: Software engineering, XML, Views: 4.335

When you put together an XSD to specify how certain XML documents should look, you are always confronted with the choice to make parts of the information you wish to convey either an element or an attribute. Some people seem to think attributes are pointless and proponents of XML alternatives JSON and YAML sometimes opine as much.

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.

XML:
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

XML:
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

XML:
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.

Volgende: The disadvantages of the Google Web Toolkit 09-'08 The disadvantages of the Google Web Toolkit
Volgende: Waterkoker of fluitketel 09-'08 Waterkoker of fluitketel

Comments


By Tweakers user H!GHGuY, Thursday 4 September 2008 20:53

Imo your example is plain wrong.
A questionnaire contains questions, not answers. Questions have associated answers.

Therefore consider:

XML:
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

XML:
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>


or

XML:
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.

By Tweakers user Confusion, Thursday 4 September 2008 21:54

I should have explained and have adapted the post accordingly: I am storing the responses to a questionnaire. The questions are explicitly not supposed to be part of that: it is useless to have the same questions in every response that is stored in the database.

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]


By Tweakers user Victor, Friday 5 September 2008 08:47

No, an attribute is not an element. What you're referring to are nodes, which is terminology used in the DOM.

To answer the title of this post: no, XML attributes are not pointless. They provide a means to store metadata:

XML:
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

By Tweakers user DOT, Friday 5 September 2008 12:05

But 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.

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.

By Tweakers user Victor, Saturday 6 September 2008 14:10

But 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.
Look it up: http://en.wikipedia.org/wiki/Metadata

The aesthetics argument is rubbish. Although XML is human readable, that's just a bonus, not its purpose.

By Tweakers user DOT, Sunday 7 September 2008 04:07

Smartass. :P Let me quote that right back at you:
The hierarchy of metadata descriptions can go on forever, but usually context or semantic understanding makes extensively detailed explanations unnecessary.
By this statement, attributes are clearly inadequate. You can't have multiple levels of meta-data in your attributes.

By Tweakers user Confusion, Sunday 7 September 2008 20:44

@Victor:
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.

@DOT:
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