Is there a standard naming convention for XML elements? [closed]

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"/>

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


XML Naming Rules

XML elements must follow these naming rules:

    - Element names are case-sensitive 
    - Element names must start with a letter or underscore
    - Element names cannot start with the letters xml(or XML, or Xml, etc) 
    - Element names can contain letters, digits, hyphens, underscores, and periods 
    - Element names cannot contain spaces

Any name can be used, no words are reserved (except xml).

Best Naming Practices

    - Create descriptive names, like this: <person>, <firstname>, <lastname>.
    - Create short and simple names, like this: <book_title> not like this: <the_title_of_the_book>.
    - Avoid "-". If you name something "first-name", some software may think you want to subtract "name" from "first".
    - Avoid ".". If you name something "first.name", some software may think that "name" is a property of the object "first".
    - Avoid ":". Colons are reserved for namespaces (more later).
    - Non-English letters like éòá are perfectly legal in XML, but watch out for problems if your software doesn't support them.

Naming Styles

There are no naming styles defined for XML elements. But here are some commonly used:

    - Lower case    <firstname> All letters lower case
    - Upper case    <FIRSTNAME> All letters upper case
    - Underscore    <first_name>    Underscore separates words
    - Pascal case   <FirstName> Uppercase first letter in each word
    - Camel case    <firstName> Uppercase first letter in each word except the first

reference http://www.w3schools.com/xml/xml_elements.asp


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.


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