XML Elements

XML Elements are present in every XML document.

What is an XML Element?

Everything from the element’s start tag to the element’s end tag in an XML document is an XML element.

Example:

 <name>Tom</name>

Text, attributes, other elements, or a mix of the above is included in an element.

Example:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
 
  <book category="Child">
    <title lang="en">ABC</title>
    <author>Unknown</author>
    <year>2020</year>
    <price>100.00</price>
  </book>
 
  <book category="IT">
    <title lang="en">XQuery Book</title>
    <author>Author 1</author>
    <author>Author 2</author>
    <author>Author 3</author>
    <author>Author 4</author>
    <year>2004</year>
    <price>350.00</price>
  </book>
 
</bookstore>

Explanation:

In the above example, text content is present in <title>, <author>, <year>, and <price>. They contain text (like 350). Element contents are present in <bookstore> and <book>. They contain elements. An attribute (category=”Child”) is present in <book>.

Empty XML Elements:

An empty element is the one with no content. An empty element in XML is indicated by:

<element></element>

A self-closing tag can also be used.

</element>

Identical results are produced in XML software (Readers, Parsers, Browsers) by any of the above forms. Attributes can be included in an empty element.

XML Naming Rules:

The following rules must be followed by each XML elements:

  • Element names are case-sensitive.
  • Element names must start with a letter or underscore.
  • Element names cannot start with the letters XML (or Xml, or xml, etc).
  • Element names can contain letters, digits, hyphens, underscores, and periods.
  • Element names cannot contain spaces.
  • Any name can be used as Element names. No words are reserved, except XML.

Best Naming Practices:

  • Descriptive names like: <person>, <firstname>, <lastname>, should be created.
  • Short and simple names like: <book_title> should be created instead of something like: <the_title_of_the_book>.
  • Avoid “-“, because, for example, if we use “first-name”, some software may think we want to subtract “name” from “first”.
  • Avoid “.”, because, for example, if we use “first.name”, some software may think that “name” is a property of the object “first”.
  • Avoid “:”, because the colons in XML are reserved for namespaces.
  • Non-English letters, for example, éòá are perfectly legal in XML. It however might be possible that our software doesn’t support them.

Naming Styles:

No naming styles are defined for XML elements. However, some commonly used naming styles are:

Style Example Explanation
Lower case <firstname> All letters lower case
Upper case <FIRSTNAME> All letters upper case
Underscore <first_name> Underscore separates words
Pascal case <FirstName> Uppercase first letter in each word
Camel case <firstName> Uppercase first letter in each word except the first
  • A naming style is good to be consistent.
  • A corresponding database is possible for XML documents. The naming rules of the database are commonly used for the XML elements.
  • A common naming rule in JavaScripts is the Camel case.

XML Elements are Extensible:

To carry more information, if required, the XML elements can be extended.

Example:

<?xml version="1.0" encoding="UTF-8"?>
<note>
  <to>Sapna</to>
  <from>Tom</from>
  <body>At 11 AM on Monday morning.</body>
</note>

Output:

MESSAGE

To: Sapna

From: Tom

At 11 AM on Monday morning.

Explanation:

Here, we are assuming that an application is created to extract the <to>, <from>, and <body> elements from the XML document to produce the above output.

Now, if some extra information is added by the author of the XML document, should the application break or crash? Obviously No, coz the application should still be able to find the <to>, <from>, and <body> elements in the XML document and produce the same output. This means that the XML can be extended without breaking applications.

Example:

<?xml version="1.0" encoding="UTF-8"?>
<note>
<date>2020-06-04</date>
  <to>Sapna</to>
  <from>Tom</from>
  <heading>Meeting</heading>
  <body>At 11 AM on Monday morning.</body>
</note>
Please Share