XML Elements
This article discusses XML elements and their syntax.
XML elements are represented by tags. Elements usually consist of an opening tag and a closing tag, but they can consist of just one tag.
Opening tags consist of <
, followed by the element name, and ending with >
. Closing tags are the same but have a forward slash inserted between the less than symbol and the element name.
Example:
Empty elements are closed by inserting a forward slash before the greater than symbol.
Example of empty tag:
The following syntax rules are important to note, especially if you're used to working with HTML where you don't usually need to worry about these rules.
All Elements Must Be Closed Properly
If you're familiar with HTML, you will know that some HTML tags don't need to be closed. In XML however, you must close all tags. This is usually done in the form of a closing tag where you repeat the opening tag, but place a forward slash before the element name (i.e. </child>
). If you are using an empty element (i.e. a tag with no closing tag), you need to place a forward slash before the greater than symbol at the end of the tag (eg, <child />
).
Example for opening/closing tags:
Example for empty elements:
Tags Are Case Sensitive
All tags must be written using the correct case. XML sees <tutorial>
as a different tag to <Tutorial>
Wrong:
Right:
Elements Must Be Nested Properly
You can place elements inside other elements but you need to ensure each element's closing tag doesn't overlap with any other tags.
Wrong:
Right:
Element Names
You can use any name you like for your elements as long as they adhere to the following rules:
- Element names can contain any character (including letters and numbers)
- Element names must not contain spaces
- Element names must not begin with a number or punctuation character (for example a comma or semi-colon etc)
- Element names must not start with the letters
xml
(whether lowercase, uppercase, or mixed case)
You shouldn't use a colon (:
) in your element names, as this is reserved for another purpose.