XML elements

May 27, 2021 17:00 XML

Table of contents

XML elements

XML elements are an integral part of an XML document, and we can think of an XML element as a container that holds text, elements, properties, media objects, or all of that.

The XML document contains XML elements.

Each XML document contains one or more elements whose scope is either separated by the start and end markers, or empty elements, with an empty element label.

What is an XML element?

XML elements refer to the portion of the label that starts (and includes) the label until (and includes) the end label.

An element can contain:

  • Other elements
  • Text
  • Property
  • Or mix all of the above...
<book category="CHILDREN">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<book category="WEB">
<title>Learning XML</title>
<author>Erik T. Ray</author>

In the example above, <bookstore> <book> have element content because they contain other elements. <book> (category="CHILDREN"). <title> text content because <author> they contain text. <year>

XML naming rules

XML elements must follow the following naming rules:

  • Names can contain letters, numbers, and other characters
  • Names cannot start with numbers or punctuation marks
  • The name cannot start with the letter xml (or XML, Xml, and so on).
  • The name cannot contain spaces

You can use any name, no reserved words.

Best naming habits

Make the name descriptive. It's also good to use <first_name> <last_name>

The name should be short and <book_title> not: <the_title_of_the_book>

Avoid the - character. If you name it this way: first-name some software will think you want first from name

Avoid the . character. If you name it this way: first.name some software will name to be a property of the first

Avoid the : " character. Colons are converted to namespaces for use (more on that later).

XML documents often have a corresponding database, where the fields correspond to elements in the XML document. A practical experience is to use the naming rules of the database to name elements in an XML document.

In XML, non-English letters such as the one are éòá legal, but it's important to note that there may be problems when your software vendor doesn't support these characters.

XML elements are extensable

XML elements are extensable to carry more information.

Take a look at the following example of XML:


Let's imagine that we've created an application that <from> extracts the elements from <body> the XML document and produces the following output: <to>


To: Xiaoming
From: Little Lion Sister

Don't forget to learn programming at the weekend

Imagine some additional information added by the author of the XML document:


So will the app break or crash?

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

One of the advantages of XML is that it can scale without interrupting the application.

That's all about XML elements, and you need to be aware that in XML, all elements must have an end tag!