Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XML elements


May 27, 2021 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...
<bookstore>
<book category="CHILDREN">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>

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:

<note>
<to>小明</to>
<from>小狮妹</from>
<body>周末别忘记学习编程哦~</body>
</note>

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>

MESSAGE

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:

<note>
<date>2020-09-09</date>
<to>小明</to>
<from>小狮妹</from>
<heading>提醒</heading>
<body>周末别忘记学习编程哦~</body>
</note>

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!