Tải bản đầy đủ (.pdf) (36 trang)

Tài liệu Module 7: Formatting and Transforming: XSL and XSLT pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (974.53 KB, 36 trang )










Contents
Overview 1
Lesson: Defining XSL 2
Lesson: Transforming XML Using XSLT 8
Lesson: XSLT in the Microsoft .NET
Framework 20
Lab 7: Creating and Editing XSLT in
FrontPage 2002 and Visual Studio .NET 25
Review 31

Module 7: Formatting
and Transforming: XSL
and XSLT



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
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
license to these patents, trademarks, copyrights, or other intellectual property.

 2001 Microsoft Corporation. All rights reserved.

Microsoft, MS-DOS, Windows, Windows NT, ActiveX, BackOffice, bCentral, BizTalk,
FrontPage, MSDN, MSN, Netshow, PowerPoint, SharePoint, Visio, Visual Basic, Visual C++,
Visual C#, Visual InterDev, Visual Studio, Windows Media, and Xbox are either registered
trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries.

The names of actual companies and products mentioned herein may be the trademarks of their
respective owners.


Module 7: Formatting and Transforming: XSL and XSLT iii


Instructor Notes
This module introduces Extensible Stylesheet Language Transformations
(XSLT) and the role it plays in the Microsoft
®
.NET Framework. XSLT is a
subset language of Extensible Stylesheet Language (XSL).
XSLT is a difficult topic with many subtle features and capabilities. There is

not time to explore most of these features and capabilities in this module. The
aim of this module is to provide an introduction to XSLT without completely
overwhelming the participants. Emphasize at the outset that XSLT is the subject
of an entire five-day course, Course 1913A, Exchanging and Transforming
Data Using XML and XSLT.
This is not an easy topic to do well. Teaching this module without preparation
will make participants confused about XSL and XSLT. Prepare thoroughly for
this topic.
After completing this module, participants will be able to:
!
Explain the relationship between XSL and XSLT.
!
Compare XSLT with Extensible Stylesheet Language Formatting Objects
(XSL-FO).
!
Explain the syntax of a basic XSLT style sheet.
!
Write a basic XSLT style sheet that transforms an XML document.

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_07.ppt

To prepare for this module:
!
Read all of the materials for this module.

!
Complete the lab.
!
Attend Course 1913A or read it and complete the labs.
!
On the World Wide Web Consortium (W3C) site, read about XSL, XSLT,
XSL-FO, and XML Path Language (XPath) in as much depth as possible.
Make sure that you understand the distinctions and relationships between
these technologies and languages.
!
Read the topics about XSLT in the Microsoft .NET Framework software
development kit (SDK) in Microsoft Visual Studio
®
.NET.

Presentation:
45 Minutes

Lab:
20 Minutes
Required Materials
Preparation Tasks
iv Module 7: Formatting and Transforming: XSL and XSLT


Module Strategy
Use the following strategy to present this module:
!
Defining XSL
This lesson provides an introduction to XSL and XSLT. Explain the

relationship between XSL and its subset languages: XPath, XSLT and
XSL-FO. Explain why XSLT was developed. You will present an overview
of XSL and present a simple example of a style sheet based on the working
draft of XSL that applies formatting to XML.
!
Transforming XML Using XSLT
In this lesson, start by discussing the power of XSLT and the many ways
that you can use it. You may want to bring extra examples to this section,
but be mindful of the timing. You will present an overview of XSLT and
present a simple example of a style sheet that transforms XML.
!
XSLT in the Microsoft .NET Framework
The goal of this lesson is to describe the nature of XSLT support in the
.NET Framework and the particular role of the XslTransform class in the
“System.Xml.Xsl” namespace. Read as thoroughly as you can about this
topic. Search for the word “xlst” in the .NET Framework SDK.
Familiarize yourself with the ways in which XslTransform works with
other parts of the .NET Framework and its classes so that you can explain
the diagram in this lesson adequately.
Ensure that you teach this topic as an overview, because the details about
many of the methods, classes, and components might overwhelm
participants. Stress that there is significant documentation on this topic and
that there is extensive and diverse support in the .NET Framework for
XSLT. If you have time, you may wish to explain the concepts of the
XmlReader and XmlWriter classes further, but be sure not to go into too
much detail.
Finally, point your participants to other courses that cover this topic in
more detail.

Module 7: Formatting and Transforming: XSL and XSLT 1



