XML Nested Elements
One of the main purposes of the development of XML was to enable inter-operability between information and the application that used or managed it. As an application is driven by logic, it is natural that the developer of the application expects data and information to be structured and organized as well. The fundamental unit of an XML document is, as discussed in previous sessions, an element. Any entity that is described as part of an XML document has to be organized as an element. An element can have other elements as part of its content. Content is thus versatile, not just character-encoded constant values such as numbers and alphanumeric strings. Even empty lines are legal content!
A few points need to be made at this point, and these will be followed by an appropriate example. The syntax of an XML document can be described as:
- An XML document's contents should be enclosed within a single element at the top level.
- An XML document can be viewed as a rooted tree of nodes (each node is an element) and thus has a recursive structure.
- Just as parantheses need to be closed properly in a (possibly complex) arithmetic expression, start and end tags of elements that may (or may not) have child nodes as content, should be correctly matched. In particular, overlapping of end tags of different elements must be avoided.
- Individual elements of the same category can have different numbers and types of elements as content.
Now is an appropriate time to discuss a real-world example. Suppose a joint family of monkeys living on a banyan tree decide to set up a business, with several adult monkeys having different responsibilities:
<monkey-business>
<venture-name>Simian Platform</venture-name>
<monkey-role>
<title>Production Damager</title>
<name>King Kong</name>
<responsibility>Stop-erations</responsibility>
<subordinates>Jerks</subordinates>
</monkey-role>
<monkey-role>
<title>Deceptionist</title>
<name>Dunston</name>
<responsibility>Curt-essy</responsibility>
</monkey-role>
</monkey-business>
In this snippet of legal XML, you can easily see how rules 1-4 are highlighted.
The element "monkey-business" is the top level root element of the "inverted-tree- visualization" of the XML document. The element category "monkey-role" is for labeling employee categories, and each element has several nested elements inside it (same as nested if All start tags are matched correctly with end tags, and we can see that the 2 elements of this category have different numbers and types of nested elements.
It is very logically organized. You can easily observe, if you have any programming experience, that programmed logic in an application can simply extract information from the organization of this document. That's the beauty of XML, and this convenience has led to its humongous presence on the web and in application software.