Element and Attribute Structure in XML
Under the optional XML declarations, every XML document contains a single-value root element, represented in this case by the rootelement element:
Other elements and text values can be nested under the root element, but the root element must be first in the list and unique in the document. This can be compared to a computer hard drive, which contains one root directory, with files and/or subdirectories
under the root directory.
Next in the sample XML document are the nested elements, attributes, and text, as illustrated by the nested firstelement under the root element in our example:
This is level 1 of the nested
The firstelement has an attribute called position with a value of 1. The position attribute provides additional data related to firstelement. In this case it indicates that the original sorting position of the first element in the XML document is 1. If the XML document data is altered and the order of the elements is rearranged as part of that alteration, the position element may be useful for reordering the element, or could be changed when the document is altered to reflect a new position of the element in the XML document, regardless of the element name. In general, attributes are great for adding more information and descriptions to the values of elements, and the text associated with elements, as shown in the previous example.
Nested under the firstelement element is the level1 element, which contains an attribute, called children. The element name is used to describe the nesting level in the XML document, and the value of the children attribute is used to describe how many more levels of nesting are contained under the level1 element, in this case, no more nested levels (0). The phrase This is level 1 of the nested elements represents a text data value that is part of the level1 element. Text data contains values associated with a tag.
The second element under the root element is called secondelement and is a variation of the firstelement element. Let’s compare the firstelement and secondelement elements to get a better sense of the structure of the document:
This is level 2 of the nested
Like the firstelement, the secondelement has an attribute called position, this time with a value of 2. Nested under the secondelement element is another level1 element. The existence of this element illustrates the fact that well-formed XML documents can have more than one instance of the same element name. The only exception to this is the root element, which must be unique.
Also, like the firstelement element, the level1 element also has an attribute called children. The level1 element is again used to describe the nesting level in the XML document, and the attribute is used to describe how many more levels of nesting are contained under the level1 element. In this case, the children attribute indicates that there is one more nesting level (1) inside the level1 element. The phrase This is level 2 of the nested elements inside the level2 element represents text data for the level2 element. Last but not least, to finish the XML document, the rootelement tag is closed: