Contents
Overview 1
Transforming XML with XSL 2
Using Templates 9
Filtering and Sorting XML 22
XSL Language Constructs 33
Creating New Nodes Dynamically 39
Lab 4: Displaying an XML Document Using
XSL 44
Review 52
Module 4: Displaying an
XML Document Using
XSL 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
Test Lead: Eric Myers
Manufacturing Manager: John Williams
Group Product Manager: Steve Elston Module 4: Displaying an XML Document Using XSL iii Instructor Notes
This module describes how to write an XSL style sheet and apply it to a static
XML document. The focus of the module is to describe the syntax for XSL
template rules, and to introduce the various filter and path specifications that
allow you to match particular elements and attributes in an XML document.
Many students find style sheets confusing, partly because there is so much
syntax to learn, but also because the concept of rule-based programming can be
quite alien. The key points to emphasize during the module are that an XSL
document is itself an XML document (with familiar < and > tags), and that a
template rule always has a current context (for example, the element or attribute
that is currently being matched).
The module primarily shows how to transform XML into HTML for display in
a browser. Toward the end of the module, we briefly discuss how to create new
XML elements/attributes/processing instructions and so on, so that one
grammar of XML can be transformed into a different grammar. Point out that
this is useful in a business-to-business e-commerce situation.
After completing this module, students will be able to:
!
Describe how XSL transforms an XML tree into a different XML result
tree.
!
Write an XSL style sheet with template rules and <xsl:value-of> actions.
Preparation Tasks
To prepare for this module, you should:
!
Read all of the materials for this module.
!
Complete the labs.
Due to the length of the answers to the labs for this course, we were unable
to include them in the Delivery Guide. Please see Appendix A or the
Student CD for the lab answers.
!
Familiarize yourself with the XSL Debugger tool, which is illustrated in the
first Demonstration.
Module 4: Displaying an XML Document Using XSL v Module Strategy
Use the following strategies to present this module:
!
Transforming XML with XSL
Remind students that we have already seen the effects of XSL style sheets in
Module 2, “Overview of XML Technologies.” Tell students that they will
now see how to create XSL style sheets, rather than only being able to use
them.
To understand how style sheets work, it is imperative that students be aware
of how an XML document maps to a tree of nodes. This mental picture is
fundamental to the students’ understanding of the XSL template-matching
rules.
Also, point out that the syntax shown in this module is the XSL syntax
because filters resemble conventional If statements found in other
programming languages.
!
XSL Language Constructs
This is a short section and can be covered fairly quickly. Students will
immediately recognize these constructs from their experience with other
programming languages. Feel free to make conceptual comparisons with If
statements, For loops, and Case statements found in other languages.
vi Module 4: Displaying an XML Document Using XSL !
Creating New Nodes Dynamically
Before describing how these XSL constructs work, it is important to
describe why you might use them — for example, to emphasize the use of
XSL to transform one XML grammar into another. This implies the need to
generate XML elements, attributes, and so on; hence the need for these
constructs.
The example in the demonstration shows how to add an HREF attribute to
an <A> element to generate a hyperlink. This technique is used in the lab to
define a hyperlink to an ASP where book details are generated and returned
to the browser.
!
Lab 4: Displaying an XML Document Using XSL
In this lab, students write an XSL style sheet from scratch. The style sheet
transforms the XML book details (returned from Query.asp) into an HTML
table.
Students find the lab quite achievable because most of the tasks have been
covered in a similar way in the module.
The Extensible Stylesheet Language (XSL) enables this kind of data conversion
in XML.
After completing this module, you will be able to:
!
Describe how XSL transforms an XML tree into a different XML result
tree.
!
Write an XSL style sheet with template rules and <xsl:value-of> actions.
!
Use <xsl:apply templates> to apply additional templates.
!
Use the path and filter capabilities of XSL for pattern matching.
!
Achieve complex styling with XSL language constructs.
!
Generate new nodes in the XML result tree.
Slide Objective
To provide an overview of
the module topics and
objectives.
Lead-in
In this module, you will learn
about displaying XML data
in a Web browser by using
the Extensible Stylesheet
Language, or XSL.
2 Module 4: Displaying an XML Document Using XSL
To introduce the general
concept of transforming an
XML tree into a different
format.
Lead-in
For a Web browser to
display XML in a user-
friendly way, the XML must
be converted into HTML.
XSL style sheets provide a
way to achieve this type of
transformation.
Module 4: Displaying an XML Document Using XSL 3 Transforming XML Trees
!
Using XSL and XSL processor for transforming XML
!
Attaching an XSL style sheet to an XML document
XML document
(different format)
XSL processor
XSL processor
HTML document
(XML-conformant)
XML
document
XSL style
sheet
The XSL processor translates the XML document into a different form of
XML. The output generated from an XSL style sheet can be any document,
such as an XML or EDI document. When transforming an XML document into
HTML, bear in mind that the HTML must be XML-compliant. For example,
each start tag must have a corresponding end tag, and case sensitivity rules.
Slide Objective
To describe the process of
transforming an XML
document from one format
to another.
Lead-in
Transforming an XML
document into another
format requires an XSL
processor. The XSL
processor applies the given
style sheet to the XML
document and creates a
new XML tree as a result.
4 Module 4: Displaying an XML Document Using XSL If more than one style sheet is attached to a document, only the first one is used.
All subsequent style sheets are ignored.
Using XSL to transform XML into different XML
XSL also has the potential to help build sophisticated e-commerce solutions. To
understand the role of XSL in e-commerce, consider the following example.
Companies A and B constantly exchange data. Both of these companies
produce widgets, and need to describe their customer orders. Each widget has a
part number, and therefore each order consists of a series of part numbers and
the Web site />.
For the purposes of this course, we will concentrate on XSLT as implemented
in the Microsoft XML COM component. At the time of this writing, few
applications implement the formatting objects as specified by the W3C.
Subsequent references to XSL will implicitly refer to features provided by
XSLT.
Note
Module 4: Displaying an XML Document Using XSL 5 Mapping XML Trees
XML document
<employees>
<employee ID="123">
<name>Jake</name>
<salary>23000</salary>
</employee>
...
</employees>
Document root
<employees>
<employee>
ID <name> <salary>
"Jake" "23000"
…
"123"
Tree structure
memory to represent the
various nodes in the
document.
6 Module 4: Displaying an XML Document Using XSL Mechanics of XSL Transformations
!
An XSL document is a well-formed XML document
$
XSL document has a single <xsl:stylesheet> element
$
Template rules are defined as <xsl:template> elements
$
Output templates are the contents of the <xsl:template>
elements and define the transformation required
<xsl:stylesheet
xmlns:xsl=" />template rule 1
output template
template rule 2
output template
</xsl:stylesheet>
<xsl:stylesheet
xmlns:xsl=" />template rule 1
output template
template rule 2
output template
</xsl:stylesheet>
defines a set of instructions
for the XSL processor.
Using these, you can
specify a pattern to match
and a template to output
when the pattern is found in
the XML document.
Delivery Tip
Remind students about
namespaces and how they
are represented.
Also tell them that
namespaces are case-
sensitive.
Delivery Tip
The xmlns:xsl namespace is
a Microsoft requirement; it is
not part of the standard.
Module 4: Displaying an XML Document Using XSL 7 Demonstration: Using the XSL Debugger In this demonstration, you will see how to use the XSL Debugger tool to view
the output of an XSL style sheet. This tool can help diagnose problems in an
XSL style sheet. You will also see how to install an XSL viewer that integrates
with Internet Explorer 5.0 and provides immediate feedback on the output of an
attached XSL style sheet.
XSL Debugger
as an HTML table, as a
result of the style sheet.
4. In Internet Explorer 5.0,
open xsl-debugger.htm.
Make sure that
employees.xsl is displayed
in the left pane and
employees.xml is displayed
in the right pane.
5. Make sure that the Show
Source check box is
selected.
6. Click Run. A pop-up
menu is displayed. Click
Step to step through the
style sheet. The results of
the XSL transformation are
displayed at the bottom of
the page.
8 Module 4: Displaying an XML Document Using XSL XSL Output Viewer
The XSL viewer utility is available on the Student CD-ROM in the
\Sampapps\xml diagnostics\XSL Output Viewer folder.
!
To install the XSL Viewer utility
• On the Student CD-ROM, right-click the file msxmlvw.inf in the folder
\Sampapps\xml diagnostics\XSL Output Viewer, and then click Install.
Format of an Output Template
!
Outputting Nodes
!
Demonstration: Outputting Values of Child Elements
!
Invoking Additional Template Rules
!
Practice: Recursively Applying TemplatesThere are two key stages of using XSL as a transformation mechanism for
XML documents. The first is to match the appropriate elements in the input
tree. The second is to generate the correct output tree. In this module, you will
learn how to match XML elements and how to generate an output tree.
Slide Objective
To introduce the module
topics.
Lead-in
Now let’s look at how to
create an XSL template to
format an XML document.
10 Module 4: Displaying an XML Document Using XSL Matching the Root Element
!
Use <xsl:template match> to match different parts of
the XML document
!
transformed by the output template.
Slide Objective
To examine the matching of
elements in the source XML
document.
Lead-in
One key question that arises
when transforming an XML
document into another
format is what elements and
attributes should be
transformed.
Delivery Tip
Stress the point that you
must begin an XSL style
sheet by matching the root
element, and that only
nodes that match a template
rule in the XSL style sheet
will be transformed. Any
node in the source XML
document that is not
matched by a template rule
in the style sheet will be
ignored.
Module 4: Displaying an XML Document Using XSL 11 Matching XML Nodes
!
For example, the following template matches all source elements that are not
matched by a more specific template:
<xsl:template match="*">
output template
</xsl:template>
Slide Objective
To discuss the use of XSL
pattern matching to match
other nodes in the XML
document.
Lead-in
There are different ways to
match nodes in the source
XML document to template
rules in the XSL style sheet.
Note
12 Module 4: Displaying an XML Document Using XSL Matching a specific element
If you wish to process a specific element, include a template that contains the
name of the element in the pattern. For example, the following template
matches <Employee> elements that are children of the current node:
<xsl:template match="Employee">
output template
</xsl:template>
Matching a specific element by path
It is also possible to match elements according to their absolute or relative
employees, which are not otherwise matched, and “Employee/Name” overrides
the more general match for the employees.
Module 4: Displaying an XML Document Using XSL 13 Format of an Output Template
!
Output templates define how elements and attributes
in the XML document will be transformed
!
For example:
$
Translate an XML document into HTML format
<xsl:template match=“Employee/Name">
<P>
<HR/>
output employee’s name
<HR/>
</P>
</xsl:template>
<xsl:template match=“Employee/Name">
<P>
<HR/>
output employee’s name
<HR/>
</P>
</xsl:template>
the <Name> element into an HTML paragraph containing two horizontal lines,
one on either side of the employee’s name.
Slide Objective
To examine the form and
style of output templates.
Lead-in
Once you have identified
which element you wish to
transform, you must dictate
the type of transformation to
be done on the element.
Note
14 Module 4: Displaying an XML Document Using XSL Outputting Nodes
!
<xsl:value-of> outputs the text content of an element
and all its descendents
!
<xsl:value-of select = ... > outputs the text content of
selected descendents
<xsl:template match="/">
<xsl:value-of />
</xsl:template>
<xsl:template match="/">
<xsl:value-of />
</xsl:template>
<xsl:template match="/">
<xsl:value-of select = "Name" />
XML document in the output
XML document. The
<xsl:value-of> XSL element
provides this functionality.
Module 4: Displaying an XML Document Using XSL 15 Outputting a descendent of the matched node
The alternative form of the <xsl:value-of> element allows you to supply a
selection criterion. In this case, only the value of the selected node is output.
The selection criterion can be any normal XSL pattern.
For example, the following matches all the direct descendents named “Name”:
<xsl:value-of select = "Name" />
And this matches all the descendents (at any level) named “Employee”:
<xsl:value-of select = "//Employee" />
Be careful when using this form of the <xsl:value-of> element. Only the first
node matching the selection criterion will be output.
Delivery Tip
Be prepared to answer
questions that students may
have about using this form
of the <xsl:value-of>
element.
16 Module 4: Displaying an XML Document Using XSL Demonstration: Outputting Values of Child Elements
the GetAllText.xsl style
sheet.
3. In Internet Explorer 5,
open employees.xml. Note
that only elements are
output.
4. Edit GetAllText.xsl to use
the output rule <xsl:value-of
select=”//employee” />.
Refresh employees.xml in
the browser. Only the data
for the first employee is
displayed.
5. Edit GetAllText.xsl to use
the output rule <xsl:value-of
select=”//name” />. Refresh
employees.xml in the
browser. Only the name of
the first employee is
displayed.
Module 4: Displaying an XML Document Using XSL 17 Invoking Additional Template Rules
!
<xsl:apply-templates> invokes template rules for
all descendents
!
<xsl:apply-templates select="pattern"> invokes
template rules for selected descendents
them.
!
The <xsl:apply-templates select = “pattern”> element applies template rules
only to selected descendents. As the XSL specification has evolved, certain parts have been subject to
different interpretations. The effect of the <xsl:apply-templates> element is one
such part. The issue here is whether templates should be applied by default
when processing a node, or only if specifically instructed to by use of an
<xsl:apply-templates> element. This means that for some processors, for
example the Lotus LotusXSL, a single <xsl:apply-templates> element in the
root node output template will result in templates being applied recursively to
all nodes in the source tree, rather than just those that are direct descendents of
the root node. This portability issue should be resolved over time as the related
specifications are finalized.
Slide Objective
To discuss the need to
apply templates in order to
propagate rule matching.
Lead-in
To apply other template
rules, you must explicitly tell
the XSL processor which
nodes you wish to apply the
templates to.
Delivery Tip
You must always have a
template for the root
</xsl:template>
<xsl:template match="Name">
<SPAN style="font-size:36pt">
<xsl:value-of />
</SPAN>
</xsl:template>
<xsl:template match="Salary">
<I><xsl:value-of /></I>
</xsl:template>
</xsl:stylesheet>
The style sheet defines four different templates. The first template matches the
root element of the XML document and applies any template rules that match
the "//Employee" pattern.
<xsl:apply-templates select="//Employee" />
The XSL processor scans the XML document for <Employee> elements at any
depth beneath the current element. The // operator is the recursive path operator,
and ensures that the XSL processor finds <Employee> elements at any depth
and not just direct children of the current element.
Once an <Employee> element is found in the XML document, the XSL
processor applies the template rule for <Employee>. This is the second template
rule in the style sheet. The output template for this template rule is as follows:
<P>
<xsl:apply-templates select="Name" />
<xsl:apply-templates select="Salary" />
</P>