Overview
!
Defining XSL
!
Transforming XML Using XSLT
!
XSLT in the Microsoft .NET Framework

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
This module introduces Extensible Stylesheet Language (XSL) and one of its
subset languages, Extensible Stylesheet Language Transformations (XSLT).
XSLT plays an important role in the handling of Extensible Markup Language
(XML) sources.
After completing this module, you will be able to:
!
Explain the relationship between XSL and XSLT.
!
Compare XSLT with Extensible Stylesheet Language Formatting Objects
(XSL-FO).
!
Explain the syntax of a basic XSLT style sheet.
!
Write a basic XSLT style sheet that transforms an XML document.



To monitor the development of W3C activity regarding XSL and XSLT,
go to the XSL page at the XSLT page at
and the XSL working group page at

For more information about XSLT, see Course 1913A, Exchanging and
Transforming Data Using XML and XSLT.
You can apply the principles that you learn in Course 1913A to
Microsoft
®
.NET languages (Microsoft Visual Basic
®
.NET and Microsoft
Visual C#

.NET), but the specific syntax and methods used with the XSLT
syntax is different.

Introduction
Objectives
Note
2 Module 7: Formatting and Transforming: XSL and XSLT


Lesson: Defining XSL
!
What Is XSL?
!
XSL Style Sheets


*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
This lesson focuses on XSL and XSLT. Understanding the difference between
them will help you to use them appropriately in your XML projects.
The actual processing of XML and XSLT depends on the particular capabilities
of the browser and processor in use. Not all browsers and processors are equally
capable of fully supporting the World Wide Web Consortium (W3C)
requirements for XSL.

To ensure that your browser remains as fully synchronized with the
W3C as possible, ensure that it is kept up-to-date with the latest upgrades and
enhancements from Windows Update. Click Start, click All programs, and
then click Windows Update to check the latest updates for your system.

After completing this lesson, you will be able to:
!
Name the component languages of XSL and state the purpose of each.
!
Distinguish between a style sheet based on the XSL working draft
namespace and a style sheet based on the XSLT Recommendation.

Introduction
Im
portant
Lesson objectives
Module 7: Formatting and Transforming: XSL and XSLT 3



What Is XSL?
!
Extensible Stylesheet Language (XSL) never achieved W3C
Recommendation status
!
XSL branched:
"
XSLT is the style sheet language
"
XSL-FO is the formatting language
"
XPath is the language for specifying locations
XPath
XSL W3C Recommendations
XSL-FO
Formatting Objects
XSL-FO
Formatting Objects
XSLT
XSL Transformations

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
XSL is an W3C initiative to develop a programming language for creating style
sheets. Here are some situations where you can use style sheets:
!

Suppose you have an XML source that you need to display as a table in
Hypertext Markup Language (HTML). You can use a style sheet to perform
this transformation.
!
Suppose you have an XML source that uses attributes to mark price
information. You are asked to combine this source with data that uses an
XML vocabulary that is composed only of elements. You can use a style
sheet to transform the source XML by converting all attributes to elements.
!
One particularly important application of style sheets is to provide different
output from the same data for display on different devices like mobile
phones, TVs, handheld personal computers (H/PCs), and so on.

A style sheet is a programming tool that you use to transform or format XML.
A style sheet contains a set of rules that governs how transformations or
formatting takes place. These rules are called templates. XML style sheets are
expressed in well-formed XML.
A style sheet must use either the XSL working draft namespace or the XSLT
namespace. Using the XSLT namespace gives you all the functionality of an
XSL namespace and provides some functionality in the newer XSLT
namespace. All style sheets use XPath statements to specify locations.
The input to a style sheet is an XML source. Style sheets produce a memory-
resident result set of the formatting or transformation. Most often, the style
sheet also contains instructions to create a stream of output, which often creates
a new document. The new document may be formatted as XML, HTML,
XHTML, text, or some other format.
Introduction
What is a style sheet?
4 Module 7: Formatting and Transforming: XSL and XSLT



As work progressed on XSL, it became clear that the job or transforming XML
would require more than a single language. All further work on transforming
the XML structure became XSL Transformations (XSLT). Concurrent with the
work on XSLT was work on XPath. XPath is now hardly identified outside of
W3C documentation as being part of XSL because it has become so integral to
other XML technologies. But it was developed in the context of the W3C XSL
initiative. XSL-FO has achieved Recommendation status as of October 2001.
Here is a summary of the three languages that have come out of the W3C XSL
initiative.
XSL Subset
Language

