tags:

views:

343

answers:

6

Possible Duplicate:
Should I use Elements or Attributes in XML?

I'm writing a configuration file in XML and I am pretty much new to the whole XML craze.

I'm curious what the SO community thinks about how various values should be represented in XML, as I can see a couple of ways to do it.

For example, when should a value be an attribute and when should it be nested within tags?

<node1 message="Hello world" id="1" />

Versus

<node1>
  <message>Hello world</message>
  <id>1</id>
</node1>

Obviously this is an extremely simple example...but are there pro's and con's for doing certain types as an attribute versus child nodes? Or both or neither?

+13  A: 

I usually treat the attributes as metadata, or data about, or qualifying, the data within the nodes.

e.g. in your above example, I'd perhaps put the message as a text node, and have an attribute id relating to it.

There's no right or wrong here, but I find the above heuristic useful. One scenario where you may want to store as much as possible in attributes is for SAX parsing, in which your element callback has a parameter of all the attributes. i.e. you get one callback for all attributes, rather than 'n' callbacks, one for each piece of data. However that's a pretty specialised reason for constructing your document that way, and I'd think carefully before doing that.

Brian Agnew
that's a good way to think about it.
nickf
Yes, this is a common approach. See e.g. http://www.ibm.com/developerworks/xml/library/x-eleatt.html
ire_and_curses
thanks! definitely a good way to consider it when designing my config
espais
+1  A: 

I'd put the ID (and references, short names, etc) in as an attribute. The rest depends on you. I've seen a lot of "attribute only" schemas, and it's not so bad for certain types of data (short strings, dates, etc) and makes writing SAXParsers easier, but it doesn't feel "right" to me.

Some people support both, i.e., data as attributes or fields, so the user can decide.

I personally think that attributes as meta-data as described above is the sane way to do things.

JeeBee
+1  A: 

The best practice is to favor elements over attributes. Elements are easier to access from the DOM tree and provide structure data about the data it describes. By structure data I mean that you can determine who the element's parents and children are versus what they should be or can be. This is very important from the perspective of schema where XML defined by schema is structurally self-aware. The only benefit to attributes is brevity, which can also be important. If you can do with a single attribute what would otherwise require three or four nested tags then you should probably use an attribute.

+2  A: 

Specifying with elements is certainly more verbose and tiresome, on the other hand it is not possible to validate as fully with attributes.

If you have two properties, one of which must be specified, you can validate this if they are child elements within the schema, with attributes you can only say if the attribute is required or not.

From the W3Schools Attributes page:

Some of the problems with using attributes are:

  • attributes cannot contain multiple values (elements can)
  • attributes cannot contain tree structures (elements can)
  • attributes are not easily expandable (for future changes)

In this (slightly contrived) example the vehicle might have both wings and wheels or some combination, with attributes we can't enforce that at least one is defined, whereas with elements we could use a choice element with a minOccurs to ensure at least one of the elements is defined.

The name attribute is always required so it can be either an attribute or element with no validation issues.

<vehicle name="plane" wheels="3" wings="2"/>
<vehicle name="sled" runners="2"/>
<vehicle name="robin reliant" wheels="3"/>

<vehicle name="plane">
  <wheels>3</wheels>
  <wings>2</wings>
</vehicle>
<vehicle name="sled">
  <runners>2</runners>
</vehicle>
<vehicle name="robin reliant">
  <wheels>3</wheels>
</vehicle>
Rich Seller
+1 for the comments re. multiple values/tree structures etc., but I don't understand "Use attributes for information that is not relevant to the data". That doesn't seem quite right!
Brian Agnew
It's slightly odd phrasing, I would read it as use attributes for metadata, though I'm not certain that's what was meant
Rich Seller
I've thought about it some more and I'm still not clear what it means, will remove it
Rich Seller
The comment about multiple values is definitely something I hadn't thought about, thanks for the tip!
espais
A: 

Your question is about attributes versus elements. Theres is a page that summarizes sources of information about attributes versus elements. You can also Google for attributes versus elements.

Martin Liversage
+1  A: 

Another point to consider: attributes cannot have significant whitespace in them.

Ned Batchelder