130

Are there any formal recommendations on element casing in XML?

I know XHTML uses lowercase element names (as opposed to HTML which canonically uses uppercase but is case-insensitive.)

But I'm talking about XML for generic content.

lowercase:

<customer> 
   <accountnumber>619</accountnumber>
   <name>Shelby Lake</name>
</customer>

camelCase:

<customer> 
   <accountNumber>619</accountNumber>
   <name>Shelby Lake</name>
</customer>

PascalCase:

<Customer> 
   <AccountNumber>619</AccountNumber>
   <Name>Shelby Lake</Name>
</Customer>

UPPERCASE:

<CUSTOMER> 
   <ACCOUNTNUMBER>619</ACCOUNTNUMBER>
   <NAME>Shelby Lake</NAME>
</CUSTOMER>

Note: I'm looking for cited guidelines rather than opinions. But the opinion with the most up-votes can be considered a guideline.

oluies
  • 17,694
  • 14
  • 74
  • 117
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
  • 1
    possible duplicate of [Is there a standard naming convention for XML elements?](http://stackoverflow.com/questions/442529/is-there-a-standard-naming-convention-for-xml-elements) – Basil Bourque Jun 17 '14 at 21:05

10 Answers10

92

Most XML standards originating from the W3C tend to use lower case with hyphens.

There is a philosophical distinction between seeing XML as a format for platform neutral documents, which W3C standards try to encourage, and languages such as XAML which see XML as a serialisation of a platform specific object graph.

If you're not using XML as a platform neutral document format, but as an application specific serialisation, then you might as well save yourself some bother and have a 1:1 correspondence between the XML names and the platform specific names. But almost any other object graph format is better than XML for that purpose.

If you are, then you might want to fit in with XHTML, XSLT, SVG, XProc, RelaxNG and the rest.

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
  • 7
    so does that mean lower case with hyphens is recommended or not recommended? – WarFox Jul 20 '11 at 13:27
  • 9
    @WarFox I don't think anyone has made an official recommendation. IME, the formats which come from w3c for interoperability tend to be in the hyphen style; formats which come from Microsoft and some others tend to be tightly coupled to an implementation and the convention for names of objects in the language used for the implementation. If you are using XML to decouple systems, then not coupling your XML to the language style of one component of that system may force you to think in terms of messages rather than objects, and so I would recommend lower case and hyphen style. – Pete Kirkham Jul 20 '11 at 14:48
  • 6
    Note that 'lowercase with hyphens' has some problems in XSLT. Specifically it is easy to confuse a node called, say, 'year-from-age' with a formula 'year - age' (e.g. subtract age from year) – Richard Kennard Aug 20 '13 at 07:38
  • 3
    @RichardKennard Is that confusion only possibly at the human level? For xslt the spaces required(?) around the operator provide a clear and unambiguous distinction, correct? – Karl Kieninger Sep 18 '13 at 12:08
  • 2
    @KarlKieninger that's true, but confusion at the human level is a significant concern, IMHO. See my expanded answer below. – Richard Kennard Sep 21 '13 at 03:27
  • 1
    I must comply with Richard. I hate hyphen in naming something. It causes confusion. BTW, I don't like underscore, either. I have to press SHIFT to type it =.= – Scott Chu Dec 11 '13 at 02:15
  • 1
    +1 for "But almost any other object graph format is better than XML for that purpose." – Oskar Austegard May 14 '14 at 21:34
67

Not that it matters, but I've always been partial to PascalCase for Elements and camelCase for attributes:

<Root>
  <ParentElement attributeId="1">
    <ChildElement attributeName="foo" />
  </ParentElement>
</Root>
Metro Smurf
  • 37,266
  • 20
  • 108
  • 140
33

There is no formal recommendation.

Since XML was designed with the twin purposes of holding documents and exchanging information between disparate systems, it was designed so as to be able to match the applications using it.

So .Net XML tends to use ProperCasing (witness XAML), while other XML will use camelCasing, python_conventions, dot.naming, and even COBOL-CONVENTIONS. The W3C seems to like lower-case-with-dashes-quite-a-bit (e.g. XSLT) or justlowercasewordssmashedtogether (e.g. MathML).

I like all lower case and no underscores, since that means less use of the [Shift] key, and my fingers are a little lazy. :)

lavinio
  • 23,931
  • 5
  • 55
  • 71
  • 2
    It seems that if it had the purpose of "exchanging information" then a standard for naming conventions would be absolutely desired. This would also apply to any documents used by more than one specific implementation (e.g. that were not one-off serializations). –  May 24 '12 at 20:28
27

To add to Metro Smurf's answer.