Purpose

File extensions associated with use

XSLT Transform XML .xsl, .xml
XPath Specify paths and
locations in XML
XPath statements do not exist in stand-
alone files. XPath statements are
embedded within statements belonging to
other programming languages.
XSL-FO Express formatting
semantics
.xsl, .xml

The original vision of XSL was to divide the task of language development into
one part that transforms XML and another that formats XML. XSLT is now the

transformation language, and XSL-FO is the formatting language.
XSLT however, is used for both transforming and for formatting XML. XSLT
transforms XML by rearranging elements, sorting and searching, and
performing conditional processing. It formats XML for presentation in a
browser or other applications by transforming XML into HTML, Extensible
HTML (XHTML), or text. These kinds of formatting are adequate for many
jobs but when greater control over formatting must be exercised than that
allowed by HTML, XSLT must yield to XSL-FO.
The purpose of XSL-FO is to precisely render images, paragraph blocks, text
lines, and many other graphical objects. XSL-FO also provides formatting
functions such as padding, font size, color, areas, and borders.
XSL subset languages
Formatting and
transformin
g XML
What is XSL-FO?
Module 7: Formatting and Transforming: XSL and XSLT 5


XSL Style Sheets
!
You may encounter examples of XSL working draft
namespaces in style sheets
!
XSL namespace is still supported by the Microsoft
XML Parser (MSXML) 3.0
"
MSXML 3.0 is installed by Windows XP and
Internet Explorer 6.0
!

XSL is no longer supported by XML Core Services
!
Current development should use XSLT
<xsl:stylesheet xmlns:xsl=" /><xsl:stylesheet xmlns:xsl=" /><xsl:stylesheet xmlns:xsl=" /><xsl:stylesheet xmlns:xsl=" />Embedded XML Fra
gment
XSL Stylesheet

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
You may encounter style sheets that use the obsolete XSL working draft
Uniform Resource Identifier (URI) in the namespace declaration. This
namespace sends a signal to the XML processor to process all elements that
are prefixed by xsl: by using special functionality of the processor that
transforms XML.
The Microsoft XML Parser (MSXML) version 3.0 still recognizes the XSL
namespace as a valid way to signify a style sheet. MSXML 3.0 is installed by
default by the Microsoft Windows
®
XP operating system and by Microsoft
Internet Explorer version 6.0. Therefore, for most projects, you can still use the
XSL URI in a style sheet namespace declaration.
However, the latest version of the Microsoft XML processor, Microsoft XML
Core Services (MSXML) version 4.0, does not recognize the XSL URI as a
valid way to signify a style sheet. Style sheets generated for new development
projects should use the XSLT URI. XSLT has reached W3C Recommendation
status. XSLT provides all of the functionality of XSL and more.
XSL style sheet

namespace
MSXML processor
support for XSL and
XSLT
6 Module 7: Formatting and Transforming: XSL and XSLT


In this example, an XML source contains information about pets:
<pets>
<pet type="cat" age=".8" gender="F" ageyears=""
agemonths="0.8">
<breed>Moggie</breed>
<color>black</color>
<name>Alberta</name>
<weight />
</pet>
<pet type="bird" age="408" gender="M" ageyears="34"
agemonths="">
<breed>Sulpher Crested Cockatoo</breed>
<color>white</color>
<name>Max</name>
<weight>7</weight>
</pet>

The following style sheet uses the XSL working draft URI in the namespace:
<xsl:stylesheet xmlns:xsl="
<xsl:template match="/">
<pets>
<xsl:apply-templates select="//pet" order-by="name"/>
</pets>

</xsl:template>

<xsl:template match="pet">
<pet>
<name><xsl:apply-templates select="name"/></name>
<breed><xsl:apply-templates select="breed"/></breed>
<weight><xsl:apply-templates
select="weight"/></weight>
</pet>
</xsl:template>

<xsl:template match="name">
<xsl:value-of />
</xsl:template>

<xsl:template match="breed">
<xsl:value-of />
</xsl:template>

<xsl:template match="weight">
<xsl:value-of />
</xsl:template>
</xsl:stylesheet>

Example
Module 7: Formatting and Transforming: XSL and XSLT 7


