79

What's the difference between the two and when should I use each:

<person>
     <firstname>Joe</firstname>
     <lastname>Plumber</lastname>
</person>

versus

<person firstname="Joe" lastname="Plumber" />

Thanks

John Saunders
  • 160,644
  • 26
  • 247
  • 397
CoolGravatar
  • 5,408
  • 7
  • 35
  • 42

5 Answers5

73

There are element centric and attribute centric XML, in your example, the first one is element centric, the second is attribute centric.

Most of the time, these two patterns are equivalent, however there are some exceptions.

Attribute centric

  • Smaller size than element centric.
  • Not very interoperable, since most XML parsers will think the user data is presented by the element, Attributes are used to describe the element.
  • There is no way to present nullable value for some data type. e.g. nullable int
  • Can not express complex type.

Element centric

  • Complex type can be only presented as an element node.
  • Very interoperable
  • Bigger size than attribute centric. (Compression can be used to reduce the size significantly.)
  • Nullable data can be expressed with attribute xsi:nil="true"
  • Faster to parse since the parser only looks to elements for user data.

Practical

If you really care about the size of your XML, use an attribute whenever you can, if it is appropriate. Use elements where you need something nullable, a complex type, or to hold a large text value. If you don't care about the size of XML or you have compression enabled during transportation, stick with elements as they are more extensible.

Background

In DOT NET, XmlSerializer can serialize properties of objects into either attributes or elements. In the recent WCF framework, DataContract serializer can only serialize properties into elements and it is faster than XmlSerializer; the reason is obvious, it just needs to look for user data from elements while deserializing.

Here an article that explains it as well Element vs attribute

Brad Turek
  • 2,472
  • 3
  • 30
  • 56
Ray Lu
  • 26,208
  • 12
  • 60
  • 59
  • 2
    Attribute-centric XML has a huge interoperability advantage with JSON. All you need is a few hints for what to put into an array and what to treat as a number or boolean, and you can do perfect automatic conversions. – PstScrpt Oct 08 '11 at 17:58
5

Sometime in the future when you add an <address> property, you won't want to make it an XML attribute. This is because an <address> might be a more complex element made up of street address, city, country, etc.

For this reason, you may want to choose the first subelement form unless you're really sure that the attribute won't need to go much deeper. The first form allows for greater extensibility in the future.

If you're at all concerned about space, compress your XML.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Just curious but couldn't you have a mix of both. Say first and last name as attributes and then address as a node. What I am trying to say is you do not have to only pick one. Correct? – Jacob Schoen Oct 28 '08 at 00:45
  • 2
    You're right, but for consistency when representing properties of a single entity ("person" in the original example), you would probably want to choose one or the other. That way you won't have to remember which is stored in which way. – Greg Hewgill Oct 28 '08 at 00:54
  • `
    ` I can't figure out how to get it to format right, but the point is, you are not cutting off extensibility. It's not child elements you're avoiding with attribute centric XML, but text nodes.
    – PstScrpt Oct 08 '11 at 17:43
4

In my company, we would favour the 2nd approach.

The way we think about it is that "firstname" and "lastname" are attributes of the "person" node, rather than sub-fields of the "person" node. It's a subtle difference.

In my opinion the 2nd approach is more concise, and readability/maintainability is significantly improved, which is very important.

Of course it would depend on your application. I don't think there is a blanket rule that covers all scenarios.

LeopardSkinPillBoxHat
  • 28,915
  • 15
  • 75
  • 111
  • 1
    The 2nd approach is more readable for a human, but the 1st approach is more readable by most parsers. The first approach is also more extensible. You should read the article referenced by **@codemeit** (http://www.ibm.com/developerworks/xml/library/x-eleatt.html). – awe May 07 '10 at 05:39
4

I found following information very helpful in explaining the choice of attributes vs elements in a short fashion

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)

Attributes are difficult to read and maintain. Use elements for data. Use attributes for information that is not relevant to the data.

source : http://www.w3schools.com/xml/xml_attributes.asp

JavaHopper
  • 5,567
  • 1
  • 19
  • 27
  • 1
    It's a shame this answer doesn't have more votes because I think it sums up the issues nicely, particularly with "Attributes are difficult to read and maintain. Use elements for data. Use attributes for information that is not relevant to the data." – Peter Ellis May 30 '19 at 01:36
2

Attributes are not order sensitive. This may be an advantage or a disadvantage depending on your situation.

Attributes cannot be duplicated. If "Joe" has two first names, then nodes are the only way to go.

Andru Luvisi
  • 24,367
  • 6
  • 53
  • 66