Contents
Overview 1
Introduction to XML 2
Validating XML Documents 20
Using the Document Object Model 31
Applying XML in N-Tier Applications 53
Lab 9: Exchanging Data Using XML 60
Best Practices 67
Review 69
Module 9: Using XML to
Exchange Data Information in this document is subject to change without notice. The names of companies,
products, people, characters, and/or data mentioned herein are fictitious and are in no way intended
to represent any real individual, company, product, or event, unless otherwise noted. Complying
with all applicable copyright laws is the responsibility of the user. No part of this document may
be reproduced or transmitted in any form or by any means, electronic or mechanical, for any
purpose, without the express written permission of Microsoft Corporation. If, however, your only
means of access is electronic, permission to print one copy is hereby granted.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual
property rights covering subject matter in this document. Except as expressly provided in any
written license agreement from Microsoft, the furnishing of this document does not give you any
Describe the purpose of XML Schemas and DTDs.
!
Manipulate XML by using DOM.
In the lab, students will examine code to see how the XML DOM can be used
to create XML documents. They will then use the DOM to read an XML
purchase order that was generated by the Purchase Order Online application
used in the labs for this course.
Materials and Preparation
This section provides you with the required materials and preparation tasks that
are needed to teach this module.
Required Materials
To teach this module, you need the following materials:
!
Microsoft PowerPoint
®
file 1907A_09.ppt
!
Module 9: Using XML to Exchange Data
!
Lab 9: Exchanging Data Using XML
Preparation Tasks
To prepare for this module, you should:
!
Read all of the materials for this module.
!
Complete the lab.
!
Read the instructor notes and the margin notes for this module.
Part C: Walk Through an XML Document
!
Part D: Search an XML Document
!
Prepare for the demonstration
1. Use Windows Explorer to navigate to the <install
folder>\Democode\Mod09\XML folder.
2. Right-click the Books folder and choose Properties
3. On the Web Sharing tab, click the Share this folder option button.
4. Leave the default alias (Books) and ensure that all Access Permissions
check boxes are checked. Then click OK.
5. Click Yes to accept the warning message and then click OK on the
Properties dialog box.
6. Open the project XMLDemo.vbp located in <install
folder>\Democode\Mod09\XML.
7. Display the cmdCreateXMLDoc_Click procedure and place a breakpoint
on the line Set xmlDoc = New MSXML.DOMDocument.
8. Display the cmdLoadXMLDocument_Click procedure and place a
breakpoint on the line Set xmlDoc = New MSXML.DOMDocument.
9. Display the cmdWalkXMLDocument_Click procedure and place a
breakpoint on the line Debug.Assert Not (xmlDoc Is Nothing).
10. Display the cmdSearchXMLDocument_Click procedure and place a
breakpoint on the line Debug.Assert Not (xmlDoc Is Nothing).
Module 9: Using XML to Exchange Data v
!
ownerDocument property of IXMLDOMNode. This code returns the
root of the document containing the node.
• Attributes are associated with elements by using the setAttribute
method of the element’s IXMLDOMElement interface.
• Each element is appended to the supplied parent node (which in this case
is the booklist element) by using the appendChild method.
9. Having returned to the cmdCreateXMLDoc_Click procedure, step over the
remaining calls to AddBook.
vi Module 9: Using XML to Exchange Data
10. Step into the SaveXMLDocument private subroutine. Step through this
routine, making the following observations:
• ADO Record and Stream objects are used to create the output XML
document. Using these objects allows the document to be output to a
Web folder by using a URL.
• A Stream object is opened from the Record object to represent the
contents of the file.
• The WriteText method of the Stream object is called with a string
representation of the XML document passed as a parameter
(xmlDoc.xml).
11. Press the Continue toolbar button to resume execution of the program.
12. A message box will be displayed confirming that the document has been
successfully created. Press OK to dismiss this message box.
!
Part B: Load and Validate an XML Document
1. Run the project if it is not already running. Click the Load XML Document
button. Execution will halt at the breakpoint within
Module 9: Using XML to Exchange Data vii
5. The hasChildNodes method of IXMLDOMNode is used to test whether
the root element has any child elements.
6. The length property of the child nodes IXMLDOMNodeList interface is
used to ascertain how many direct children elements the booklist element
possesses. This number represents the number of books in the book list.
7. A For loop is established to process each book element.
8. The bookNode IXMLDOMNode variable is set to each successive child
node.
9. The node type is checked by using the nodeTypeString property. In this
case, the node type will always be “element” because only book elements
are direct children of the booklist element.
10. For each element, the ProcessBookElement private subroutine is called,
which outputs the element tag name and text together with the values of the
isbn and type attributes. Make sure the Immediate window is visible as
output is sent to this window. Notice that the text property associated with
the book element is a concatenation of all the text nodes for all child
elements of book.
!
Part D: Search an XML Document
A set of XSL patterns has been provided in the XSL Pattern combo box. You
can repeat these steps for each pattern. The patterns are:
//author
Returns all author elements in the document
//book[@isbn=’1-444444-11-0’]
Returns the book element with the specified isbn attribute value.
to convey additional context, or metadata, or it can define the structure of
the data contained within the tags (for example, by defining the fields of a
purchase order). Although it may initially seem fairly simplistic, the
simplicity and flexibility of XML are two of the key features that have
helped make it the de facto information exchange mechanism for e-
commerce.
Discuss the syntax of XML and how it encompasses other data. Look at
how XML can be applied and the types of data it can help to represent.
Emphasize the suitability of XML for document interchange (e-business)
and explain that document exchange is how XML is used in the lab
scenario. The purchase order system produces XML order documents for
vendor trading partners. Mention other members of the XML family and
which parts of the XML family are implemented in some common
Microsoft products.
There is a practice of using Microsoft Internet Explorer 5 to view an XML
document. The practice initially uses Internet Explorer 5 to display the
XML data in its raw format and then asks students to associate an XSLT
style sheet with the XML document. Internet Explorer 5 is used to view the
document again. This time, as Internet Explorer 5 processes the style sheet,
the data is displayed in an HTML table.
!
Validating XML Documents
Describe the concept of XML validation and that it is frequently useful to
know whether an XML document conforms to a specific XML grammar.
The process of checking that an XML document conforms to a specific
XML grammar is called validation. Applications can accept or reject
documents based on their validity. The two common mechanisms for
defining the XML grammars used when validating XML documents are
DTDs and XML Schemas.
Explain why it is useful to validate XML documents. Show students how
Best Practices
Summarize the best practices that should be observed when using XML to
exchange data.
THIS PAGE INTENTIONALLY LEFT BLANK
Module 9: Using XML to Exchange Data 1 #
##
#
Overview
!
Introduction to XML
!
Validating XML Documents
!
Using the Document Object Model
!
Applying XML in N-Tier Applications
!
Lab 9: Exchanging Data Using XML
!
Best Practices
!
Review
#
##
#
Introduction to XML
!
What is XML?
!
Benefits of XML
!
XML Syntax
!
XML Family of Standards
!
XML Support in Microsoft Products
!
Practice: Viewing an XML Document in Internet Explorer 5 XML defines a generic mechanism for adding tagged information to character
data. This extra information can help to convey additional context, or metadata,
or it can define the structure of the data contained within the tags (for example,
to define the fields of a purchase order). Although it may seem fairly simplistic
at first, the simplicity and flexibility of XML are two of the key features that
have helped to make it the de facto information exchange mechanism for e-
commerce.
In this section, you will learn the syntax of XML and how it encompasses other
data. You will look at how XML can be applied and the types of data it can help
to represent. You will learn about the other members of the XML family and
which parts of the XML family are implemented in some common Microsoft
<author>Anne Ringer</author>
<price>10.95</price>
</book>
<book>
<title>Life Without Fear</title>
<author>Albert Ringer</author>
<price>7.00</price>
</book>
</booklist>XML is a standard for defining data in a tagged form. It allows you to define
extra information beyond the raw data contained in a file or stream. This extra
information takes two primary forms:
!
Imposing a structure on the underlying raw data
!
Providing extra information, or metadata, about part of the underlying raw
data
An XML tag consists of a name enclosed in angle brackets, such as <book>. As
you will see later, tags usually have matching end tags; in this case, </book>.
The pair of tags defines an XML element within which data can be contained. If
the application processing the file or stream containing the tags is XML-aware,
it will identify these tags and interpret them appropriately.
A portion of a simple XML document is shown below:
<booklist>
<book>
<title> Is Anger the Enemy?</title>
<author>Anne Ringer</author>
browser. These formatting instructions do
not convey any information about the data
within them.
Consist of a fixed set of tags defined by
the World Wide Web Consortium (W3C).
If you include your own tags in an HTML
document, the browser will silently ignore
them.
Can be used to define the type and
structure of the data contained within tags.
This function preserves the original
meaning of the data in the document.
Allow you to define your own tags to
create your own grammar or dialect to
describe the data in your document. In
fact, XML has only a few predefined tags
that pertain to document structure.
XML and HTML look similar because they come from a common origin. They
are both derived from the Standard Generalized Markup Language (SGML).
SGML is used to define the structure and metadata for complex documentation,
such as that required to describe the electrical wiring contained in an airliner.
SGML was used before the advent of the Internet. The problem with SGML is
that it is a complex syntax with many options for providing a high level of
flexibility. Unfortunately, this feature can make it difficult to handle. As a
result, SGML-aware applications have tended to reflect this difficulty in their
complexity and price.
HTML was an attempt to apply SGML principles and provide a small subset of
tags for simple documents. The success of the Word Wide Web (WWW) is a
testament to how important simplicity is when creating a common standard.
Standardization of documents
$
XML grammars being standardized for vertical markets
$
Microsoft’s BizTalk initiative
!
Metadata
$
Tools such as Rational Rose can export OO design model in a
format known as XML Metadata Interchange (XMI)
!
Structure and interoperability in infrastructure
$
For example, the Simple Object Access Protocol (SOAP)As it has evolved, XML has found a variety of applications:
!
Data exchange
The main area in which XML is being applied is data exchange. The ability
to exchange structured data between applications is a key enabler for e-
commerce. For many years, the Electronic Data Interchange (EDI) standard
governed most data interchange between organizations. This standard acted
as a barrier to entry for smaller firms because it was traditionally expensive
to implement. The EDI standardization process also limited the speed at
which EDI-based systems could respond to changing conditions. With XML
representing the data and the Web acting as the transport mechanism, the
barrier to entry has lowered considerably. Also, the flexibility of XML has
increased dramatically because two organizations simply need to agree to a
common XML grammar to start the exchange of data.
This discussion should give you a few ideas about how XML is being applied.
For more information about the advantages and uses of XML, see "Proposed
Applications and Industry Initiatives" on the Extensible Markup Language
page of Robin Cover’s XML/SGML Web pages located at
www.oasis-open.org/cover/xml.html.
8 Module 9: Using XML to Exchange Data XML Syntax
!
Comments
!
Elements
$
Must nest correctly
$
Unicode, case sensitive
$
Can contain text, other elements, or both
<!–- This is my favorite book -->
<booklist>
<book>
<title>Is Anger the Enemy?</title>
<author>Anne Ringer</author>
<price>10.95</price>
</book>
</booklist>To read and manipulate XML, you must understand some of the syntax of XML
unicode characters. This rule applies to both XML tags and the data. As a
result, XML does not provide equivalence between uppercase and lowercase
characters (that is, it is case sensitive). The following example is not correct
XML syntax:
<!-- This is invalid XML syntax: -->
<title>Is Anger the Enemy?</TITLE>
The following example shows an XML element containing a mixture of other
elements and text:
<chapter title="Inorganic Chemistry">
In this chapter, we will discuss inorganic chemistry...
<section>
Transition Metals
</section>
Transition Metals are found in the centre of the periodic
table...
<section>
Group 1 Metals
</section>
Group 1 Metals have a single electron...
</chapter>
Note that the indentation is only shown for clarity. There is no need for such
spacing, or indeed new lines, in your XML document.
It is important that XML tags nest correctly. If tag B is contained within tag A,
then there must be an end tag for tag B before the end tag for tag A. The
following example shows invalid nesting:
<!-- This is invalid XML syntax: -->
<book>
<author>
XML Syntax (continued)
!
Attributes
!
Document Structure
<book ISBN=“1-444444-11-0”>
<title>Is Anger the Enemy?</title>
<author>Anne Ringer</author>
<price>10.95</price>
</book>
<?xml encoding=“UTF-8” version=“1.0”?>
<!DOCTYPE booklist SYSTEM “booklist.dtd”>
<booklist>
<book>
<title>Is Anger the Enemy?</title>
<author>Anne Ringer</author>
<price>10.95</price>
</book>
</booklist>
XML Declaration
Prolog
Root ElementAttributes
XML elements can have one or more attributes applied to them. An attribute
consists of a single name/value pair. The following example below shows how
an attribute containing an ISBN number could be applied to the book element:
DTD defines the expected structure of the document. For more information
about DTDs, see the Validating XML Documents section later in this
module.
!
Root element
There should be a single XML element that encloses all of the other XML
elements and data in the document. This element is called the root element.
An XML document that conforms to this structure and obeys the rules for
attributes, elements, and comments defined previously is called a well-formed
XML document. The following example shows a well-formed XML document:
<?xml encoding="UTF-8" version="1.0"?>
<!DOCTYPE booklist SYSTEM "booklist.dtd">
<booklist>
<book>
<title>Is Anger the Enemy?</title>
<author>Anne Ringer</author>
<price>10.95</price>
</book>
<book>
<title>Life Without Fear</title>
<author>Albert Ringer</author>
<price>7.00</price>
</book>
</booklist>
For more information about the structure of XML documents, refer to the XML
specification on the W3C Web site at www.w3.org/TR/REC-xml.
Module 9: Using XML to Exchange Data 13
As is the nature of standards, the XML family is constantly evolving to take
into account new uses and challenges that present themselves as the XML
family of standards is applied. Some of the main members of the XML family
of standards are described in the following discussion.
Schema
The term schema is somewhat overloaded in the XML environment. In generic
terms, a schema describes some form of plan or structure. In computer terms,
the term schema is commonly used to describe the structure of the tables and
columns in databases. Used in its generic form, an XML schema would define
what can and cannot be in a particular XML document. It would describe which
elements could contain which other elements, what attributes each element can
have, and so forth.
The XML specification already contains a form of schema called a DTD. The
DTD can be used to define an XML grammar to which a document must
conform. The grammar could define a purchase order, the structure of a book, a
financial transaction, or the format of an RPC packet. An XML-aware tool can
then use the DTD to ensure that a document conforms to the given grammar.
This process is termed validating the document. A document that has been
proven to conform to its associated grammar is called a valid XML document.
14 Module 9: Using XML to Exchange Data
Unfortunately, the DTD syntax is somewhat limited because compatibility with
SGML is required. At the time of writing, the W3C is in the process of defining
a replacement for DTDs called XML Schema. The XML Schema standard is
based on work by Microsoft and other W3C members and will replace DTDs
over time.
For more information about DTDs and XML Schema, see Validating XML
Documents in this module.
implementation of the DOM to help find particular nodes in the DOM tree.
For more information about XPath, XSLT, and XSL, you can go to the W3C
Web site at www.w3.org/Style/XSL.
Module 9: Using XML to Exchange Data 15
Namespaces
People and organizations are free to define their own XML grammar. A
potential problem, however, is that the names of the elements and attributes in
these grammars may conflict. For example, consider an XML grammar that
defines the structure of a book. Each chapter in the book would have a title.
Imagine that the book described family trees. Each tree may be defined
according to another XML grammar specifically for family tree description. In
this family tree grammar, each member of a family may also have a title. The
title for a family member may have limitations placed on it (for example,
limited to “Mr..” “Ms.,” “Mrs.,” “Dr.,” and so on), whereas there would be no
such restrictions on the titles of the chapters.
In this example, we need a way to differentiate between the titles used in
different XML grammars. Using namespaces solves this problem. Namespaces
are prefixes that can be used to establish that a particular element or attribute
belongs to a specific XML grammar. An example of using namespaces is
shown below:
<book xmlns:bookns='urn:com:booknamespace'
xmlns:familyns=’urn:com:familyns’>
<chapter>
<bookns:title>My Family</bookns:title>
The original Bloggs family can be traced back
to <familyns:title>Dr.</familyns:title> Jack Bloggs
at the turn of the 1900's...