XML Elements
An XML element is the fundamental building block of an XML document. It represents a single piece of structured data, defined by a start-tag, some content, and an end-tag. Understanding how to properly create and name elements is essential for writing valid and well-formed XML.
What is an XML Element?
An XML element is anything delimited by start and end tags or an empty-element tag in the case of empty elements. In practice, it defines a "container" for a piece of data.
Example of an Element:
<name>Tom Nolan</name>
An element can contain:
- Text: Simple text content.
- Other Elements: Creating a nested, hierarchical structure.
- Attributes: To provide metadata about the element.
- A mix of the above, or be completely empty.
Each XML document contains one or more elements.
Syntax of an Element
The syntax is as follows:
<element-name attribute1 attribute2>
...content
</element-name>
where:
- element-name is the name of the element.
- attribute1, attribute2 are attributes of the element separated by white spaces. Each attribute defines a property
of the element and it is written as
name="value"where value is a string inside single''or double""quotes. - ...content is the content of the element.
Syntax for Empty Element
An element with no content is called an empty element. It can be written in two ways, but the second is highly recommended.
1. A Start-Tag Immediately Followed by an End-Tag:
<linebreak></linebreak>
2. A Self-Closing Tag (Recommended): A more concise and common way to represent an empty element is with a single "self-closing" tag, which includes a forward slash before the closing angle bracket.
<linebreak/>
This is the recommended best practice for empty elements.
Empty elements can have attributes.
Example
Example with some XML elements:
<?xml version = "1.0"?>
<contact-info>
<address category="residence">
<name>Tom Nolan</name>
<company>ACME Inc.</company>
<phone>(123) 456-6789</phone>
</address>
</contact-info>
XML Element Naming Rules
XML elements are containers that can contain text or elements, and they must follow these rules:
- Case-Sensitive: Element names are case-sensitive.
<Book>and<book>are two different tags. - Start Character: Names must start with a letter or an underscore (
_). They cannot start with a number or any other punctuation. - Allowed Characters: After the first character, names can contain letters, digits, hyphens (
-), underscores (_), and periods (.). - No Spaces: Element names cannot contain spaces.
- No
xmlPrefix: Names cannot start with the lettersxml(in any case, likeXML,Xml, etc.), as this is reserved.
Common Naming Conventions
While XML has no official naming style, developers have adopted several common conventions to keep their documents readable and consistent. The most important thing is to choose one style and use it consistently throughout your document.
| Style | Example | Description |
|---|---|---|
| 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 |
Conclusion
XML elements are the foundation of any XML document. By following the strict syntax and naming rules, you create a "well-formed" document that is reliable and can be processed by any standard XML parser.
- An element is a complete unit, from its start-tag to its end-tag.
- Element names are case-sensitive and have strict rules for allowed characters.
- Choose a consistent naming convention for your elements to make your XML documents easy to read and maintain.