XML Document: Dissection
Excuse the unpleasant title of this XML lesson, but the name is suited for what we are about to do. In this lesson, we will be taking apart and analyzing each part of an XML document. Consider this an overview of the XML document, and do not be unsettled if you don't understand all that is discussed in this lesson. We will cover all these parts in greater detail throughout the tutorial.
Parts of an XML Document
An XML document is made up of a small group of building blocks. W3C wanted to keep XML simple, and judging by the relatively few components of an XML document, they seem to have succeeded. We have divided the components into sections and subsections.
- XML Declaration
- DTD Declaration
- Root Element (Document)
- Elements (Nested Elements)
- Element Attributes and Values
Webpage standards now recommend that an HTML document follow this form, so that they can be considered valid XML. Fun Fact: In an HTML file the root element is <html>.
The XML prolog is an optional piece of information that must come before the root element when used. There are two pieces that make up the prolog: the XML declaration and the Document Type Declaration (DTD).
The XML declaration states which version of XML you are using, while the DTD describes the rules your XML document must follow. If you are just dying to know more about the DTD and can't wait to read through the lessons as we have laid them out (we won't be offended), check out our XML DTD lesson.
XML Root Element (Document)
The root element, also referred to as the "document", dictates what kind of XML document it is. When creating an HTML file that is XML complaint, the root element will be <html>. The root element must be the first element in an XML document and there can only be one root element per file!
XML Element & Attributes
The meat of an XML document comes from the elements that are contained within the root element. Each element represents a different type of data that is being stored in the document. The element <p> might represent paragraph text, while the element <gif> may contain data for a GIF image.
Although not required, elements often have attributes that are associated with them. XML attributes are similar to HTML attributes in that they have a name:value relationship. An example attribute for an <img> element might be "src", which represents the source location of the image.
XML Element & Attribute Code:
<img src = "C:/MyDocs/Happy.gif"></img>
In addition to the information stored in the element itself (attributes), the bulk of data in XML usually appears between the opening and closing tag of an XML element. This is often referred to as the XML's "content". Below, we have an imaginary XML document that stores a story.
<title>The Truth About the Family</title>
<paragraph>Here's a story of a lovely lady who...</paragraph>
<paragraph>...they became the Brady Bunch...</paragraph>
Quick Learning Check:
Can you tell which element is the root element in the above XML code? If you thought <story>, then you were right on the money!
Download Tizag.com's XML Book
If you would rather download the PDF of this tutorial, check out our
XML eBook from the Tizag.com store.
Found Something Wrong in this Lesson?
Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time!