14
Chapter 3 Chapter 3 XSL T RANSFORMATIONS (XSLT) เนื้อหา Wh t i XSL? 2 What is XSL? XPATH Simple XSLT Internet Technology Chapter 3 What is XSL? 3 XSL stands for Ex tensible S tylesheet L anguage CSS was designed for styling HTML pages, and can be used to style XML pages XSL was designed specifically to style XML pages, and is much more sophisticated than CSS XSL consists of three languages: XSL consists of three languages: XSLT (XSL T ransformations) is a language used to transform XML documents into other kinds of documents (most commonly HTML, th b di l d) so they can be displayed) XPath is a language to select parts of an XML document to transform with XSLT XSL-FO (XSL F ormatting O bjects) is a replacement for CSS There are no current implementations of XSL-FO, and we won’t cover it Internet Technology Chapter 3 What is XSLT? XSLT t d f XSL T f ti 4 XSLT stands for XSL Transformations XSLT is the most important part of XSL XSLT transforms an XML document into another XML document XSLT uses XPath to navigate in XML documents XSLT is a W3C Recommendation Internet Technology Chapter 3

290371 Ch4 XSLT - Burapha Universitysirisuda/290371/290371_Ch3... · 2010-12-07 · XSLT = XSL Transformations yXSLT is the most important part of XSL 5. yXSLT is used to transform

  • Upload
    others

  • View
    14

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 290371 Ch4 XSLT - Burapha Universitysirisuda/290371/290371_Ch3... · 2010-12-07 · XSLT = XSL Transformations yXSLT is the most important part of XSL 5. yXSLT is used to transform

Chapter 3Chapter 3

X S L T R A N S F O R M A T I O N S ( X S L T )

เนอหา

Wh t i XSL?

2

What is XSL?

XPATH

Simple XSLT

Internet Technology Chapter 3

What is XSL?3

XSL stands for Extensible Stylesheet Languagey g gCSS was designed for styling HTML pages, and can be used to style XML pagesXSL was designed specifically to style XML pages, and is much more sophisticated than CSSXSL consists of three languages:XSL consists of three languages:

XSLT (XSL Transformations) is a language used to transform XML documents into other kinds of documents (most commonly HTML,

th b di l d)so they can be displayed)XPath is a language to select parts of an XML document to transform with XSLTXSL-FO (XSL Formatting Objects) is a replacement for CSS

There are no current implementations of XSL-FO, and we won’t cover it

Internet Technology Chapter 3

What is XSLT?

XSLT t d f XSL T f ti

4

XSLT stands for XSL Transformations

XSLT is the most important part of XSL

XSLT transforms an XML document into another XML document

XSLT uses XPath to navigate in XML documentsXSLT is a W3C Recommendation3

Internet Technology Chapter 3

Page 2: 290371 Ch4 XSLT - Burapha Universitysirisuda/290371/290371_Ch3... · 2010-12-07 · XSLT = XSL Transformations yXSLT is the most important part of XSL 5. yXSLT is used to transform

XSLT = XSL Transformations

XSLT is the most important part of XSL

5

XSLT is the most important part of XSL.

XSLT is used to transform an XML document into XSLT is used to transform an XML document into another XML document, or another type of document that is recognized by a browser, like HTML and XHTML. g yNormally XSLT does this by transforming each XML element into an (X)HTML element.

With XSLT you can add/remove elements and attributes to or from the output file You can also rearrange and to or from the output file. You can also rearrange and sort elements, perform tests and make decisions about which elements to hide and display, and a lot more.

Internet Technology Chapter 3

How does it work?6

Th XML d t i d i t XML The XML source document is parsed into an XML source tree

h d fi l h h You use XPath to define templates that match parts of the source tree

You use XSLT to transform the matched part and put the transformed information into the result tree

The result tree is output as a result document

Parts of the source document that are not matched by a template are typically copied unchanged

Internet Technology Chapter 3

XPATH คออะไร ?7

เปนโครงสราง สาหรบ การอางถงโหนดชนดตางๆ ภายในเอกสาร XMLภายในเอกสาร XML

ใช path expression เพอเขาถงเอกสาร XML

ประกอบดวย library ของ standard function

เปนสงสาคญใน XSLT

Internet Technology Chapter 3

Simple XPathp8

Here’s a simple XML document:

<?xml version="1.0"?><library>

XPath expressions look a lot like paths in a computer file <library>

<book><title>XML</title><author>Gregory Brill</author>

a computer file system

/ means the document i lf (b ifi <author>Gregory Brill</author>

</book><book>

i l J d XML / i l

itself (but no specific elements)/library selects the root

l<title>Java and XML</title><author>Brett McLaughlin</author>

</book>

element/library/book selects every book element

</library > //author selects everyauthor element, wherever it occurs

Internet Technology Chapter 3

Page 3: 290371 Ch4 XSLT - Burapha Universitysirisuda/290371/290371_Ch3... · 2010-12-07 · XSLT = XSL Transformations yXSLT is the most important part of XSL 5. yXSLT is used to transform

ใ ใ Aสญลกษณทใชใน XPATH

Expression Description

Node name การเอยชอลอยๆ หมายถง การเขาถงโหนดลกทกตวของโหนดทมชอปรากฏ

โ / หากขนตนดวยเครองหมาย / แลวตามดวยชอโหนด หมายถง การอางจากตาแหนงอางองทเปนโหนดราก หรอ root element ของ XML

// การขนตนดวยเครองหมาย // นาหนาชอโหนด หมายถง การอางโหนดโดยไมสนใจ// การขนตนดวยเครองหมาย // นาหนาชอโหนด หมายถง การอางโหนดโดยไมสนใจวาขอมลจะอยทใดใน XML

. การขนตนดวยเครองหมาย . นาหนาชอหรอการอางอง หมายถงโหนดปจจบน

.. การขนตนดวยเครองหมาย .. หรอใช .. หมายถง โหนดพอแมของโหนดปจจบน

@ เขาถงสมาชกทเปนแอตทรบวตของอลเมนต9Internet Technology Chapter 3

ตวอยางเอกสาร XML<?xml version="1 0" encoding="ISO 8859 1" ?><?xml version= 1.0 encoding= ISO-8859-1 ?><bookstore>

<book>titl l " " H P tt /titl<title lang="en">Harry Potter</title>

<author>J K. Rowling</author> <year>2005</year><price>29.99</price>

</book><book>

<title lang="eng">Learning XML</title><author>Erik T. Ray</author><year>2003</year>y /y<price>39.95</price>

</book></bookstore>

10Internet Technology Chapter 3

คาอธบายตวอยางb k t โ b kbookstore เขาถงโหนดลกทกตวของอลเมนต bookstore/bookstore เขาถงโหนด bookstore โดยโหนด bookstore นตองเปนอลเมนตรากตองเปนอลเมนตรากbookstore/book เขาถงอลเมนต book ทเปนสมาชกของbookstorebookstore//book เลอกอลเมนต book ทกตวไมวาจะอยทใดใน XMLb k t //b k เลอกอลเมนต b k ทเปนอลเมนตbookstore//book เลอกอลเมนต book ทเปนอลเมนตลกหลานของ bookstore จากทกๆ ตาแหนงทเขาลกษณะดงกลาวดงกลาว//@lang เขาถงอลเมนตทกตวทมชอแอดตรบวตวา lang

11Internet Technology Chapter 3

การเขาถงสมาชกวธการเขาถงสมาชกของเอกสาร XML

1. โดยใช Predicate1. โดยใช Predicate2. โดยใช Wildcard3. โดยใช ตวกระทาทางลอจก ( | )4. XPATH Axes (อางองจากโหนดปจจบน)5. ใชการอางโหนดแบบ relative

12Internet Technology Chapter 3

Page 4: 290371 Ch4 XSLT - Burapha Universitysirisuda/290371/290371_Ch3... · 2010-12-07 · XSLT = XSL Transformations yXSLT is the most important part of XSL 5. yXSLT is used to transform

การเขาถงสมาชก โดยใช Predicateการเขาถงสมาชก โดยใช Predicate

1. โดยใช Predicate (เปนการเขาถงสมาชกใน list)เราจะใช [] บรรจลาดบท เพอเจาะจงไปยงตวทตองการตวใดตวหนง* สงเกตวาคาใน [ ] นนจะเรมตนดวย 1 หมายถงสมาชกแรก (ไมใชศนย)

/bookstore/book[1] เขาถงอลเมนต book ตวแรกทเปน/bookstore/book[1] เขาถงอลเมนต book ตวแรกทเปนสมาชกของ bookstore (ซงเปนอลเมนตราก)/bookstore/book[last()] เขาถงอลเมนต book ตวสดทายทเปนสมาชกของ /bookstore/book[last()] เขาถงอลเมนต book ตวสดทายทเปนสมาชกของ bookstore/bookstore/book[last()-1] เขาถงอลเมนต book ตวรองสดทาย/bookstore/book[last() 1] เขาถงอลเมนต book ตวรองสดทาย/bookstore/book[position()<3] เลอกกลมของอลเมนต book ตวแรกและตวทสอง

13Internet Technology Chapter 3

โ ใ การเขาถงสมาชกโดยใช Predicate (ตอ)//title[@lang] เลอกอลเมนตชอ title ทกตวทมแอตตรบวต lang//title[@lang='eng'] เลอกอลเมนตชอ title ทกตวทมแอตตรบวต //title[@lang= eng ] เลอกอลเมนตชอ title ทกตวทมแอตตรบวต lang ซงมคาเทากบ eng/bookstore/book[price>35 00] เลอกอลเมนต book ทเปนอลเมนตลก/bookstore/book[price>35.00] เลอกอลเมนต book ทเปนอลเมนตลกของ bookstore ซงมอลเมนต price ทมคามากกวา 35.00/bookstore/book[price>35 00]/title เลอกอลเมนต title ทเปนโหนด/bookstore/book[price>35.00]/title เลอกอลเมนต title ทเปนโหนดลกของอลเมนต book ซงมอลเมนต price ทมคามากกวา 35.00

14Internet Technology Chapter 3

โ ใ การเขาถงสมาชกโดยใช Wildcard2. โดยใช Wildcard

สามารถใชเครองหมาย * เปนการเอยถงชอใดๆ ทไมเจาะจง หรอใชเมธอด node() ในการอางถงโหนดชนดใดๆ เชน

/bookstore/* เลอกอลเมนตลกทกตวทเปนสมาชกของอลเมนต bookstore//* เลอกอลเมนตทกตวทปรากฏในเอกสาร XML

//title[@*] เลอกอลเมนต title ทกตวทมแอตตรบวตปรากฏฏ

15Internet Technology Chapter 3

โ ใ การเขาถงสมาชกโดยใชตวกระทาทางลอจก ( | )3. โดยใชตวกระทาทางลอจก ( | )ใ ป ใ ใ ใชเครองหมาย | เพอเลอกคาตอบทปรากฏอยในเทอมใดเทอมหนงของเอกสาร XML

//book/title | //book/price หาคาตอบเปนรายการลสตทประกอบไปดวยอลเมนต title และอลเมนต price ทกตวทเปนสมาชกของ bookp

16Internet Technology Chapter 3

Page 5: 290371 Ch4 XSLT - Burapha Universitysirisuda/290371/290371_Ch3... · 2010-12-07 · XSLT = XSL Transformations yXSLT is the most important part of XSL 5. yXSLT is used to transform

โ ใ การเขาถงสมาชกโดยใช XPATH Axes4. โดยใช XPATH Axes (อางองจากโหนดปจจบน)ใ ใ ใช XPATH Axes เพอกาหนดจดตงตนในการเขาถงขอมล XML จากตาแหนงอางองตางๆ เชน

ancestor เลอกโหนดทกตวทเปนโหนดบรรพบรษของโหนดปจจบนancestor-or-self เลอกโหนดปจจบนและโหนดบรรพบรษทกตวancestor or self เลอกโหนดปจจบนและโหนดบรรพบรษทกตวattribute เลอกโหนดแอตตรบวตทกตวทเปนสมาชกของอลเมนตปจจบนhild เลอกโหนดลกของโหนดปจจบนchild เลอกโหนดลกของโหนดปจจบน

descendant เลอกโหนดลกหลานทกตวของโหนดปจจบน

17Internet Technology Chapter 3

โ ใ การเขาถงสมาชกโดยใช XPATH Axes (ตอ)descendant-or-self เลอกโหนดปจจบนและโหนดลกหลานทกตวfollowing เลอกองคประกอบเอกสาร XML ทกตวทอยหลง

แทกปดของอลเมนตปจจบนfollowing-sibling เลอกอลเมนตหรอโหนดทเปนโหนดพนองทกตวทอยตามมาจากfollowing sibling เลอกอลเมนตหรอโหนดทเปนโหนดพนองทกตวทอยตามมาจากโหนดปจจบนnamespace เลอกโหนดทเกบเนมสเปซซงเปนสมาชกของโหนดปจจบนnamespace เลอกโหนดทเกบเนมสเปซซงเปนสมาชกของโหนดปจจบน

18Internet Technology Chapter 3

การเขาถงสมาชกโดยใช XPATH Axes (ตอ) โ parent เลอกโหนดพอแม

preceding เลอกองคประกอบเอกสาร XML ทงหมดทอยกอนหนาแทกเปดของอลป เมนตปจจบน

preceding-sibling เลอกอลเมนตหรอโหนดทเปนโหนดพนองทกตวทอยกอนโหนดป ปจจบนself เลอกโหนดปจจบน

19Internet Technology Chapter 3

โ ใ โ R l iการเขาถงสมาชกโดยใชการอางโหนดแบบ Relative

5. โดยใชการอางโหนดแบบ Relative(กาหนดตาแหนงอางองจากโหนดปจจบน)ไมมการใชเครองหมาย / นาหนา XPATH อาจจะใช Axes โดยตามดวยเครองหมาย :: เพอเขาถงโหนดยอยๆ ทตองการตอไป เชน

child::book เลอกอลเมนต book ทกตวทเปนอลเมนตลกของโหนดปจจบนatttribute::lang เลอกแอดตรบวต lang (ทเปนโหนดลก)ของอลเมนตปจจบน

20Internet Technology Chapter 3

Page 6: 290371 Ch4 XSLT - Burapha Universitysirisuda/290371/290371_Ch3... · 2010-12-07 · XSLT = XSL Transformations yXSLT is the most important part of XSL 5. yXSLT is used to transform

การเขาถงสมาชก โดยใชการอางโหนดแบบ Relative (ตอ)

child::* เลอกโหนดลกทกตวของโหนดปจจบนtt ib t * เลอกแอดตรบวตทกตวของอลเมนตปจจบนattribute::* เลอกแอดตรบวตทกตวของอลเมนตปจจบน

child::text() เลอกเทกซโหนดทกตวทเปนโหนดลกของโหนดปจจบนchild::node() เลอกโหนดลกทกตวของโหนดปจจบน

21Internet Technology Chapter 3

โ ใ โ การเขาถงสมาชกโดยใชการอางโหนดแบบ Relative (ตอ)descendant::book เลอกอลเมนต book ทกตวทเปนโหนดลกหลานของโหนดdescendant::book เลอกอลเมนต book ทกตวทเปนโหนดลกหลานของโหนดปจจบน

t b k เลอกอลเมนต b k ทกตวทเปนโหนดบรรพบรษของโหนดancestor::book เลอกอลเมนต book ทกตวทเปนโหนดบรรพบรษของโหนดปจจบน

ป child::*/child::price เลอกอลเมนต price ซงเปนอลเมนตลกของอลเมนตทกตวทเปนอลเมนตลกของโหนดปจจบน (เลอกอลเมนต price ทกตวทมฐานะเปน

ป หลานของอลเมนตปจจบน

22Internet Technology Chapter 3

A โ ป XPATH โอเปอรเรเตอรOperator Description Example Return valueOperator Description Example Return value

| Computes two node-sets //book | //cd Returns a node-set with all book cd elementsa boo cd e e e ts

+ Addition 6 + 4 10

- Subtraction 6 - 4 2- Subtraction 6 - 4 2

* Multiplication 6 * 4 24

div Division 8 div 4 2

= Equal Price = 9.80 true if price is 9.80false if price is 9.90

!= Not equal Price != 9.80 true if price is 9.90false if price is 9.80

23Internet Technology Chapter 3

Operator Description Example Return valueOperator Description Example Return value< Less than price < 9.80 true if price is 9.00

false if price is 9.80p<= Less than or equal to price <= 9.80 true if price is 9.00

false if price is 9.90> Greater than price > 9.80 true if price is 9.90

false if price is 9.80>= Greater than or equal to price >= 9.80 true if price is 9.90

false if price is 9.70i 9 80 d if i i 9 80or or price = 9.80 and

price = 9.70true if price is 9.80false if price is 9.50

and and price > 9.00 and true if price is 9.80and and price 9.00 and price < 9.90

true if price is 9.80false if price is 8.50

mod Modulus 5 mod 2 1(division remainder)

24Internet Technology Chapter 3

Page 7: 290371 Ch4 XSLT - Burapha Universitysirisuda/290371/290371_Ch3... · 2010-12-07 · XSLT = XSL Transformations yXSLT is the most important part of XSL 5. yXSLT is used to transform

ตวอยางเอกสาร XML<?xml version="1 0" encoding="ISO 8859 1" ?><?xml version= 1.0 encoding= ISO-8859-1 ?><bookstore>

<book>titl l " " H P tt /titl<title lang="en">Harry Potter</title>

<author>J K. Rowling</author> <year>2005</year><price>29.99</price>

</book><book>

<title lang="eng">Learning XML</title><author>Erik T. Ray</author><year>2003</year>y /y<price>39.95</price>

</book></bookstore>

25Internet Technology Chapter 3

Simple XSLTp

<xsl:for each select "//book"> loops through every <xsl:for-each select= //book > loops through every book element, everywhere in the document

<xsl:value-of select="title"/> chooses the content of the title element at the current locationt e t tle e e e t at t e cu e t ocat o

<xsl:for-each select="//book"><xsl:value-of select="title"/>

</xsl:for-each>h h f h titl l f h chooses the content of the title element for each

book in the XML document

26Internet Technology Chapter 3

Using XSL to create HTMLg

O goal is to t thi Into HTML that Our goal is to turn this:<?xml version="1.0"?><library>

Into HTML that displays something like this:y

<book><title>XML</title><author>Gregory Brill</author>

Book Titles:• XML<author>Gregory Brill</author>

</book><book><title>Java and XML</title>

• XML• Java and XML

Book Authors: G B ill<title>Java and XML</title>

<author>Brett McLaughlin</author></book>/lib

• Gregory Brill• Brett McLaughlin

</library >Note that we’ve grouped titles and

th t l

27

authors separatelyInternet Technology Chapter 3

What we need to do

W d t XML i t fil (l t’ ll it We need to save our XML into a file (let’s call it books.xml)

d fil ( b k l) h We need to create a file (say, books.xsl) that describes how to select elements from books.xml and

b d th i t HTML embed them into an HTML pageWe do this by intermixing the HTML and the XSL in the books xsl filebooks.xsl file

We need to add a line to our books.xml file to tell it to refer to books xsl for formatting informationto refer to books.xsl for formatting information

28Internet Technology Chapter 3

Page 8: 290371 Ch4 XSLT - Burapha Universitysirisuda/290371/290371_Ch3... · 2010-12-07 · XSLT = XSL Transformations yXSLT is the most important part of XSL 5. yXSLT is used to transform

books.xml, revised,

<?xml version "1 0"?><?xml version= 1.0 ?><?xml-stylesheet type="text/xsl" href="books.xsl"?><library>library

<book><title>XML</title> This tells you <author>Gregory Brill</author>

</book><book>

where to find the XSL file

<book><title>Java and XML</title><author>Brett McLaughlin</author>author Brett McLaughlin /author

</book></library >

29Internet Technology Chapter 3

Desired HTML

<html><head><title>Book Titles and Authors</title>

</head> Brown text is data extracted <body><h2>Book titles:</h2><ul>

Brown text is data extracted from the XML document

<li>XML</li><li>Java and XML</li>

</ul>h2 B k th /h2

Yellow text is our HTML template<h2>Book authors:</h2>

<ul><li>Gregory Brill</li>li Brett McLaughlin /li

HTML template

We don’t necessarily <li>Brett McLaughlin</li></ul>

</body></html>

We don t necessarily know how much data

we will have

30

</html>

Internet Technology Chapter 3

XSL outline

<?xml version "1 0" encoding "ISO 8859 1"?><?xml version= 1.0 encoding= ISO-8859-1 ?>

l t l h t i "1 0"<xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<html> ... </html>

</xsl:template></xsl:stylesheet>

31

</xsl:stylesheet>

Internet Technology Chapter 3

Selecting titles and authorsg

<h2>Book titles:</h2><ul>

<xsl:for-each select="//book"><xsl:for each select= //book ><li>

<xsl:value-of select="title"/>/li

Notice the xsl:for</li>

</xsl:for-each></ul>

xsl:for-each loop

ul<h2>Book authors:</h2>

...same thing, replacing title with authorN i h XSL h d h HTML Notice that XSL can rearrange the data; the HTML result can present information in a different order than the XML

32

than the XML

Internet Technology Chapter 3

Page 9: 290371 Ch4 XSLT - Burapha Universitysirisuda/290371/290371_Ch3... · 2010-12-07 · XSLT = XSL Transformations yXSLT is the most important part of XSL 5. yXSLT is used to transform

All of books.xml

<?xml version "1 0"?><?xml version= 1.0 ?><?xml-stylesheet type="text/xsl" href="books.xsl"?><library>library

<book><title>XML</title><author>Gregory Brill</author>

</book><book><book>

<title>Java and XML</title><author>Brett McLaughlin</author>author Brett McLaughlin /author

</book></library >

Note: if you do View Source, this is what you will see not

33

this is what you will see, not the resultant HTML

Internet Technology Chapter 3

All of books.xsl

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"

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

<h2>Book authors:</h2><ul><xsl:for-each

<xsl:template match="/"><html><head><title>Book Titles and Authors</title>

select="//book"><li><xsl:value-of

l " h "/</head><body><h2>Book titles:</h2><ul>

select="author"/></li>

</xsl:for-each>/ lul

<xsl:for-each select="//book"><li><xsl:value-of select="title"/>

</li>

</ul></body>

</html></xsl:template></li>

</xsl:for-each></ul>

</xsl:template></xsl:stylesheet>

34Internet Technology Chapter 3

How to use it

In a modern browser such as Netscape 6 In a modern browser, such as Netscape 6, Internet Explorer 6, or Mozilla 1.0, you can just open the XML fileopen the XML file

Older browsers will ignore the XSL and just show you the XML contents as continuous text

You can use a program such as Xalan, MSXML, or Saxon to create the HTML as a fileor Saxon to create the HTML as a file

This can be done on the server side, so that all the client side browser sees is plain HTML

The server can create the HTML dynamically from the information currently in XML

35Internet Technology Chapter 3

The result (in IE)( )

36Internet Technology Chapter 3

Page 10: 290371 Ch4 XSLT - Burapha Universitysirisuda/290371/290371_Ch3... · 2010-12-07 · XSLT = XSL Transformations yXSLT is the most important part of XSL 5. yXSLT is used to transform

Examplep

? l i " " di "ISO 88 "?

37

<?xml version="1.0" encoding="ISO-8859-1"?><catalog>

<cd><title>Empire Burlesque</title><artist>Bob Dylan</artist><country>USA</country>y / y<company>Columbia</company><price>10.90</price><year>1985</year><year>1985</year>

</cd>...</catalog>

Internet Technology Chapter 3

Create an XSL Style Sheety

<?xml version="1 0" encoding="ISO-8859- l f h l t " t l / d"

38

<?xml version= 1.0 encoding= ISO-8859-1"?>

<xsl:stylesheet version="1.0"

<xsl:for-each select="catalog/cd"><tr>

<td><xsl:value-of select "title"/></td><xsl:stylesheet version 1.0

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

select= title /></td><td><xsl:value-of

select="artist"/></td></tr>

<xsl:template match="/"><html><body>

</tr></xsl:for-each></table>

</body>y<h2>My CD Collection</h2><table border="1"><tr bgcolor="#9acd32">

</body></html>

</xsl:template>

<th align="left">Title</th><th align="left">Artist</th>

</tr>

</xsl:stylesheet>

Internet Technology Chapter 3

Link the XSL Style Sheet to the XML Documenty

? l i " " di "ISO 88 "?

39

<?xml version="1.0" encoding="ISO-8859-1"?><?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?><catalog>

<cd><title>Empire Burlesque</title><artist>Bob Dylan</artist>y /<country>USA</country><company>Columbia</company><price>10.90</price><price>10.90</price><year>1985</year>

</cd>...</catalog></catalog>

Internet Technology Chapter 3

XSLT <xsl:template> Element40

xsl style sheet ประกอบไปดวยเซตของกฎตางๆหรอรปแบบตาง เรยกวา template template

<xsl template> ใ ใ template<xsl:template> ใชในการสราง template<xsl:template match="/">

math attribute ใชเชอมความสมพนธระหวาง tempate กบ xml element คาขางในคอ XPath expression

Internet Technology Chapter 3

Page 11: 290371 Ch4 XSLT - Burapha Universitysirisuda/290371/290371_Ch3... · 2010-12-07 · XSLT = XSL Transformations yXSLT is the most important part of XSL 5. yXSLT is used to transform

XSLT <xsl:template> Elementp

<?xml version="1.0" encoding="ISO- <tr>

41

8859-1"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999

<td>.</td>

<td>.</td>

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

<xsl:template match="/">

</tr>

</table>

</body><xsl:template match= / >

<html>

<body>

</html>

</xsl:template>

<h2>My CD Collection</h2>

<table border="1">

<tr bgcolor="#9acd32">

</xsl:stylesheet>

<tr bgcolor #9acd32 >

<th>Title</th>

<th>Artist</th>

/t</tr>

Internet Technology Chapter 3

XSLT <xsl:value-of> Element42

<xsl:value-of> ใชสาหรบแสดงคาของโนดทถกเลอก

<xsl:value-of select="catalog/cd/title"/>

คาของattribute select คอ XPath expression p

Internet Technology Chapter 3

XSLT <xsl:value-of> Element

<?xml version="1.0" encoding="ISO- <tr>

43

8859-1"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999

<td><xsl:value-of select="catalog/cd/title"/></td>

<td><xsl:value-of xmlns:xsl http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

td xsl:value of select="catalog/cd/artist"/></td>

</tr>

</table><xsl:template match= / >

<html>

<body>

</table>

</body>

</html>

<h2>My CD Collection</h2>

<table border="1">

<tr bgcolor="#9acd32">

</xsl:template>

</xsl:stylesheet><tr bgcolor #9acd32 >

<th>Title</th>

<th>Artist</th>

/t

</xsl:stylesheet>

</tr>

Internet Technology Chapter 3

XSLT <xsl:for-each> Element44

<xsl:for-each> ใชสาหรบวน loop

<xsl:for-each select="catalog/cd">.

</xsl:for-each>

attribute select คอ XPath expression p

Internet Technology Chapter 3

Page 12: 290371 Ch4 XSLT - Burapha Universitysirisuda/290371/290371_Ch3... · 2010-12-07 · XSLT = XSL Transformations yXSLT is the most important part of XSL 5. yXSLT is used to transform

XSLT <xsl:for-each> Element

<?xml version="1.0" encoding="ISO- </tr>

45

8859-1"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999

<xsl:for-each select="catalog/cd">

<tr>

<td><xsl:value-of xmlns:xsl http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<td><xsl:value-of select="title"/></td>

<td><xsl:value-of select="artist"/></td><xsl:template match= / >

<html>

<body>

select= artist /></td>

</tr>

</xsl:for-each>

<h2>My CD Collection</h2>

<table border="1">

<tr bgcolor="#9acd32">

</table>

</body>

</html><tr bgcolor #9acd32 >

<th>Title</th>

<th>Artist</th>

</html>

</xsl:template>

/ l t l h t</xsl:stylesheet>

Internet Technology Chapter 3

Filtering the Outputg p

We can also filter the output from the XML file by adding a

46

We can also filter the output from the XML file by adding a criterion to the select attribute in the <xsl:for-each> element.

<xsl:for-each select="catalog/cd[artist='Bob Dylan']">

Legal filter operators are:

• = (equal)• != (not equal)• &lt; less than• &gt; greater than

Internet Technology Chapter 3

Filtering the Outputg p

<?xml version="1.0" encoding="ISO-88 "?

<xsl:for-each l " l / d[ i ' b

47

8859-1"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/T f "

select="catalog/cd[artist='Bob Dylan']">

<tr>

td l l f /Transform">

<xsl:template match="/">

h l

<td><xsl:value-of select="title"/></td>

<td><xsl:value-of select="artist"/></td><html>

<body>

<h2>My CD Collection</h2>

select= artist /></td>

</tr>

</xsl:for-each>

</table><table border="1">

<tr bgcolor="#9acd32">

<th>Title</th>

</table>

</body>

</html>

</xsl:template><th>Artist</th>

</tr>

</xsl:template>

</xsl:stylesheet>

Internet Technology Chapter 3

XSLT <xsl:sort> Element48

The <xsl:sort> element is used to sort the output.ใชในการจดเรยงลาดบ element ทระบในการแสดงผล

Where to put the Sort Informationp

To sort the output, simply add an <xsl:sort> p , p yelement inside the <xsl:for-each>element in the XSL file:

Internet Technology Chapter 3

Page 13: 290371 Ch4 XSLT - Burapha Universitysirisuda/290371/290371_Ch3... · 2010-12-07 · XSLT = XSL Transformations yXSLT is the most important part of XSL 5. yXSLT is used to transform

XSLT <xsl:sort> Element

<?xml version="1.0" encoding="ISO- </tr>

49

8859-1"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999

<xsl:for-each select="catalog/cd">

<xsl:sort select="artist"/>

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

<xsl:template match="/">

<tr>

<td><xsl:value-of select="title"/></td>

td l l f <xsl:template match= / >

<html>

<body>

<td><xsl:value-of select="artist"/></td>

</tr>

<h2>My CD Collection</h2>

<table border="1">

<tr bgcolor="#9acd32">

</xsl:for-each>

</table>

</body><tr bgcolor #9acd32 >

<th>Title</th>

<th>Artist</th>

</body>

</html>

</xsl:template>

</xsl:stylesheet>Internet Technology Chapter 3

XSLT <xsl:if> Element50

The < sl if> ele e t is sed to t a co ditio alThe <xsl:if> element is used to put a conditionaltest against the content of the XML file.

Syntax

<xsl:if test="expression">......some output if the expression is true......

</xsl:if>

Internet Technology Chapter 3

XSLT <xsl:if> Element

<?xml version="1.0" encoding="ISO-88 "?

<tr>

51

8859-1"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/T f "

<td><xsl:value-of select="title"/></td>

<td><xsl:value-of l t " ti t"/ /tdXSL/Transform">

<xsl:template match="/">

<html>

b d

select="artist"/></td>

</tr>

</xsl:if>

/ l f h<body>

<h2>My CD Collection</h2>

<table border="1">

</xsl:for-each>

</table>

</body>

<tr bgcolor="#9acd32">

<th>Title</th>

<th>Artist</th>

</html>

</xsl:template>

</xsl:stylesheet>

</tr>

<xsl:for-each select="catalog/cd">

<xsl:if test="price &gt; 10">

Internet Technology Chapter 3

XSLT <xsl:choose> Element52

The < sl choose> ele e t is sed i co j ctio ith The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional testsconditional tests.

The <xsl:choose> ElementThe <xsl:choose> Element

Syntax<xsl:choose>

Syntax <xsl:when test="expression">... some output ...

</xsl:when>l h i<xsl:otherwise>

... some output ....</xsl:otherwise>

</xsl:choose>

Internet Technology Chapter 3

</xsl:choose>

Page 14: 290371 Ch4 XSLT - Burapha Universitysirisuda/290371/290371_Ch3... · 2010-12-07 · XSLT = XSL Transformations yXSLT is the most important part of XSL 5. yXSLT is used to transform

XSLT <xsl:choose> Element

<?xml version="1.0" encoding="ISO-88 "?

<tr>

d l l f l " i l "/ / d

53

8859-1"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/T f "

<td><xsl:value-of select="title"/></td>

<xsl:choose>

<xsl:when test="price &gt; 10">

<td bgcolor="#ff00ff">/Transform">

<xsl:template match="/">

h l

<td bgcolor= #ff00ff >

<xsl:value-of select="artist"/></td>

</xsl:when>

<xsl:otherwise><html>

<body>

<h2>My CD Collection</h2>

<td><xsl:value-of select="artist"/></td>

</xsl:otherwise>

</xsl:choose>

/<table border="1">

<tr bgcolor="#9acd32">

<th>Title</th>

</tr>

</xsl:for-each>

</table>

</body><th>Artist</th>

</tr>

<xsl:for-each select="catalog/cd">

</body>

</html>

</xsl:template>

Internet Technology Chapter 3

XSLT <xsl:choose> Element54

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" <xsl:when test="price &gt; 9">xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<xsl:when test= price &gt; 9 ><td bgcolor="#cccccc"><xsl:value-of select="artist"/></td>

</xsl:when><html><body><h2>My CD Collection</h2><table border="1">

<xsl:otherwise><td><xsl:value-of select="artist"/></td>

</xsl:otherwise></xsl:choose>

<tr bgcolor="#9acd32"><th>Title</th><th>Artist</th>

</tr>l f h l l d

</xsl:choose></tr></xsl:for-each>

</table>/b d<xsl:for-each select="catalog/cd">

<tr><td><xsl:value-of select="title"/></td>

<xsl:choose>l h " i "

</body></html>

</xsl:template>

<xsl:when test="price &gt; 10"><td bgcolor="#ff00ff"><xsl:value-of select="artist"/></td>

</xsl:when>

</xsl:stylesheet>

Internet Technology Chapter 3

XSLT <xsl:apply-templates> Elementpp y p55

The <xsl:apply-templates> The <xsl:apply-templates> element applies a template to the current element or to the current element's child nodesor to the current element s child nodes.

• If we add a select attribute to the <xsl:apply-• If we add a select attribute to the <xsl:apply-templates> element it will process only the child element that matches the value of the attribute element that matches the value of the attribute. • We can use the select attribute to specify the order in which the child nodes are processedin which the child nodes are processed.

Internet Technology Chapter 3

XSLT <xsl:apply-templates> Elementpp y p56

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="title">Title: <span style="color:#ff0000"><xsl:value-of select="."/></span>

<xsl:template match="/"><html><body><h2>My CD Collection</h2>

<xsl:value of select . /></span><br /></xsl:template>

l l h " i "y /

<xsl:apply-templates/> </body></html></xsl:template>

<xsl:template match="artist">Artist: <span style="color:#00ff00"><xsl:value-of select="."/></span><br />/ p

<xsl:template match="cd"><p><xsl:apply-templates select="title"/>

<br /></xsl:template>

</xsl:stylesheet>pp y p /

<xsl:apply-templates select="artist"/></p></xsl:template>

Internet Technology Chapter 3