XML Elements
An XML document contains XML elements.
What Is an XML Element?
An XML element refers to the part from (and including) the start tag to (and including) the end tag.
An element can contain:
- Other elements
- Text
- property
- or a mix of all of the above...
<
bookstore
<
book
category
=
"
CHILDREN
"
<
titleHarry Potter
title
<
author
J K. Rowling
author
<
year
2005
year
<
price
29.99
price
book
<
book
category
=
"
WEB
"
<
titleLearning XML
title
<
author
Erik T. Ray
author
<
year
2003
year
<
price
39.95
price
book
bookstoreIn the example above,
XML Naming Rules
XML elements must follow these naming rules:
- Names can contain letters, digits, and other characters
- A name cannot begin with a number or a punctuation mark
- Names cannot begin with the letters xml (or XML, Xml, etc.)
- Names cannot contain spaces
Any name can be used, there are no reserved words.
Best naming practices
Make names descriptive. Using names with underscores is also nice:
The name should be short and simple, for example:
Avoid the "-" character. If you name things this way: "first-name", some software will think you want to subtract name from first.
Avoid the "." character. If you name things this way: "first.name", some software will consider "name" to be a property of the object "first".
Avoid the ":" character. The colon is converted into a namespace for use (introduced later).
XML documents often have a corresponding database, where the fields correspond to elements in the XML document. A useful rule of thumb is to use the database's naming conventions to name elements in the XML document.
In XML, non-English letters such as éòá are perfectly legal, but you need to be aware of potential problems that may arise when your software vendor does not support these characters.
XML elements are extensible
XML elements are extensible to carry more information.
See the following XML example:
<
note
<
to
Tove
</
to
<
from
Jani
</
from
<
bodyDon't forget me this weekend!
</
body
</
noteLet's imagine we create an application that can extract
| MESSAGE
To: Tove
From: Jani
Don't forget me this weekend!
| | --- |Imagine that the author of the XML document added some extra information:
<
note
<
date
2008-01-10
date
<
to
Tove
to
<
from
Jani
from
<
heading
Reminder
heading
<
bodyDon't forget me this weekend!
body
note
Will this application break or crash?
No. The application can still find the
One of the advantages of XML is that it can be extended without breaking applications.