Go to previous pageGo to next page

1.3.5. Element Naming and Wellformedness

XML elements have simple naming rules as you will see in this chapter.

XML elements must follow these naming rules:

  • Names can contain letters, numbers, and other characters
  • Names must not start with a number or punctuation characters. Names must not start with the letters xml (or XML, or Xml, etc.)
  • Names cannot contain spaces.

Take care when you "invent" element names and follow these simple rules:

  • Any name can be used, no words are reserved, but the idea is to make names descriptive.
  • Avoid " - " and " . " in names such as <first-name> or <first.name>.
  • Element names can be as long as you like, but do not exaggerate. Ideally names are as short and simple as possible: <book_title> instead of <the_title_of_the_book>.
  • Non-English letters like é ò á are perfectly legal in XML element names, but watch out for problems if your software vendor does not support them.
  • The " : " should not be used in element names because it is reserved for termnamespaces.
Legal and      illegal element namesLegal and illegal element names

"Well Formed" XML Documents

When having created an XML file you have to check if it is well formed. A "well formed" XML document is a document that conforms to the XML syntax rules above.

A check for wellformedness contains the following checks:

  • if prolog <?xml version="1.0"?> is existing and valid,
  • whether we have exactly one root element (e.g. <person>),
  • if all elements are correctly opened and closed (<person> </person>),
  • if the elements are correctly nested (<person><name> </name></person>).

Presentation of XML code

Spaces that are not part of the XML content (outside of tags) are ignored by an XML interpreter. Therefore, you are free in the arrangement of your XML code. It does not matter for an XML interpreter wheter the code is written all in one line or one line below the other without indentions, but from the point of view of a human XML-file reader, we stronlgy recommend to arrange your XML code in a legible way by using indentions.

The following figure shows two examples of hardly readable code and in contrast code that is easy to read for a human:

Presentation of XML      codePresentation of XML code

XML editors format XML code automatically in a legible way.

Exercise

Check your XML document that you created in the last chapter for its wellformedness by opening in a web browser. The browser checks the document for its wellformedness and alerts the mistakes if your document is not wellformed. If you have questions ask your tutor.



Go to previous page
Go to next page