Is there a standard naming convention for XML elements?

XmlCoding StyleNaming Conventions

Xml Problem Overview


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

Xml Solutions


Solution 1 - Xml

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

Solution 2 - Xml

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>

Solution 3 - Xml

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.

Solution 4 - Xml

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

Solution 5 - Xml

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

Solution 6 - Xml

It's subjective, but if there are two words in an element tag, the readibility can be enhanced by adding an underscore between words (e.g. <my_tag>) instead of using no separator. Reference: http://www.w3schools.com/xml/xml_elements.asp. So according to w3schools the answer would be:

<my_tag attribute="some value">

The value needn't use an underscore or separator, since you are allowed spaces in attribute values but not in element tag names.

Solution 7 - Xml

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.

Solution 8 - Xml

rss is probably one of the most consumed xml schemas in the world and it is camelCased.

Spec is here: http://cyber.law.harvard.edu/rss/rss.html

Granted it has no node attributes in the schema, but all the node element names are camelCased. For example:

lastBuildDate managingEditor pubDate

Solution 9 - Xml

I normally align XML naming convention with the same naming convention in other parts of code. The reason is when I load the XML into Object its attributes and element names can be referred as the same naming convention currently used in the project.

For example, if your javascript using camelCase then your XML uses camelCase as well.

Solution 10 - Xml

Microsoft embraces two convention:

  1. For configuration, Microsoft uses camelCase. Look at Visual Studio config file. For VS2013, it is stored in:

    C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe.config

Example:

<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>

2. Microsoft also uses UpperCase for their XAML. I guess it is to differentiate from HTML (which uses lowercase).

Example:

<MenuItem Header="Open..." Command="ApplicationCommands.Open">
    <MenuItem.Icon>
        <Image Source="/Images/folder-horizontal-open.png" />
    </MenuItem.Icon>
</MenuItem>

Solution 11 - Xml

There is no explicit recommendation. Based on other recommendation from W3C, the one for XHTML, I've opted for lowercase:

> 4.2. Element and attribute names must be in lower case > > XHTML documents must use lower case for all HTML element and attribute > names. This difference is necessary because XML is case-sensitive e.g. > <li> and <LI> are different tags.

Solution 12 - Xml

XML Naming Rules

XML elements must follow these naming rules:

  • Names can contain letters, numbers, and other characters
  • Names cannot start with a number or punctuation character
  • Names cannot start with the letters xml (or XML, or Xml, etc)
  • Names cannot contain spaces Any name can be used, no words are reserved.

Source: [W3 School][1]

[1]: http://www.w3schools.com/xml/xml_elements.asp "W3School"

Solution 13 - Xml

I have been searching a lot for a good approach, also reading this thread and some others and I would vote for using hyphens.

They are used broadly in ARIA ( https://developer.mozilla.org/de/docs/Web/Barrierefreiheit/ARIA ) which can be seen in many source codes and are therefore common. As already pointed out here, they are certainly allowed, which is also explained here: https://stackoverflow.com/questions/1478486/using-in-xml-element-name

Also as a side benefit: When writing HTML in combination with CSS, you often have classes whose names use hyphens as separator by default as well. Now, if you have custom tags that use CSS classes or custom attributes for tags that use CSS classes, then something like:

<custom-tag class="some-css-class">

is more consistent and reads - in my humble opinion - much nicer than:

<customTag class="some-css-class">

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontpowerView Question on Stackoverflow
Solution 1 - XmlMarc GravellView Answer on Stackoverflow
Solution 2 - XmlFarhad MalekiView Answer on Stackoverflow
Solution 3 - XmlRaithlinView Answer on Stackoverflow
Solution 4 - XmlPhiLhoView Answer on Stackoverflow
Solution 5 - XmlannakataView Answer on Stackoverflow
Solution 6 - XmlalistairView Answer on Stackoverflow
Solution 7 - XmlPete KirkhamView Answer on Stackoverflow
Solution 8 - XmlevermeireView Answer on Stackoverflow
Solution 9 - XmlmicksatanaView Answer on Stackoverflow
Solution 10 - XmlJeson MartajayaView Answer on Stackoverflow
Solution 11 - XmlDiego MentaView Answer on Stackoverflow
Solution 12 - XmlpetermeissnerView Answer on Stackoverflow
Solution 13 - XmlIceFireView Answer on Stackoverflow