XML Elements
XML Elements
Section titled “XML Elements”An XML document is structured using XML Elements.
What is an XML Element?
Section titled “What is an XML Element?”An XML element consists of everything from its start tag to its end tag, including the content between them.
An element can contain:
- Text content
- Other nested elements (child elements)
- Attributes (metadata about the element)
- A mix of the above
- Nothing (an empty element)
Example of an XML document containing various elements:
<?xml version="1.0" encoding="UTF-8"?><bookstore> <book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price currency="USD">29.99</price> </book> <book category="WEB"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price currency="USD">39.95</price> </book></bookstore>In this example:
<bookstore>is the root element, containing two<book>elements.- Each
<book>element contains child elements:<title>,<author>,<year>, and<price>. - The
<book>element also has an attributecategory. - The
<title>element has an attributelang. - The
<price>element has an attributecurrency. <title>,<author>,<year>, and<price>contain text content.
Empty XML Elements
Section titled “Empty XML Elements”An element with no content is called an empty element. Empty elements can be represented in two ways:
- Using a start tag followed immediately by an end tag:
<data></data>- Using a self-closing tag (more concise):
<data/>Both forms are equivalent to an XML parser. Note that empty elements can still have attributes:
<img src="logo.png" alt="Company Logo"/>XML Naming Rules
Section titled “XML Naming Rules”XML element and attribute names must adhere to these rules:
- Names are case-sensitive (e.g.,
<book>is different from<Book>). - Names must start with a letter (a-z, A-Z) or an underscore (_).
- Names cannot start with the sequence
xml(orXML,Xml, etc.). - After the first character, names can contain letters, digits (0-9), hyphens (-), underscores (_), and periods (.).
- Names cannot contain spaces.
- Colons (:) are reserved for namespaces and should not be used in regular names.
Almost any name can be used, as long as it follows these rules. There are no reserved keywords apart from the xml prefix restriction.
Best Naming Practices
Section titled “Best Naming Practices”While the rules are flexible, following best practices improves readability and maintainability:
- Be Descriptive: Use names that clearly indicate the element’s purpose or content (e.g.,
<customerAddress>,<orderItem>). - Be Consistent: Choose a naming convention and stick to it throughout your XML document or project.
- Avoid Ambiguity: While hyphens and periods are allowed, be mindful that some programming languages or tools might interpret them specially if XML data is mapped directly to code objects. Using underscores (
first_name) or camelCase (firstName) is often safer for broader compatibility. - Consider Readability: Short, meaningful names are generally preferred over very long ones (e.g.,
<bookTitle>instead of<theCompleteTitleOfTheBookInQuestion>). - Character Encoding: While non-ASCII characters (like é, ö, å) are allowed in names, ensure your XML document declares UTF-8 encoding (
<?xml version="1.0" encoding="UTF-8"?>) and that all processing software correctly handles UTF-8 to avoid issues.
Common Naming Styles
Section titled “Common Naming Styles”There’s no single official naming style for XML, but consistency is key. Common styles include:
| Style | Example | Description |
|---|---|---|
| lower case | <firstname> | All letters are lowercase. Words can be hard to distinguish. |
| UPPER CASE | <FIRSTNAME> | All letters are uppercase. Sometimes used for constants or root elements. |
| snake_case | <first_name> | Words are separated by underscores. Common in databases and some programming languages. |
| PascalCase (UpperCamelCase) | <FirstName> | Each word starts with an uppercase letter. Common in object-oriented programming. |
| camelCase (lowerCamelCase) | <firstName> | The first word starts lowercase, subsequent words start uppercase. Common in JavaScript and Java. |
| kebab-case | <first-name> | Words are separated by hyphens. Common in HTML custom elements and CSS. |
Choose a style that fits your project context (e.g., matching database column names or programming language conventions) and apply it consistently.
XML Elements are Extensible
Section titled “XML Elements are Extensible”One of the key strengths of XML is its extensibility. You can add new elements or attributes without necessarily breaking existing applications that process the document.
Consider this initial XML document:
<?xml version="1.0" encoding="UTF-8"?><note> <to>Tove</to> <from>Jani</from> <body>Don't forget me this weekend!</body></note>An application might be designed to extract only the <to>, <from>, and <body> elements to display a message.
Later, the XML author might add more information:
<?xml version="1.0" encoding="UTF-8"?><note importance="high"> <date>2023-10-27</date> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body></note>A well-designed application that specifically looks for <to>, <from>, and <body> should still function correctly. It would simply ignore the new <date>, <heading> elements and the importance attribute.
This allows XML formats to evolve over time. Applications can choose to support new elements/attributes as needed, while older applications can often continue working with the data they understand.