XML Elements

An XML document contains XML elements.

What is an XML element?

An XML element refers to all content from (including) the element's start tag to (including) the element's end tag.

<price>29.99</price>

An element can contain:

  • Text
  • Attributes
  • Other elements
  • or above mixed
<bookstore>
<book category="food">
  <title lang="zh">Talks About Eating</title>
  <author>Liang Shiqiu</author>
  <press>Jiangsu Literature and Art Publishing House</press>
  <year>2013</year>
  <price>35</price>
  <ISBN>9787539962771</ISBN>
</book>
<book category="children">
  <title lang="zh">The Fantastic Mr. Fox</title>
  <author>Rolfe Dalle</author>
  <translator>David</translator>
  <press>Tomorrow Publishing House</press>
  <year>2009</year>
  <price>10</price>
  <ISBN>9787533259563</ISBN>
</book>
</bookstore>

In the above example:

Elements such as <title>, <author>, <year>, and <price> haveText content, because they contain text (such as 29.99).

The <bookstore> and <book> haveElement content, because they contain elements.

The <book> has oneAttributes (for example: category="children").

Empty XML elements

Elements without content are called empty elements.

In XML, you can indicate empty elements like this:

<element></element>

You can also use so-called self-closing tags:

<element />

Both forms produce the same result in XML software (readers, parsers, browsers).

Tip:Empty elements can also have attributes.

XML Naming Conventions

XML elements must follow the following naming conventions:

  • Element names are case-sensitive
  • The element name must start with a letter or an underscore
  • The element name cannot start with the letters xml (or XML, Xml, etc.)
  • The element name can contain letters, numbers, hyphens, underscores, and periods
  • The element name cannot contain spaces

Any name can be used, there are no reserved words (except xml).

Best Naming Practices

Names should be descriptive, like <person>, <firstname>, <lastname>.

Names should be short, like <book_title>, rather than like <the_title_of_the_book>.

Avoid the '-' character. If you name it like this: 'first-name', some software may think you want to subtract 'name' from 'first'.

Avoid the '.' character. If you name it like this: 'first.name', some software may consider 'name' as the property of the object 'first'.

Avoid the ':' character. The colon will be converted to a namespace (to be introduced later).

Non-English letters like é, ò, á are also valid XML element names, but be aware that there may be issues when software developers do not support these characters.

Naming Conventions

Some common naming conventions for XML elements:

Style Example Description
LowerCase <firstname> All letters lowercase
UpperCase <FIRSTNAME> All letters uppercase
Snake_case <first_name> Underscore-separated words (commonly used in SQL databases)
PascalCase <FirstName> Capitalize the first letter of each word (commonly used by C programmers)
CamelCase <firstName> Capitalize the first letter of each word except the first one (commonly used in JavaScript)

Tip:Choose your naming style and keep it consistent!

XML documents often have a corresponding database where the fields correspond to the elements in the XML document.

There is a practical experience: use the naming conventions of the database to name the elements in the XML document.

XML elements are extensible

XML elements are extensible to carry more information.

Please see the following XML example:

<note>
  <to>George</to>
  <from>John</from>
  <body>Don't forget the meeting!</body>
</note>

Let's imagine that we have created an application that can extract the <to>, <from>, and <body> elements and produce the following output:

MESSAGE
To: George
From: John
Don't forget the meeting!

Imagine that the author of this XML document later added some additional information to it:

<note>
  <date>2008-01-10</date>
  <to>George</to>
  <from>John</from>
  <heading>Reminder</heading>
  <body>Don't forget the meeting!</body>
</note>

Will this application break or crash?

No. This application can still find the <to>, <from>, and <body> elements in the XML document and produce the same output.

One of the advantages of XML is that it can often be extended without interrupting the application.