Wednesday, July 2, 2008

Code it Right: XML Naming Conventions

Name Your XML Elements Correctly!

Two blog posts ago I mentioned what XML was and why should you use it. In this post I'll discuss the naming conventions that you should follow when writing XML element names.

  • There are no reserved words that you need to avoid, as in JavaScript or programming languages.
  • Names can start with letters of the alphabet or non-Latin characters, upper case or lower case, or a dash. You can't start names with numbers or punctuation marks.
  • After the first character of the name only numbers, hyphens and periods are allowed.
  • Names can't have any spaces. For example:
    <my   tag>
  • Don't use the letters "XML" as the beginning of a name - neither lower, upper or mixed case.
  • Don't use colons ( : )
  • Don't place a space after the opening <
    <   this>
    is not allowed. However, you can place a space before the closing >

It's important to know that XML tags are case sensitive. So you can't start an element like this: <Mytag> and close it with </mytag> You'll get an error. I would recommend you stick with only using lowercase, or your own style that you use consistently.

XML and Whitespace

If you add space in your element data (called PCDATA) then that space will not be stripped out. For example:

<mytag>This space           will not be stripped out, as it would be in HTML</mytag>

Stay tuned for more posts on well-formed XML.


0 comments: