1. Trang chủ
  2. » Công Nghệ Thông Tin

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

36 635 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Module 7: Formatting and Transforming: XSL and XSLT
Trường học Microsoft Corporation
Chuyên ngành Information Technology
Thể loại module
Năm xuất bản 2001
Thành phố Redmond
Định dạng
Số trang 36
Dung lượng 0,95 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Contents Overview 1 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

Trang 1

Contents

Overview 1

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

Trang 2

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

Trang 3

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

Trang 4

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

Trang 5

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 http://www.w3.org/style/xsl, the XSLT page at http://www.w3.org/TR/xslt, and the XSL working group page at http://www.w3.org/Style/2000/xsl-charter.html

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

Trang 6

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)

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

Important

Lesson objectives

Trang 7

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

***************************** 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 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

memory-a new document The new document mmemory-ay be formmemory-atted memory-as XML, HTML, XHTML, text, or some other format

Introduction

What is a style sheet?

Trang 8

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

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

transforming XML

What is XSL-FO?

Trang 9

! XSL is no longer supported by XML Core Services

! Current development should use XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

***************************** 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

Trang 10

In this example, an XML source contains information about pets:

<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

Trang 11

Demonstration: Formatting XML Using a Style Sheet

! In this demonstration you will see:

whose columns can be sorted

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

Trang 12

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

Trang 13

What Is XSLT?

! XSLT uses templates to transform XML

! Templates define transformations

! XSLT uses XPath to fix locations and for conditional processing

***************************** 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

Trang 14

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

Trang 15

Parts of an XSLT Style Sheet

! Identify this as an XSLT style sheet

! Match a pattern

! Apply a transformation

! Create output

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

***************************** ILLEGAL FOR NON - TRAINER USE ******************************

Here are the parts of an XSLT style sheet:

! The <xsl:stylesheet> element and namespace The <xsl:stylesheet> element contains the style sheet The namespace identifies the style sheet as such to the XML processor The XML processor

is sensitive to casing of the URI

! The <xsl:template> element with the match or name attribute

A style sheet contains one or more <xsl:template> elements The

<xsl:template> elements match a pattern in the source XML The match

attribute is an XPath pattern If you want to call a specific template pattern

by name, then you can substitute the name attribute for the match attribute

When it is defined, the template can be applied

! The <xsl:apply-templates> element with the select attribute

The <xsl:apply-templates> element applies a transformation to any pattern matched by the XPath expression

! The <xsl:value-of> or <xsl:copy-of> element with the select attribute

One or more <xsl:value-of> or <xsl:copy-of> elements are nested within a

<xsl:template> element The function of these elements is to produce output

Introduction

Trang 16

These are examples of an XML document and an XSLT style sheet They illustrate XSLT template syntax The following is the XML document:

Trang 17

The following XSLT style sheet, which is referenced in the XML document, checks the location of the employee so that the correct currency type for their salary is reported:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:au="http://www.demotest.com" version="1.0">

<! MATCH THE ROOT ELEMENT, CREATE A BASIC HTML DOCUMENT, >

<! AND APPLY-TEMPLATES TO ALL DESCENDANTS >

<! MATCH THE EMPLOYEES ELEMENT AND >

<! APPLY-TEMPLATES TO SUBSETS OF EMPLOYEE ELEMENTS >

<xsl:template match="employees">

<xsl:apply-templates select="employee[salary]"/>

<xsl:apply-templates select="employee[au:salary]"/> </xsl:template>

<! MATCH THE EMPLOYEE ELEMENTS WITH [SALARY] CHILD >

<! AND OUTPUT NAME, JOBTITLE, AND SALARY >

<! MATCH THE EMPLOYEE ELEMENTS WITH [AU SALARY] CHILD >

<! AND OUTPUT NAME, JOBTITLE, AND SALARY >

Trang 18

When the XML document is opened in Microsoft Internet Explorer, the output looks like this:

Ngày đăng: 10/12/2013, 16:15

TỪ KHÓA LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm

w