XML & DTDs
Computing Resources >> Tutorials >> Web Development >> XML & DTDs  

Introduction and Objectives

XML Anatomy

Creating a Simple XML Document

"Well Formed" vs. Valid

Is Your Markup Well-Formed?

Creating a DTD

Validating with a DTD

XML Resources

Evaluate this tutorial

PDF Handout

Creating a Simple XML Document

Now that you know the basic rules for creating an XML document, let's try them out.

Like most, if not all, standards developed by the W3Group, you can create XML documents using a plain text editor like Notepad (PC), TextEdit (Mac), or pico (Unix). You can also use programs like Dreamweaver and Cooktop, but all that is necessary to create the document is a text editor.

Let's say we have two types of documents we would like to wrap in XML: emails and letters. We want to encode the emails and letters because we are creating an online repository of archival messages within an organization or by an individual. By encoding them in XML, we hope to encode their content once and be able to translate it to a variety of outputs, like HTML, PDFs, or types not yet created.

To begin, we need to declare an XML version:

<?xml version="1.0" encoding="iso-8859-1"?>

Now, after declaring the XML version, we need to determine the root element for the documents. Let's use message as the root element, since both email and letters can be classified as messages.

<?xml version="1.0" encoding="iso-8859-1"?>
<message>
</message>

Note: You might have noticed that I created both the opening and closing tags for the message element. When creating XML documents, it is useful to create both the opening and closing elements at the same time. After creating the tags, you would then fill in the content. Since one of the fatal errors for XML is forgetting to close an element, if you make the opening and closing tags each time you create an element, you won't accidentally forget to do so.

Parent and child relationships

A way of describing relationships in XML is the terminology of parent and child. In our examples, the parent or "root" element is <message>, which then has two child elements, <email>, and <letter>.

An easy way of showing how elements are related in XML is to indent the code to show that an element is a child of another. For example,

<?xml version="1.0" encoding="iso-8859-1"?>
<message>

<email>
</email>
</message>

Now that we have the XML declaration, the root element, and the child element (email), let's determine the information we want to break out in an email. Say we want to keep information about the sender, recipients, subject, and the body of the text. Since the information about the sender and recipients are generally in the head of the document, let's consider them children elements of a parent element that we will call <header>. In addition to <header>, the other child elements of <email> will be <subject> and <text>. So our XML will look something like this:

<?xml version="1.0" encoding="iso-8859-1"?>
<message>

<email>
<header>
<sender>me@ischool.utexas.edu</sender>
<recipient>you@ischool.utexas.edu</recipient>

</header>
<subject>Re: XML
</subject>
<text>I'm working on my XML project right now.
</text>

</email>

</message>

Now, let's create an XML document for a letter. Some of the information in a letter we want to know include the sender, the recipient, and the text of the letter. Additionally, we want to know the date that it was sent and what salutation was used to start off the message. Let's see what this would look like in XML:

<?xml version="1.0" encoding="iso-8859-1"?>
<message>

<letter>
<letterhead>
<sender>Margaret</sender>
<recipient>God</recipient>
<date>1970</date>
</letterhead>
<text>
<salutation>Are you there God?</salutation>
It's me Margaret...
</text>
</letter>

</message>

Now say we wanted to keep track of whether or not these messages were replies or not. Instead of creating an additional element called <reply>, let's assign an attribute to the elements <email> and <letter> indicating whether that document was a reply to a previous message.

In XML, it would look something like this:

<email reply="yes">

or

<letter reply="no">

When creating XML documents, it's always useful to spend a little time thinking about what information you want to store, as well as what relationships the elements will have. Now that we've made some XML documents, let's talk about "well formed" XML and valid XML.

 

next section >

 

Watch the video
screenshot
Choose format/speed:

real
media dial-up | broadband
real media dial-up | broadband

Entire tutorial captioned
real media dial-up | broadband
windows media dial-up | broadband

Flash version of tutorial
segment | entire

html transcript

© 2004 Jacob Cleary | iSchool | UT Austin | webmaster