The National Information Exchange Model (NIEM: http://en.wikipedia.org/wiki/National_Information_Exchange_Model) says to use:

  • Upper CamelCase (PascalCase) for elements.
  • (lower) camelCase for attributes.

The NIEM makes for a good option when you're looking to conform to some standard.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Koert van Kleef
  • 772
  • 9
  • 17
  • 1
    Here is the NIEM document where they describe the naming conventions for attributes and elements: https://www.niem.gov/documentsdb/Documents/Technical/NIEM-NDR-1-3.pdf – e1i45 Mar 14 '14 at 09:42
  • I know it's old, but the above link is broken, but this link seems to have updated details regarding the NIEM convention for Elements/Attrubutes: https://reference.niem.gov/niem/specification/naming-and-design-rules/4.0/niem-ndr-4.0.html#section_10.8.1 – CajunCoding Jul 08 '20 at 20:32
15

To expand on my comment above: the use of 'lowercase with hyphens' has some problems in XSLT. Specifically it is easy to confuse a node called, say, 'year-from-age' with a formula 'year - age' (e.g. subtract age from year).

As @KarlKieninger points out, this is only a problem at the human level and not for the XSLT parser. However since this will often not produce an error, using 'lowercase with hyphens' as a standard is asking for trouble, IMHO.

Some pertinent examples:

<a>1</a><b>1</b>
<xsl:value-of select="a+b"/>
outputs 2, as expected

<a>1</a><b>1</b>
<xsl:value-of select="a-b"/>
DOES NOT ERROR, BUT OUTPUTS NOTHING AT ALL

In the above code, you must put at least one space before a subtraction operator, but there is no such requirement for an addition operator.

<a-b>1</a-b><c>1</c>
<xsl:value-of select="a-b -c"/>
outputs 0, as expected

But note how confusing the above is to read!

<a>1</a><a-b>3</a-b><b>2</b>
<xsl:value-of select="a-b"/>
outputs 3

<a>1</a><a-b>3</a-b><b>2</b>
<xsl:value-of select="a -b"/>
outputs -1

The presence of a single space changes the output above, but neither variant is an error.

Community
  • 1
  • 1
Richard Kennard
  • 1,325
  • 11
  • 20
  • 3
    Sold. Additionally MS .NET code generation tool (xsd.exe) will simply drop the hyphens when it creates deserialization classes. So instead if properties like "alpha-beta" you get "alphabeta." So not only don't the names translate exactly, they are harder to read. And if you need to build xml with MS SQL hyphens are a pain in there as well. MS specific stuff shouldn't dictate a standard, but it is in wide enough use that I think it can be counted as a consideration. And it steers me toward underscore delimited lower-case or mixed case. – Karl Kieninger Oct 21 '13 at 04:46
  • 1
    It looks like XSD.exe now adds an XmlElementAttribute to most properties, and if there is a hyphen it explicitly adds the ElementName string as the first parameter so it's clarified. The problem I found is that depending on your .NET libraries, this may not matter - the element still won't deserialize on some systems. It works in VS2012 on Win7, but not running as an EXE on a 2008 Server. – David Storfer Feb 05 '15 at 21:48
13

Google's style guide recommends (perhaps even mandates) camelCase for all element names, as well as attribute names:

All names must use lowerCamelCase. That is, they start with an initial lower-case letter, then each new word within the name starts with an initial capital letter.

Rationale: Adopting a single style provides consistency, which helps when referring to names since the capitalization is known and so does not have to be remembered. It matches Java style, and other languages can be dealt with using automated name conversion.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
kencordero
  • 151
  • 1
  • 5
13

See the UN/CEFACT XML Naming and Design Rules Technical Specification Version 3.0 page 23 for some example rules used in several standards.

Specifics (from page 23 of Version 3.0 dated 17 December 2009):

  • LowerCamelCase (LCC) MUST be used for naming attributes.
  • UpperCamelCase (UCC) MUST be used for naming elements and types.
  • Element, attribute and type names MUST be in singular form unless the concept itself is plural.

(other link, Swedish site)

nurettin
  • 11,090
  • 5
  • 65
  • 85
oluies
  • 17,694
  • 14
  • 74
  • 117
  • 2
    -1: your link is broken - perhaps it's an internal URL? Please fix it, and I'll remove the downvote. – John Saunders Jun 30 '10 at 23:11
  • 3
    While certainly interesting, the quoted document at this specific section / page establishes rules for XML **Schemas**, not the parsers / xml documents that adhere to this schema. These are two different things. – MrCC Jan 17 '16 at 11:52
  • 1
    For anyone wondering what the difference is between LowerCamelCase and UpperCamelCase: mySettingName is an example of LowerCamelCase (which might have been smarter to call lowerCamelCase), and MySettingName is an example of UpperCamelCase. – Jinlye Oct 30 '19 at 12:37
4

The original intent for XML casing was lower case with hyphens. It's case sensitive and doesn't require you follow that convention -- so you can do whatever you want. I have no citations, sorry.

Adam Luter
  • 2,193
  • 1
  • 15
  • 20
1

I wouldn't say HTML "canonically" uses uppercase. I think originally uppercase was used to visually separate HTML from the content more easily. With syntax highlighting nowadays, that's just not necessary.

I veer towards lowercase, with dashes if necessary (quicker to type, too). Mixing case in XML just feels wrong to me.

DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290
-2

Camel case gets my vote.

As to cited examples perhaps this question can be come the link people cite.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306