XML is the bread and butter of the so-called Web 2.0 development movement. While writing a web service to export some data to an XML file, through a few trials I learned some best practices with XML naming conventions.
Basic Naming Rules
These are nothing new for most programming languages, but sometimes slight variances apply (e.g., is punctuation allowed for the first character, as an underscore?). The rules according to the W3C schools guidelines:
- Names can contain letters, numbers, and other characters
- Names must not start with a number or punctuation character
- Names must not start with the letters xml (or XML, or Xml, etc)
- Names cannot contain spaces
Thus, "_" and "-" are acceptable characters.
Allowed Characters - Best Practices
The "-" character may be allowed, but it may lead to problems later with server-side code trying to subtract your XML element values. I used "-" in my CSS, since it is of no consequence in that language, and spread the habit to XML believing the same to be true. Use the underscore (_) and save yourself a headache.
In like manner, strive for all lower-case characters. XML is case-sensitive, just like CSS. Using camel-case will only lead to heartache when you typo that one character and your whole world explodes into hate.
Element Name Consistency
Ensure you have a solid schema. Often times to display your stored XML data, you will use an XSLT. XSLTs will often work by cycling through elements of a consistent naming convention and outputting your values depending on logic or required HTML.
Just last week I absentmindedly generated an XML file with a minor schema failing. The resulting XML, with that failing exaggerated, could have resembled the following:
XML:
-
<catalog>
-
<rock>
-
<radiohead>
-
<album>O.K. Computer</album>
-
</radiohead>
-
</rock>
-
<folk>
-
<matt-costa>
-
<album>Songs We Sing</album>
-
</matt-costa>
-
</folk>
-
</catalog>
This would be impossible to transform with an XSLT, as "rock" and "folk", in the structure of the XML, have no correlation. In a similar vein, the artist inside the genre could be anything - there is no way to know that "radiohead" and "matt-costa" are both artists.
Follow a schema with generic, semantic names, and either use elemental content or attributes to display your pertinent values. A refined XML file follows:
XML:
-
<catalog>
-
<genre id="rock">
-
<artist>
-
<name>Radiohead</name>
-
<album>O.K. Computer</album>
-
</artist>
-
</genre>
-
<genre id="folk">
-
<artist>
-
<name>Matt Costa</name>
-
<album>Songs We Sing</album>
-
</artist>
-
</genre>
-
</catalog>
Ah, how much easier this is to transform with an XSLT. Rather than analyzing element depth - which is vicious as it is inconsistent with larger XML files - one could process based on each genre, each artist, et cetera.
Get a naming convention down before you start processing your data; sometimes one of the tougher parts of programming is coming up with appropriate names. Be universal, be sustainable.
Popularity: 20% [?]