XML has a strict hierarchical syntax. The XML documents must contain a single
root element, with child elements nested within the root element. Each element
is defined with a start and close tag.users.xml
<user>
<name>
<forename>Gez</forename>
<surname>Lemon</surname>
</name>
<email>[email protected]</email>
</user>
In the above example, "user" is the root element. The "name" and "email" elements are child elements of "user". The name element is further defined as having child elements "forname" and "surname".
The text between the tags is the content of the element.
Unlike HTML, whitespace is preserved with the contents of the elements. The following example will leave 5 spaces after the "forename" element.
<user>
<name>
<forename>Gez </forename>
<surname>Lemon</surname>
</name>
<email>[email protected]</email>
</user>
If the content is converted to HTML, then the browser will ignore the white
space. XML is case sensitive. Specifying a closing tag of /User
in the above
example would be incorrect.
Like HTML, XML elements may have attributes in the start tag to provide extra information about that element. Attributes are name/value pairs. For example, the following might be the attributes for the <img> element in HTML.
<img src="images/logo.gif" alt="Logo" width="100" height="50" />
In XML, all attributes must be assigned values, and the values must be in quotes. The attribute name is case-sensitive. We could rewrite our previous example so that email was an attribute rather an element of the structure.
users.xml
<user email="[email protected]">
<name>
<forename>Gez</forename>
<surname>Lemon</surname>
</name>
</user>
Attributes and child elements appear to achieve the same thing. However, attributes can be in any order and there may only be one instance of the attribute. If the attribute name is long it starts to get difficult to read, and attributes can't have a sub-structure. Elements can be ordered and the multiplicity can be defined. Elements may have a sub-structure and the length of the name is less important.
If order, structure or multiplicity are required, then an element should be
used. One of the most common use of attributes in XML is where the information
is not actually part of the data. For example, using an identifier for the "name" element. users.xml
< user>
<name nameid="64">
<forename>Gez</forename>
<surname>Lemon</surname>
</name>
<email>[email protected]</email>
</user>
This enables a relationship to be defined from an external entity. The following example is a list of jobs that may be contracted. The constractor and placement are stored in a different data source, and linked using the nameid and placeid attributes respectively.
contract.xml
<contract>
<job>
<contractor nameid="64"/>
<placement placeid="23"/>
<rate>Free</rate>
</job>
...
</contract>
Comments