Demonstration: Formatting XML Using a Style Sheet
!

In this demonstration you will see:
"
A style sheet that formats XML in a table
whose columns can be sorted
"
A style sheet that formats XML as an
HTML table

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
In the following demonstration, you will see an XSL style sheet that formats
XML in a table whose columns can be sorted and an XSL style sheet that
formats XML as an HTML table.
!
Review the code
1. Using Notepad, open petsorting.htm and petsort.xsl in the
install_folder\Democode\Mod07 folder.
2. Review the key features of each file.
3. In Internet Explorer, open petsorting.htm, and then sort the data by clicking
on each column heading in the table.
4. Switch to the Notepad source, and then note the location of the embedded
XML data as a data island on the page.
5. Using Notepad, open pets5xsl.xml and petsort2.xsl.
6. Review the key features of each file.
7. In Internet Explorer, open pets5xsl.xml.
8. Close all open windows.


Introduction
8 Module 7: Formatting and Transforming: XSL and XSLT


Lesson: Transforming XML Using XSLT
!
What Is XSLT?
!
Parts of an XSLT Style Sheet
!
What Can You Do with XSLT?

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
XSLT is the transformation language that is a core part of XSL. It provides the
mechanisms for transforming one XML structure into another.
An XSLT style sheet may itself be transformed by using another XSLT style
sheet. This is because XSLT style sheets are written in well-formed XML. The
fact that XSLT can also be modified makes it highly adaptable.
XSLT is also designed by the W3C to be used independently of XSL.
After completing this lesson, you will be able to describe:
!
The types of transformations that can be done with XSLT.
!
Ways in which you can use XSLT in a project that uses XML data.
!
The parts of an XSLT style sheet.


Introduction
Lesson objectives
Module 7: Formatting and Transforming: XSL and XSLT 9


What Is XSLT?
!
XSLT uses templates to transform XML
!
Templates define transformations
!
XSLT uses XPath to fix locations and for conditional processing
!
XSLT is a declarative language
!
Transformations are applied recursively and independently of the
sequence in which they appear in the style sheet
XML Source
XSLT
Result Set
XSLT
Formatted Output
XML
HTML
Text…
transform transform

*****************************
ILLEGAL FOR NON

-
TRAINER USE
******************************
W3C created XSLT to provide a transformation language that enables you to
manipulate XML documents. To manipulate XML documents, you use an
XPath expression to retrieve data from the node tree and then apply templates
and match metadata and data for specific transformations.
XSLT is the language that allows you to transform XML trees. Transformations
can involve creating new elements or attributes, doing calculations, filtering,
sorting, changing the structure of the node tree to a new form for an output
XML document, or any combination of these. XSLT version 1.0 is the basis
upon which W3C is creating even more sophisticated transformation
capabilities.
XPath expressions provide XSLT with the semantics for identifying and
manipulating nodes in an XML tree. XSLT statements, called templates, match
certain patterns of elements or attributes. Templates contain XPath location
paths. XSLT uses XPath to match patterns of XML data and metadata in the
XML source.
Although the emphasis on XSLT is often on its transformation capabilities,
when it is used in conjunction with XPath expressions, it is extremely powerful
for selectively finding information wherever it appears in an XML document.
By using templates and various matching and selection methods, XSLT can
efficiently locate and select information from a very mixed XML document,
retrieve the data and the metadata, and produce different output documents.
An example of this is a compound business document from one government
agency that must be split into two or more simple documents for use by another
government agency.
Another example is an XML scientific data document that contains both
biological and chemical information from a research lab. When received by a
genetic engineering organization, the document must be split into a separate

document for the biological data and another for the chemical data. You can use
XSLT to accomplish these types of tasks.
Introduction
Transformations
XSLT and XPath
10 Module 7: Formatting and Transforming: XSL and XSLT


XSLT is a declarative language. Declarative languages describe results and let
the processor determine the best way to achieve it. This means that an XSLT
style sheet is a description of the result that you want to achieve. Compare
XSLT to Visual Basic Scripting Edition (VBScript), which is an imperative
programming language. In VBScript, you must specify exactly the method in
which a result is generated.
One implication of using a declarative language is that you do not need to be
concerned about the sequence of the XSLT statements as they are written in the
XSLT style sheet from top to bottom. This sequence is not necessarily the same
sequence that the XSLT processor uses to actually perform transformations.
XSLT is a declarative
language

×