Contents
Overview 1
Lesson: The Form of XML 2
Lesson: Designing an XML Vocabulary 13
Lesson: Namespaces 17
Lab 2: Designing an XML Vocabulary 25
Review 28
Module 2: Basic XML
Information in this document, including URL and other Internet Web site references, is subject to
change without notice. Unless otherwise noted, the example companies, organizations, products,
domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious,
and no association with any real company, organization, product, domain name, e-mail address,
logo, person, places or events is intended or should be inferred. Complying with all applicable
copyright laws is the responsibility of the user. Without limiting the rights under copyright, no
part of this document may be reproduced, stored in or introduced into a retrieval system, or
transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or
otherwise), or for any purpose, without the express written permission of Microsoft Corporation.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual
Materials and Preparation
This section provides the materials and preparation tasks that you need to teach
this module.
To teach this module, you need the following materials:
• Microsoft
®
PowerPoint
®
file 2500A_02.ppt
To prepare for this module:
!
Read all of the materials for this module.
!
Complete all practices.
!
Complete the demonstrations.
!
Complete the lab.
This module contains code examples that are linked to text hyperlinks at the
bottom of PowerPoint slides. These examples enable you to teach from code
examples that are too long to display on a slide. All the code examples for this
module are contained in one .htm file. Participants can copy the code directly
from the browser or from the source, and paste it into a development
environment.
To display a code sample, click a text hyperlink on a slide. Each link opens an
instance of Microsoft Internet Explorer and displays the code associated with
the link. At the end of each example, a link displays a table of contents of all
Encoding schemes, such as Unicode.
A good place to start is Microsoft MSDN
®
online at
Recommended Reading
Module 2: Basic XML v Module Strategy
Use the following strategy to present this module:
!
The Form of XML
Explain the rules of form that govern well-formed XML.
!
Designing an XML Vocabulary
This lesson can help you to prepare to explain the next lesson, which is
about namespaces, and the next module, which is about validation.
!
Namespaces
This module provides a very basic introduction to the idea of namespaces.
Most of the time, learners have trouble understanding two points regarding
namespaces. The first point of difficulty comes in understanding situations
that require their use. The lesson provides a concrete example of why you
might need to differentiate one element name from another in the context of
combining data from two separate XML sources. This is probably good
enough to get you through this lesson. You will see in subsequent modules
plenty of examples of namespaces, especially in the chapter on XSLT. The
second point that learners have trouble understanding is the logic behind
Create an XML vocabulary that meets a set of business requirements.
!
Use a default or an explicit namespace.
Objectives
2 Module 2: Basic XML Lesson: The Form of XML
!
Parts of an XML Document
!
What Is Well-Formed XML?
!
Rules for Elements
!
Rules for Attributes
!
Processing Instructions
!
Comments
!
How to Handle Reserved Characters
*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
In this lesson, you will examine the rules that govern the form of XML.
<planet ID="1">
<name>Mercury</name>
</planet>
<planet ID="2">
<name>Venus</name>
</planet>
<!-- There are more planets. -->
</planets>
Processing instructions
Elements
Attributes
Comments
Root element
Child elements
*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
This topic describes the key parts of an XML document.
Most XML documents begin with an instruction to the XML processor. Here,
the XML processor is instructed that the document is formed according to the
World Wide Web Consortium (W3C) XML Recommendation version 1.0.
A set of nested elements usually comes after the processing instruction. An
element usually consists of a start tag and a closing tag pair. Between the start
tag and closing tag pair, an element may contain data content or other elements.
An element may consist of just the closing tag.
The first element that the XML processor encounters must consist of a start tag
and a closing tag. This first element contains all other elements and is called the
Application
Application
Well-Formed XML Not Well-Formed XML
Closing tag ‘temp’
does not match the
start tag ‘Temp’
Line 1, Position 11
Closing tag ‘temp’
does not match the
start tag ‘Temp’
Line 1, Position 11
Error!
Error!
<Temp>22</Temp>
<Temp>22</Temp>
*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
For an XML document to be processed and shared between applications, it
must be formed according to a rigid set of rules. An XML document that
conforms to a set of rules is said to be well-formed. The current rules that
govern the form of XML are stated in the W3C Recommendation entitled
XML 1.0.
The XML 1.0 Recommendation differentiates an XML processor from an
application. An XML processor is any software that reads XML so that you can
access the document content and structure. XML processors are also called
parsers.
No space after the left angle bracket (<)
!
The case of start and closing tags must match
!
The first element is the root element
!
The root element must have start and closing tags
!
All child elements must nest within the root
!
Nesting elements cannot overlap
!
An empty child element can consist of a single tag
<ElementName />
<ElementName />
<ElementName>element content</ElementName>
<ElementName>element content</ElementName>
<Root>
<ChildA>
<ChildB>content
</ChildB>
</ChildA>
</Root>
<Root>
<ChildA>
<ChildB>content
</ChildB>
</ChildA>
</Root>
XML Example
!
The nesting of elements cannot overlap.
!
An element that lacks content can consist of a single closing tag unless it is
the root element.
!
The root element must consist of a start tag and a closing tag, even if it has
no content.
Introduction
S
yntax
Rules for elements
6 Module 2: Basic XML Find violations of form in the elements of the following XML document:
elements.xml
<cloud-types>
<High-level>
<name>cirrus</name>
<name>cirrostratus</name>
</High-Level>
<Mid-Level>
<name>altocumulus</name>
<name>altostratus</name>
</Mid-Level>
<Low-Level>
<name>nimbostratus</name>
<name>stratocumulus</name>
Rules for Attributes
!
Declare them in start tags and processing instructions
!
Separate multiple declarations with a space
!
An attribute consists of a name and an assignment
"
Each name must be unique within an element
"
You can reuse names throughout a document
"
There are no spaces in names
"
Use either single or double quotes for assignments
<tree species ="Salix">Willow</tree>
<tree species ="Salix">Willow</tree>
Name
Name
Assignment
Assignment
XML Example
*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
Rules for attributes
Example
8 Module 2: Basic XML
Find violations of form in the attributes of the following XML document:
attributes.xml
<cloud-types>
<High-Level al t='+6,000 meters'>
<name latin='curl of hair'>cirrus</name>
<name latin='curl of hair + layer'>cirrostratus</name>
</High-Level>
<Mid-Level ALT='2000 - 6000 meters'>
<name latin='high heap'>altocumulus</name>
<name latin='high layer'>altostratus</name>
</Mid-Level>
<Low-Level alt '-2000 meters'>
<name latin='rain layer'>nimbostratus</name>
<name latin='layer heap'>stratocumulus</name>
</Low-Level>
<Vertical-Development alt='>12000 meters'>
<name latin='heap'>fair weather cumulus</name>
<name latin='heap rain' latin='heap
rain'>cumulonimbus</name>
</Vertical-Development>
<Other-Types alt="variable'>
<name>contrails</name>
<name>billow clouds</name>
<name>mammatus</name>