. Element names are case-sensitive, must start with a letter or underscore, and may contain letters, digits, hyphens, underscores, and periods. Attributes appear inside the start tag of an element and provide additional metadata about that element. Each attribute consists of a name–value pair separated by an equals sign, with the value enclosed in quotes. An element cannot have two attributes with the same name. The choice between using an attribute versus a child element is a design decision: attributes are generally preferred for metadata or identifiers, while child elements are preferred for data that may contain structured content or that may repeat. Text nodes hold the character data between tags. They may contain any Unicode characters except the reserved characters less-than (<), ampersand (&), and in attribute values, the quote character used as delimiter. These characters must be escaped using predefined entity references: < for <, & for &, > for >, ' for single quote, and " for double quote.
XML
Beginner
1 min read
Elements, Attributes, and Text Nodes
. Element names are case-sensitive, must start with a letter or underscore, and may contain letters, digits, hyphens, underscores, and periods. Attributes appear inside the start tag of an element and provide additional metadata about that element. Each attribute consists of a name–value pair separated by an equals sign, with the value enclosed in quotes. An element cannot have two attributes with the same name. The choice between using an attribute versus a child element is a design decision: attributes are generally preferred for metadata or identifiers, while child elements are preferred for data that may contain structured content or that may repeat. Text nodes hold the character data between tags. They may contain any Unicode characters except the reserved characters less-than (<), ampersand (&), and in attribute values, the quote character used as delimiter. These characters must be escaped using predefined entity references: < for <, & for &, > for >, ' for single quote, and " for double quote.
Example
<?xml version="1.0" encoding="UTF-8"?>
<library>
<!-- Element with attributes and a text node child -->
<book id="b001" available="true">
<title>Learning XML</title>
<author>Erik Ray</author>
<!-- Numeric text content -->
<pages>416</pages>
<!-- Attribute value with entity reference -->
<publisher name="O'Reilly Media" />
<!-- Text node with entity references -->
<description>
This book covers XML & related standards.
Price is < $50.
</description>
</book>
<!-- Self-closing element (empty element) -->
<placeholder id="b002" />
<!-- Element names: case-sensitive -->
<Book>This is different from <book> above</Book>
</library>