Creating an Example XML Document
The sample document you'll use today, and which you'll also see tomorrow when working with DTDs, will store data about a set of employees, such as their names, projects they're working on, and so on. This document will start, as all XML documents should, with an XML declaration:
<?xml version = "1.0"?>
Because all the documents you'll see today are self-contained (they don't refer to or include any external entities), you'll also add the standalone attribute, setting it to "yes", and specify that we're using UTF-8 encoding:
<?xml version = "1.0" encoding="UTF-8" standalone="yes"?>
And you'll also add a root element, called <document> in this case, although you can use any legal name:
<?xml version = "1.0" encoding="UTF-8" standalone="yes"?> <document> . . .
</document>
The root element will contain all the other elements in the document. In this case, that will be three <employee> elements:
<?xml version = "1.0" encoding="UTF-8" standalone="yes"?> <document> <employee> . . . </employee> <employee> . . . </employee> <employee> . . . </employee> </document>
For each employee, we can store a name in a <name> element, which itself encloses a <lastname> and <firstname> element:
<?xml version = "1.0" encoding="UTF-8" standalone="yes"?> <document> <employee> <name> <lastname>Kelly</lastname> <firstname>Grace</firstname> </name> . . . </employee> . . . </document>
We'll also store each employee's hire date, as well as the projects they're working on. For each project, we can store the product name, ID, and price:
<?xml version = "1.0" encoding="UTF-8" standalone="yes"?> <document> <employee> <name> <lastname>Kelly</lastname> <firstname>Grace</firstname> </name>
<hiredate>October 15, 2005</hiredate>
<projects>
<project>
<product>Printer</product>
<id>111</id>
<price>$111.00</price>
</project>
<project>
<product>Laptop</product>
<id>222</id>
<price>$989.00</price>
</project>
</projects>
</employee> . . . </document>
That's what the data looks like for one employee; you can see the full document, ch03_01.xml, in Listing 3.1. Documents like this one can grow very long, but that presents no problem to XML processorsas long as the document is well-formed.
Listing 3.1 Sample Well-Formed XML Document (ch03_01.xml)
<?xml version = "1.0" encoding="UTF-8" standalone="yes"?> <document> <employee> <name> <lastname>Kelly</lastname> <firstname>Grace</firstname> </name> <hiredate>October 15, 2005</hiredate> <projects> <project> <product>Printer</product> <id>111</id> <price>$111.00</price> </project> <project> <product>Laptop</product> <id>222</id> <price>$989.00</price> </project> </projects> </employee> <employee> <name> <lastname>Grant</lastname> <firstname>Cary</firstname> </name> <hiredate>October 20, 2005</hiredate> <projects> <project> <product>Desktop</product> <id>333</id> <price>$2995.00</price> </project> <project> <product>Scanner</product> <id>444</id> <price>$200.00</price> </project> </projects> </employee> <employee> <name> <lastname>Gable</lastname> <firstname>Clark</firstname> </name> <hiredate>October 25, 2005</hiredate> <projects> <project> <product>Keyboard</product> <id>555</id> <price>$129.00</price> </project> <project> <product>Mouse</product> <id>666</id> <price>$25.00</price> </project> </projects> </employee> </document>
Today's work gets us into the structure of XML documents, and there's some terminology we should get to know at this point having to do with the relative position of elements in an XML document. As an example, take a look at an employee element in ch03_01.xml.
Elements on the same level, such as <name>, <hiredate>, and <projects> in an <employee> element, are all called siblings. Similarly, the two <project> elements in each <projects> element are siblings.
This family-type relationship is also continued with child and parent relationships. For example, the parent of the two <project> elements is the <projects> element. And the two <project> elements are children of the <projects> element.
You can always count on every non-root element to have exactly one, and only one, parent element. And a parent element can enclose an indefinite number of child elements (which can also mean zero child elements). You can continue the analogy to multiple generations as well; for example, the two <project> elements in this case are also grandchildren of the <employee> element.
That gives us the example document and terminology we'll need; now let's take a look at the well-formedness constraints you'll find in XML.