views:

3860

answers:

5

Is there any standard, de facto or otherwise, for XML documents? For example which is the "best" way to write a tag?

<MyTag />
<myTag />
<mytag />
<my-tag />
<my_tag />

Likewise if I have an enumerated value for an attribute which is better

<myTag attribute="value one"/>
<myTag attribute="ValueOne"/>
<myTag attribute="value-one"/>
+9  A: 

I suspect the most common values would be camelCased - i.e.

<myTag someAttribute="someValue"/>

In particular, the spaces cause a few glitches if mixed with code-generators (i.e. to [de]serialize xml to objects), since not many languages allow enums with spaces (demanding a mapping between the two).

Marc Gravell
Hm ... best answer ... I think it is a decent answer, but it is just an opinion. Having some sort of reference would be nice.
Hamish Grubijan
+7  A: 

I would tend to favour lowercase or camelcase tags and since attributes should typically reflect data values - not content - I would stick to a value which could be used as a variable name in whatever platform/language might be interested, i.e. avoid spaces but the other two forms could be ok

annakata
+1 for thinking about variable/function names
Ates Goral
@downvoter: please do me the courtesy of explaining yourself.
annakata
+3  A: 

I favour TitleCase for element names, and camelCase for attributes. No spaces for either.

<AnElement anAttribute="Some Value"/>

As an aside, I did a quick search for Best Practices in XML, and came up with this rather interesting link: XML schemas: Best Practices.

Raithlin
+4  A: 

For me, it is like discussing of code style for a programming language: some will argue for a style, others will defend an alternative. The only consensus I saw is: "Choose one style and be consistent"!

I just note that lot of XML dialects just use lowercase names (SVG, Ant, XHTML...).

I don't get the "no spaces in attributes values" rule. Somehow, it sends to the debate "what to put in attributes and what to put as text?".
Maybe these are not the best examples, but there are some well known XML formats using spaces in attributes:

  • XHTML, particularly class attribute (you can put two or more classes) and of course alt and title attributes.
  • SVG, with for example the d attribute of the path tag.
  • Both with style attribute...

I don't fully understand the arguments against the practice (seem to apply to some usages only) but it is legal at least, and quite widely used. With drawbacks, apparently.

Oh, and you don't need a space before the auto-closing slash. :-)

PhiLho
+4  A: 

Many document centred XML dialects use lower case basic Latin and dash. I tend to go with that.

Code generators which maps XML directly to programming language identifiers are brittle, and (with the exception of naive object serialisation, such as XAML) should be avoided in portable document formats; for best reuse and information longevity the XML should try to match the domain, not the implementation.

Pete Kirkham