106
8/3/2019 06 - XML - XSLT http://slidepdf.com/reader/full/06-xml-xslt 1/106 e X tensible M arkup L anguage Lecturer: Phan Vo Minh Thang MSc. eXtensible Markup Language Using XSLT and XPath to Enhance HTML Documents Thanks to Roger L. Costello <?xml version=“1.0”> <course startdate=“February 06, 2006”> <title> eXtensible Markup Language </title> <lecturer>Phan Vo Minh Thang</lecturer> </course>

06 - XML - XSLT

Embed Size (px)

Citation preview

Page 1: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 1/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

eXtensible Markup Language

Using XSLT and XPath to Enhance HTMLDocuments

Thanks to Roger L. Costello

<?xml version=“1.0”><course startdate=“February 06, 2006”>

<title> eXtensible Markup Language </title><lecturer>Phan Vo Minh Thang</lecturer>

</course>

Page 2: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 2/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

History

XSL

XSLXSLT

XSLTXPath

XLink/ 

XPointer

XQuery

XML

Schemas

(high-precision graphics, e.g., PDF)

(low-precision

graphics, e.g.,HTML,

text, XML)

Page 3: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 3/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Note

For brevity, instead of using the term XSLT/XPath, I willsimply call it XSL.

? l i “1 0” t i l XML L t N t ti id “06” XSLT / ti / t i l

Page 4: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 4/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Multiple Output Formats

XSL may be used to generate either HTML, XML, or text

XSL Processor

XSL

XML HTML (or XML or text)

? ml i “1 0” t i l XML Lect res Notes ti id “06” XSLT / ti / t i l

Page 5: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 5/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

xalan/xt/saxon

xalan: A free XSL processor, implemented in Java, from Apache(http://www.apache.org/)

xt: A free XSL processor, implemented in Java, from James Clark(http://www.jclark.com/)

saxon: A free XSL processor, implemented in Java, from Michael Kay(http://users.iclway.co.uk/mhkay/saxon

xalan/xt/saxon

XML XSL

HTML (or XML or text)

Invoking from a DOS command line:

run-xalan FitnessCenter.xml FitnessCenter.xsl FitnessCenter.htmlrun-xt FitnessCenter.xml FitnessCenter.xsl FitnessCenter.html

run-saxon FitnessCenter.xml FitnessCenter.xsl FitnessCenter.html

<?xml version “1 0”> <material> XML Lectures Notes <section id “06”> XSLT </section> </material>

Page 6: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 6/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<? xml version= 1.0 > <material> XML Lectures Notes  <section id=  06 > XSLT </section> </material> 

Styling XML Documents using IE6 or Netscape7

Put a stylesheet PI at the top of your XML document. Now you can simply drop the XML document into the browser and the XML

will be automatically styled using the stylesheet referenced in the stylesheetPI.

<?xml version="1.0"?>

<?xml-stylesheet type="text/xsl" href="FitnessCenter.xsl"?>

<FitnessCenter><Member level="platinum">

<Name>Jeff</Name>

<Phone type="home">555-1234</Phone>

<Phone type="work">555-4321</Phone><FavoriteColor>lightgrey</FavoriteColor>

</Member>

</FitnessCenter>

Add this

stylesheetPI to the

top of 

your XMLdocument

<?xml version=“1 0”> <material> XML Lectures Notes <section id=“06”> XSLT </section> </material>

Page 7: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 7/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<? xml version= 1.0 > <material> XML Lectures Notes  <section id=  06 > XSLT </section> </material> 

HTML Generation

We will first use XSL to generate HTML documents When generating HTML, XSL should be viewed as a tool to enhance

HTML documents.

• That is, the HTML documents may be enhanced by extracting dataout of XML documents

• XSL provides elements (tags) for extracting the XML data, thusallowing us to enhance HTML documents with data from an XML

document

<?xml version=“1 0”> <material> XML Lectures Notes <section id=“06”> XSLT </section> </material>

Page 8: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 8/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<? xml version= 1.0 > <material> XML Lectures Notes  <section id=  06 > XSLT </section> </material> 

Enhancing HTML Documents with XML Data

XMLDocument

HTML

Document(with embedded

XSL elements)

XSL element

XML data

XSLProcessor

XML data

<?xml version=“1.0”> <material> XML Lectures Notes <section id=“06”> XSLT </section> </material>

Page 9: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 9/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<? xml version 1.0 > <material> XML Lectures Notes  <section id  06 > XSLT </section> </material> 

Enhancing HTML Documents with theFollowing XML Data

<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="FitnessCenter.xsl"?>

<FitnessCenter>

<Member level="platinum">

<Name>Jeff </ Name><Phone type="home">555-1234</ Phone>

<Phone type="work">555-4321</ Phone>

<FavoriteColor>lightgrey</ FavoriteColor></ Member>

</ FitnessCenter>

FitnessCenter.xml

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Page 10: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 10/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

< e s o 0 > < ate a > ectu es otes <sect o d 06 > S </sect o > </ ate a >

Embed HTML Document in an XSL Template

<?xml version="1.0"?>

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

version="1.0">

<xsl:output method="html"/>

<xsl:template match="/">

<HTML><HEAD>

<TITLE>Welcome</TITLE></HEAD><BODY>

Welcome!</BODY>

</HTML></xsl:template>

</xsl:stylesheet>

FitnessCenter.xsl (see html-example01)

Note how we

have the HTML

document

embedded withian XSL template

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Page 11: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 11/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Note

The HTML is embedded within an XSL template, which isan XML document

• Consequently, the HTML must be well formed, i.e.,

every start tag must have an end tag

Because the HTML is embedded within an XSL template,we are able to add XSL elements to the HTML, allowing

us to extract data out of XML documents

Let's customize the HTML welcome page by putting in themember's name. This is achieved by extracting the namefrom the XML document. We use an XSL element to dothis.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Page 12: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 12/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Extracting the Member Name

<?xml version="1.0"?>

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

version="1.0">

<xsl:output method="html"/>

<xsl:template match="/">

<HTML>

<HEAD>

<TITLE>Welcome</TITLE></HEAD>

<BODY>

Welcome <xsl:value-of select="/FitnessCenter/Member/Name"/>!</BODY>

</HTML></xsl:template>

</xsl:stylesheet>

(see html-example02)

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Page 13: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 13/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Note

Notice how we have enhanced the HTML document byusing data from the XML document!

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Page 14: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 14/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Extracting a Value from an XML Document,Navigating the XML Document

Extracting values:

• use the <xsl:value-of select="…"/> XSL element

Navigating:

• The slash ("/") indicates parent/child relationship

• A slash at the beginning of the path indicates that it isan absolute path, starting from the top of the XMLdocument

 /FitnessCenter/Member/Name

"Start from the top of the XML document, go to the FitnessCenter element,

from there go to the Member element, and from there go to the Name element."

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Page 15: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 15/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Document / 

PI<?xml version=“1.0”?>

ElementFitnessCenter

ElementMember

ElementName

ElementPhone

ElementPhone

ElementFavoriteColor

TextJeff 

Text555-1234

Text555-4321

Textlightgrey

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Page 16: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 16/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Extract the FavoriteColor and use it as the bgcolor

<?xml version="1.0"?>

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

version="1.0">

<xsl:output method="html"/>

<xsl:template match="/">

<HTML>

<HEAD>

<TITLE>Welcome</TITLE></HEAD>

<BODY bgcolor="{/FitnessCenter/Member/FavoriteColor}">Welcome <xsl:value-of select="/FitnessCenter/Member/Name"/>!

</BODY>

</HTML></xsl:template>

</xsl:stylesheet>

(see html-example03)

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Page 17: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 17/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Note

Attribute values cannot contain "<" nor ">"

- Consequently, the following is NOT valid:

<Body bgcolor="<xsl:value-of select='/FitnessCenter/Member/FavoriteColor'/>">

To extract the value of an XML element and use it as an attribute

value you must use curly braces:<Body bgcolor="{/FitnessCenter/Member/FavoriteColor}">

Evaluate the expression within

the curly braces. Assign the value

to the attribute.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Page 18: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 18/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Extract the Home Phone Number

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

version="1.0">

<xsl:output method="html"/>

<xsl:template match="/">

<HTML>

<HEAD>

<TITLE>Welcome</TITLE>

</HEAD><BODY bgcolor="{/FitnessCenter/Member/FavoriteColor}">

Welcome <xsl:value-of select="/FitnessCenter/Member/Name"/>!

<BR/>

Your home phone number is:

<xsl:value-of select="/FitnessCenter/Member/Phone[@type='home']"/></BODY>

</HTML>

</xsl:template>

</xsl:stylesheet>

(see html-example04)

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Page 19: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 19/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Note

In this example we want "the Phone element where the value

of its type attribute equals 'home' ":

<xsl:value-of select="/FitnessCenter/Member/Phone[@type='home']"/>

The expression within […] is called a "predicate". Its purposeis to filter.

Note the use of the single quotes within the double quotes.select=" … ' …' …"

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Page 20: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 20/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Review - HTML Table

<table border=“1” width=“100%”>

</table>

<tr>

<tr>

<tr> </tr>

</tr>

</tr><th>

</th><th>

</th><th>

</th>

<td>

</td>

<td>

</td><td>

</td>

<td></td>

<td></td>

<td></td>

This will create a table with 3 rows - the first row contains a header

for each column. The next two rows contains the table data.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Page 21: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 21/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<table border=“1” width=“75%”>

<tr><th>Fruit</th>

<th>Color</th>

</tr>

<tr>

<td>Papaya</td>

<td>Red</td>

</tr>

<tr>

<td>Banana</td>

<td>Yellow</td>

</tr>

</table>

Fruit Color

Papaya Red

Banana Yellow

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

C T bl f Ph N b

Page 22: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 22/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Create a Table of Phone Numbers

Suppose that a Member has an arbitrary number of phonenumbers (home, work, cell, etc).

Create an HTML table comprised of the phone numbers.

On each row of the table put the type (home, work, cell,etc) in one column and the actual phone number in thenext column.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

<? l i "1 0"?>

Page 23: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 23/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<?xml version="1.0"?>

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

version="1.0">

<xsl:output method="html"/>

<xsl:template match="/">

<HTML>

<HEAD>

<TITLE>Welcome</TITLE>

</HEAD><BODY bgcolor="{/FitnessCenter/Member/FavoriteColor}">

Welcome <xsl:value-of select="/FitnessCenter/Member/Name"/>!

<BR/>

Your phone numbers are:

<TABLE border="1" width="25%"><TR><TH>Type</TH><TH>Number</TH></TR><xsl:for-each select="/FitnessCenter/Member/Phone">

<TR><TD><xsl:value-of select="@type"/></TD><TD><xsl:value-of select="."/></TD>

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

</TABLE></BODY>

</HTML>

</xsl:template>

</xsl:stylesheet>

(see html-example05)

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

It ti th h XML El t

Page 24: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 24/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Iterating through XML Elements

<xsl:for-each select="/FitnessCenter/Member/Phone">

<!- - Within here we are at one of the Phone elements.

Thus, in <xsl:value-of select=" path", the value for

 path is relative to where we are in the XMLdocument. The "." refers to the Phone element

that we are currently positioned at. - ->

</xsl:for-each>

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Page 25: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 25/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Absolute Path versus Relative Path

<xsl:value-of select="/FitnessCenter/Member/Phone[@type='home']"/ 

This is an absolute xPath expression

(we start from the top of the XML tree

and navigate down the tree)

<xsl:value-of select="@type"/>

This is a relative xPath expression (relative

to where we currently are located, give me

the value of the type attribute)

Do Lab1,

Parts 1-3

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Special Offer to Platinum Members

Page 26: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 26/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Special Offer to Platinum Members

Let's further enhance our example to provide a specialoffer to "platinum" members.

We need to check to see if the "level" attribute on the

Member element equals "platinum".

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Page 27: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 27/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<HTML>

<HEAD>

<TITLE>Welcome</TITLE>

</HEAD><BODY bgcolor="{/FitnessCenter/Member/FavoriteColor}">

Welcome <xsl:value-of select="/FitnessCenter/Member/Name"/>!

<BR/>

<xsl:if test="/FitnessCenter/Member/@level='platinum'">

Our special offer to platinum members today is ...<BR/>

</xsl:if>Your phone numbers are:

<TABLE border="1" width="25%">

<TR><TH>Type</TH><TH>Number</TH></TR><xsl:for-each select="/FitnessCenter/Member/Phone">

<TR>

<TD><xsl:value-of select="@type"/></TD>

<TD><xsl:value-of select="."/></TD>

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

</TABLE>

</BODY>

</HTML>

(see html-example06)

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Conditional Processing

Page 28: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 28/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Conditional Processing

Use the <xsl:if test="…"/> element to perform conditionalprocessing.

Do Lab1,

Part 4

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Accessing Multiple Parts of the XML

Page 29: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 29/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Accessing Multiple Parts of the XMLDocument Let's enhance the table to contain three columns - the

name of the Member, the type of the phone (home, work,cell, etc), and the actual phone number.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Page 30: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 30/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<HTML>

<HEAD>

<TITLE>Welcome</TITLE>

</HEAD><BODY bgcolor="{/FitnessCenter/Member/FavoriteColor}">

Welcome <xsl:value-of select="/FitnessCenter/Member/Name"/>!

<BR/>

<xsl:if test="/FitnessCenter/Member/@level='platinum'">

Our special offer to platinum members today is ...<BR/>

</xsl:if>

Your phone numbers are:

<TABLE border="1" width="25%">

<TR><TH>Name</TH><TH>Type</TH><TH>Number</TH></TR><xsl:for-each select="/FitnessCenter/Member/Phone">

<TR>

<TD><xsl:value-of select="../Name"/></TD><TD><xsl:value-of select="@type"/></TD>

<TD><xsl:value-of select="."/></TD>

</TR>

</xsl:for-each>

</TABLE>

</BODY>

</HTML>

(see html-example07)

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Page 31: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 31/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Getting the Name when accessing the

Phone

Member

Phone

555-1234

Phone

555-4321

Name

Jeff 

Notice how

when in the for-eachloop we need to access

the Name which is "up

and over" with respect

to the Phone element

Bottom line: we can access elements in other parts of the XML tree via the “../” operator.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Other ways to Access the XML Data

Page 32: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 32/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Other ways to Access the XML Data

<xsl:value-of select="/FitnessCenter/Member[1]/Name"/>

"Select the Name of the first Member"

<xsl:value-of select="/FitnessCenter/Member[position()=1]/Name"/>

"Select the Name of the first Member"

<xsl:value-of select="/FitnessCenter/Member[last()]/Name"/>

"Select the Name of the last Member"

<xsl:for-each select="/FitnessCenter/Member[not(position()=last())]">

<!- - Process all Members but the last - ->

</xsl:for-each>

 Note: Assume that there are multiple Members

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Page 33: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 33/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Other ways to Access the XML Data (cont.)

<xsl:for-each select="/FitnessCenter/Member[position() != last())]">

<!- - Process all Members but the last - -></xsl:for-each>

<xsl:for-each select="/FitnessCenter/Member[position() &gt;1]">

<!- - Process all Members but the first - ->

</xsl:for-each>

<xsl:for-each select="/FitnessCenter//Name"><!- - Process all Name elements which have FitnessCenter

as an ancestor - ->

</xsl:for-each>

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Page 34: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 34/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Other ways to Access the XML Data (cont.)

<!- - Iterate through a list of Member nodes - ->

<xsl:for-each select="/FitnessCenter/Member">

<!- - Output the Name of the "current" Member - ->

<xsl:value-of select="./Name"/>

</xsl:for-each>

<!- - Since a specific Member is not specified, all Member nodes - ->

<!- - all selected. That is, the Name node within each Member - ->

<!-- node is selected. --><xsl:for-each select="/FitnessCenter/Member/Name">

<xsl:value-of select="."/>

</xsl:for-each>

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Nodelist

Page 35: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 35/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Nodelist

This xPath expression:

 /FitnessCenter/Member

selects a list of nodes (a list of Member nodes).This list of nodes is called a "nodelist".

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Enhanced XML Document

Page 36: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 36/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Enhanced XML Document

<?xml version="1.0"?>

<FitnessCenter>

<Member id="1" level="platinum">

<Name>Jeff </ Name>

<Phone type="home">555-1234</ Phone>

<Phone type="work">555-4321</ Phone>

<FavoriteColor>lightgrey</ FavoriteColor>

</ Member>

<Member id="2" level="gold">

<Name>David</ Name>

<Phone type="home">383-1234</ Phone><Phone type="work">383-4321</ Phone>

<FavoriteColor>lightblue</ FavoriteColor>

</ Member>

<Member id="3" level="platinum">

<Name>Roger</ Name><Phone type="home">888-1234</ Phone>

<Phone type="work">888-4321</ Phone>

<FavoriteColor>lightyellow</ FavoriteColor>

</ Member>

</ FitnessCenter>

Note that each Member

now has a unique id

(the id attribute)

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Review - HTML Hyperlinking

Page 37: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 37/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Review HTML Hyperlinking

<A name="AnnaAndTheKing"></A>

<A href="#AnnaAndTheKing">Click Here</A>

...

This creates an internal hyperlink (the source "anchor" links to

the target anchor).

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Hyperlink Name to Home Phone

Page 38: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 38/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

yp

Problem: create an HTML document that has two tables -a Member Name table, and a Member home Phonenumber table.

Hyperlink the Member's Name to his/her Phone.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

<TABLE border "1" width "25%">

Page 39: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 39/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<TABLE border="1" width="25%">

<TR><TH>Name</TH></TR>

<xsl:for-each select="/FitnessCenter/Member">

<TR>

<TD>

<A href="#{@id}">

<xsl:value-of select="Name"/></A>

</TD>

</TR>

</xsl:for-each>

</TABLE>

<BR/><BR/><BR/><BR/><BR/><TABLE border="1" width="25%">

<TR><TH>Home Phone Number</TH></TR>

<xsl:for-each select="/FitnessCenter/Member">

<TR>

<TD><A name="{@id}"><xsl:value-of select="Phone[@type='home']"/>

</A></TD>

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

</TABLE>

(see html-example08)

Do Lab1,

Parts 5-6

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Numbering

Page 40: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 40/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Numbering

There is an XSL element that returns a numbercorresponding to the element's position in the set ofselected nodes

(see html-example09)

<xsl:for-each select="/FitnessCenter/Member">

<xsl:number value="position()" format="1"/><xsl:text>. </xsl:text>

<xsl:value-of select="Name"/>

<BR/>

</xsl:for-each> Output:1. Jeff 

2. David

3. Roger

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Start Numbering from 0

Page 41: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 41/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

g

How would you start the numbering from zero, rather thanone?

<xsl:number value="position() - 1" format="1">

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

format attribute of xsl:number

Page 42: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 42/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

In the previous example we saw how to generatenumbers, and we saw that the generated numbers were 1,2, 3, etc.

With the format attribute we can specify the format of thegenerated number, i.e., 1, 2, 3 or I, II, III, or A, B, C, or …

• format=“1” generates the sequence: 1, 2, 3, …

• format=“01” generates: 01, 02, 03, …

• format=“A” generates: A, B, C, …

• format=“a” generates: a, b, c, …

• format=“I” generates: I, II, III, …

• format=“i” generates: i, ii, iii, ...

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

format attribute of xsl:number

Page 43: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 43/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<xsl:for-each select="/FitnessCenter/Member">

<xsl:number value="position()" format="A"/><xsl:text>. </xsl:text>

<xsl:value-of select="Name"/>

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

Output:A. Jeff 

B. David

C. Roger

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Sorting

Page 44: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 44/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Sorting

There is an XSL element that sorts the elements thatyou extract from the XML document

<xsl:for-each select="/FitnessCenter/Member">

<xsl:sort select="Name" order="ascending"/><xsl:value-of select="Name"/>

<BR/>

</xsl:for-each>

"For each Member, sort the Name elements"

Output:

DavidJeff 

Roger

(see html-example10)

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Sorting

Page 45: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 45/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<xsl:for-each select="/FitnessCenter/Member">

<xsl:sort select="Name" order="ascending"/><xsl:value-of select="Name"/>

<BR/>

</xsl:for-each>

The set of Member elements selected by xsl:for-each is

sorted using the Name child element. This occurs prior

to the first iteration of the loop. After the set of Memberelements are sorted then the looping begins.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

concat() function

Page 46: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 46/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

concat(destination string , string to add )

Note: if you want to concatenate more than one string tothe destination string then simply add more arguments

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Page 47: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 47/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<xsl:for-each select="/FitnessCenter/Member">

<xsl:value-of select="concat('Welcome ', Name, '!')"/>

<BR/>

</xsl:for-each>

Output:

Welcome Jeff!

Welcome David!

Welcome Roger!

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

xsl:variable

Page 48: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 48/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Hello World

This XSL element allows you to create a variable to hold a value (which could

be a string or a subtree of the XML document).

The variable is referenced by $variable-name 

<xsl:variable name=“hello” select=“'Hello World'”/>

This creates a variable called hello, that has a value which is the literal string, ‘Hello World’.

We could use this variable as follows:

Value = <xsl:value-of select=“$hello”/>

This will output:

Value = Hello World

hello

<? xml 

version=“1.0”> <material> XML Lectures Notes 

<section id=“ 06 

”> XSLT 

</section> </material> 

Page 49: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 49/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Member's Phone Numbers:<TABLE border="1" width="25%">

<TR><TH>Name</TH><TH>Type</TH><TH>Number</TH></TR>

<xsl:for-each select="/FitnessCenter/Member">

<xsl:variable name="name" select="Name"/><xsl:for-each select="Phone">

<TR>

<TD><xsl:value-of select="$name"/></TD>

<TD><xsl:value-of select="@type"/></TD>

<TD><xsl:value-of select="."/></TD></TR>

</xsl:for-each>

</xsl:for-each>

</TABLE>

(see html-example12)

<? xml 

version=“1.0”> <material> XML Lectures Notes 

<section id=“ 06 

”> XSLT 

</section> </material> 

xsl:variable

Page 50: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 50/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<xsl:variable name=“member” select=“Member[1]”/>

This creates a variable called member, that has a value which is a subtree.

We could use this variable as follows:

Name = <xsl:value-of select=“$member/Name”/>

Home Phone = <xsl:value-of select=“$member/Phone[@type='home']”/>

This will result in generating:

Name = Jeff 

Home Phone = 555-1234

Member

...Name Phone Phone

Jeff  555-1234 555-4321

<? xml 

version=“1.0”> <material> XML Lectures Notes 

<section id=“ 06 

”> XSLT 

</section> </material> 

xsl:variable

Page 51: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 51/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

A variable is “write once, read many”.

• That is, you can assign a variable a value only once, but then youcan retrieve the value of the variable many times.

A variable has a scope limited to the XSL element that it isnested within. Its scope starts where it is defined andextends to the end of the XSL element that it is nestedwithin.

<? xml 

version=“1.0”> <material> XML Lectures Notes 

<section id=“ 06 

”> XSLT 

</section> </material> 

Page 52: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 52/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Member's Phone Numbers:<TABLE border="1" width="25%">

<TR><TD>Name</TD><TD>Type</TD><TD>Number</TD></TR>

<xsl:for-each select="/FitnessCenter/Member">

<xsl:variable name="name" select="Name"/><xsl:for-each select="Phone">

<TR>

<TD><xsl:value-of select="$name"/></TD>

<TD><xsl:value-of select="@type"/></TD>

<TD><xsl:value-of select="."/></TD></TR>

</xsl:for-each>

</xsl:for-each>

</TABLE>

The name variable's life ends hereDo Lab2,

Part 1

<? xml 

version=“1.0”> <material> XML Lectures Notes 

<section id=“ 06 

”> XSLT 

</section> </material> 

Global Variables

Page 53: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 53/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

You can create a variable outside of <xsl:template match="/">. Then,

the variable is global.

<?xml version="1.0"?>

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

version="1.0">

<xsl:output method="html"/>

<xsl:variable name="pi" select="'3.142857142857'"/>

<xsl:template match="/" >

<HTML>

<HEAD>

<TITLE>Value of Pi</TITLE>

</HEAD>

<BODY>

The value of pi = <xsl:value-of select="$pi"/></BODY>

</HTML>

</xsl:template>

<? xml 

version=“1.0”> <material> XML Lectures Notes 

<section id=“ 06 

”> XSLT 

</section> </material> 

Problem

Page 54: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 54/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Suppose that we want to create a variable, names, and we want this

variable to contain a list of the Member Names, with each nameseparated by a slash. How would you create such a variable?

Here’s what you might attempt to do:

Member's Names:

<xsl:variable name="names" select="/FitnessCenter/Member[1]/Name"/>

<xsl:for-each select="/FitnessCenter/Member[position() &gt; 1]">

<xsl:variable name="names" select="concat($names, '/')"/><xsl:variable name="names" select="concat($names, Name)"/>

</xsl:for-each>

<xsl:value-of select="$names"/>

Output:

Jeff 

(see html-example13)

<? xml 

version=“1.0”> <material> XML Lectures Notes 

<section id=“ 06 

”> XSLT 

</section> </material> 

Let’s add some statements to trace this example

Page 55: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 55/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<xsl:variable name="names" select="/FitnessCenter/Member[1]/Name"/>

<xsl:value-of select="$names"/><BR/>

<xsl:for-each select="/FitnessCenter/Member[position() &gt; 1]">

<xsl:variable name="names" select="concat($names, '/')"/>

<xsl:value-of select="$names"/><BR/>

<xsl:variable name="names" select="concat($names, Name)"/>

<xsl:value-of select="$names"/><BR/>

</xsl:for-each><xsl:value-of select="$names"/>

Output:

Jeff Jeff/ 

Jeff/David

Jeff/ <--- Why did we loose the previous Name? That name went out of scope.

Jeff/Roger

Jeff Obviously, this approach doesn’t work. So how do we do it?

(see html-example14)

<? xml 

version=“1.0”> <material> XML Lectures Notes 

<section id=“ 06 

”> XSLT 

</section> </material> 

Here’s what we would like to do

Page 56: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 56/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

names

Open up the

names box

<xsl:for-each select="/FitnessCenter/Member">

Add this iteration’s Name and a slash to the open names box

</xsl:for-each>

Iterate through

each name, adding into

the open box

Jeff/David/RogerClose the box

names

Jeff … / … David … / … Roger

<? xml 

version=“1.0”> <material> XML Lectures Notes 

<section id=“ 06 

”> XSLT 

</section> </material> 

Problem - Solution

Page 57: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 57/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

In all previous examples of creating a variable we declared the name of the

variable and then had a select attribute which gave the variable its value.

We can omit the select attribute:

<xsl:variable name=“names”>

- Do stuff in here. All output will go into the names “box”.

</xsl:variable>

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Problem - Solution

Page 58: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 58/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Member's Names:

<xsl:variable name="names"><xsl:value-of select="/FitnessCenter/Member[1]/Name"/>

<xsl:for-each select="/FitnessCenter/Member[position() &gt; 1]">

<xsl:text>/</xsl:text>

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

</xsl:variable><xsl:value-of select="$names"/>

(see html-example15)

Output:

Member's Names: Jeff/David/Roger

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Page 59: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 59/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

contains() function

contains(string to be tested, test string) returns true if

string to be tested  contains test string 

<xsl:if test=“contains($greeting, ‘welcome’)”>

$greeting contains ‘welcome’

</xsl:if>

Do Lab2,

Part 2

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

xsl:choose

Page 60: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 60/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

xsl:choose allows you to elegantly express multipleconditional tests. Here’s the structure:

<xsl:choose>

<xsl:when test='something>[action]

</xsl:when>

<xsl:when test='something'>

[action]

</xsl:when><xsl:otherwise>

[action]

</xsl:otherwise>

</xsl:choose>

The first xsl:when statement that

evaluates to true is executed. If 

none evaluates to true then the

xsl:otherwise statement is

executed.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Page 61: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 61/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Implementing an if-then-else

There is no if-then-else element in XSL.

However, there is an elegant way to do it:

<xsl:choose>

<xsl:when test="contains($member-list, 'Jeff')"><xsl:text>Jeff is a member</xsl:text>

</xsl:when>

<xsl:otherwise>

<xsl:text>No member by the name Jeff</xsl:text>

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

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

substring-before() String Function

Page 62: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 62/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Here’s the form of this string function:

substring-before(string, pattern)

Example:

<xsl:variable name="phone" select="Phone"/>

<xsl:value-of select="substring-before($phone, '-')"/>

“Get the contents of Phone and put it into the variable

called ‘phone’. Then extract from the content of ‘phone’ the string

before the '-' (i.e., the telephone exchange)”.

555-1234substring-before($phone, ‘-’)

555

phone

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

substring-after() String Function

Page 63: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 63/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Here’s the form of this string function:

substring-after(string, pattern)

Example:

<xsl:variable name="phone" select="Phone"/>

<xsl:value-of select="substring-after($phone, '-')"/>

“Get the contents of Phone and put it into the variable

called ‘phone’. Then extract from the content of ‘phone’ the string

after the '-'”.

555-1234substring-after($phone, ‘-’)

1234

phone

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

starts-with() String Function

Page 64: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 64/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Here’s the form of this string function:

starts-with(string, pattern)

Example:

<xsl:if test="starts-with(Phone, '555')">

[action]

</xsl:if>

“If the Phone starts with the

string, ‘555’ then do [action]”.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

substring() function

substring(string i len?) returns the substring of string that

Page 65: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 65/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

substring(string , i , len? ) returns the substring of string that

starts at the i th position and has length, len . The lengthargument (len ) is optional. If not present then this functionreturns the substring starting at the i th position all the way

to the end of the string. Note: the first character is atposition 1 (not 0 as with some languages)

substring(‘1234567890’, 2, 5) returns ‘23456’

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

string-length() function

string length(string) returns the length of the string

Page 66: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 66/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

string-length(string ) returns the length of the string 

string-length(‘1234567890’) returns 10

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

translate() function

Page 67: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 67/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

translate(string, from-pattern, to-pattern)

Example. translate(“Hello”, “ABCDEFGHIJKLMNOPQRSTUVWXYZ”,

“abcdefghijklmnopqrstuvwxyz”);

this will convert Hello to hello (i.e., convert to lower case)

A better approach to the above problem is:

<xsl:variable name="upperCaseChars" select=" 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' "/>

<xsl:variable name="lowerCaseChars" select=" 'abcdefghijklmnopqrstuvwxyz' "/>

translate(“Hello”, $upperCaseChars, $lowerCaseChars)

Note: need to put the stringwithin (single) quotes, otherwise

the XSL Processor will try to

interpret it as an XML element.

Do Lab2,

Part 3

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Boolean and Relational Operators

Boolean operators: not and or

Page 68: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 68/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Boolean operators: not, and, or

Relational operators: <, >, =, <=, >=, !=

The less than and greater than signs are reserved

symbols, so they need to be escaped when you use them.Thus, the relational operators will appear in your XSL codelike this:

< > = <= >= !=

&lt; &gt; = &lt;= &gt;= !=

Want this:

Use this:

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Arithmetic

The arithmetic operators available: + - * div mod

Page 69: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 69/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

The arithmetic operators available: +, -, , div, mod

(remainder from doing a division)

• Note: recall that an XML element can have a dash in the name.So, if you want to indicate subtraction, be sure to surround “-” with

blank spaces.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Arithmetic functions

Page 70: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 70/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

sum(node set ) this function sums up all the values in the set of nodes

floor(number ) returns the largest integer that is not greater than number • Example. floor(2.5) returns 2

ceiling(number ) returns the smallest integer that is not less thannumber 

• Example. Ceiling(2.5) returns 3

round(number ) returns the integer closest to number 

• Example. round(2.3) returns 2

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Enhanced XML Document

Page 71: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 71/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<?xml version="1.0"?>

<FitnessCenter>

<Member id="1" level="platinum">

<Name>Jeff </ Name>

<Phone type="home">555-1234</ Phone>

<Phone type="work">555-4321</ Phone><FavoriteColor>lightgrey</ FavoriteColor>

<MembershipFee>340</ MembershipFee>

</ Member>

<Member id="2" level="gold">

<Name>David</ Name>

<Phone type="home">383-1234</ Phone><Phone type="work">383-4321</ Phone>

<FavoriteColor>lightblue</ FavoriteColor>

<MembershipFee>500</ MembershipFee>

</ Member>

<Member id="3" level="platinum">

<Name>Roger</ Name>

<Phone type="home">888-1234</ Phone>

<Phone type="work">888-4321</ Phone>

<FavoriteColor>lightyellow</ FavoriteColor>

<MembershipFee>340</ MembershipFee>

</ Member></ FitnessCenter>

Note that each Member

now has MembershipFee

element

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Compute Membership Revenue

Page 72: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 72/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Membership Fee Revenue:

<xsl:value-of select="sum(//MembershipFee)"/>

(see html-example16)

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

xsl:attribute

Page 73: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 73/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

xsl:attribute

This XSL element is used by nesting it within an

output element. It enables you to create an attributefor the output element

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Coloring alternate rows

Page 74: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 74/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Member Names:

<TABLE border="1" width="25%">

<xsl:for-each select="/FitnessCenter/Member"><TR>

<xsl:if test="position() mod 2 = 0"><xsl:attribute name="bgcolor">yellow</xsl:attribute>

</xsl:if><TD><xsl:value-of select="Name"/></TD>

</TR>

</xsl:for-each>

</TABLE>

(see html-example17)

For each even row of the table, the TR value will be:

<TR bgcolor="yellow">

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

count() function

Page 75: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 75/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

count(set of node) returns an integer representing the number

of nodes (i.e., XML elements) in the set.

Example.

Number of members =

<xsl:value-of select="count(//Member)"/>

Output:

Number of members = 5 Do Lab2,

Part 4

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Selecting all Elements/Attributes

Page 76: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 76/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<xsl:for-each select="/FitnessCenter/Member">

<xsl:for-each select="@*">...

</xsl:for-each>

<xsl:for-each select="*">

...

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

For eachattribute do ...

For each

child element

do ...

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Getting the Name of the Element/Attributeusing the name() Function

Page 77: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 77/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<xsl:for-each select="/FitnessCenter/Member"><xsl:for-each select="@*">

Attribute = <xsl:value-of select="name(.)"/>

</xsl:for-each>

<xsl:for-each select="*">

Element = <xsl:value-of select="name(.)"/>

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

(see html-example19)

name(node) returns the name of "node"

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

When to use Curly Braces?

Page 78: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 78/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

“When I assign an attribute a value, when do I use curly braces and when do Inot use them?”

Use curly braces for these attributes:

- the attribute of a literal result element (where you literally type what shouldbe output)

Example: <a href=“#{@id}”>

- the name attribute of xsl:attribute

Example: <xsl:attribute name =“{@value}”>

- the name attribute of xsl:piExample: <xsl:pi name =“{@value}”>

- the name attribute of xsl:element

Example: <xsl:element name =“{@value}”>

- the optional attributes of xsl:sort:Example: <xsl:sort order =“{@value}”>

lang =“{@value}”>

data-type =“{@value}”>

case-order =“{@value}”>

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

document( ) Function

This function enables you to access other XML documents

Page 79: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 79/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

(besides the XML document that you specify when youinvoke the XSL Processor).

The format for using the document() function is:document(url ), where url is a URL to another XMLdocument

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Fitness Centers Merger

Another fitness center has just merged with us. They have

Page 80: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 80/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

an xml document (FitnessCenter2.xml) containing theirMembers.

You are to create an XSL-enhanced HTML document thatcreates a single table comprised of all the Members fromboth fitness clubs.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

<TABLE border="1" width="75%">

<TR><TH>Name</TH><TH>Phone(home)</TH>...</TR>

<xsl:for-each select="/FitnessCenter/Member">

Page 81: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 81/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<TR><TD><xsl:value-of select="Name"/></TD>

<TD><xsl:value-of select="Phone[@type='home']"/></TD>

<TD><xsl:value-of select="Phone[@type='work']"/></TD>

<TD><xsl:value-of select="FavoriteColor"/></TD>

</TR>

</xsl:for-each>

<xsl:variable name="fitnessCenter2"select="document('file://localhost/xml-course/.../FitnessCenter2.xml')"/>

<xsl:for-each select="$fitnessCenter2/FitnessCenter/Member"><TR>

<TD><xsl:value-of select="Name"/></TD>

<TD><xsl:value-of select="Phone[@type='home']"/></TD>

<TD><xsl:value-of select="Phone[@type='work']"/></TD><TD><xsl:value-of select="FavoriteColor"/></TD>

</TR>

</xsl:for-each>

</TABLE>

(see html-example20)

Do Lab3,

Part 1

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Parameterized Processing

You can create a subroutine (called a named template),

Page 82: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 82/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

and you can pass to it parameters.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

<xsl:template match="/">

<HTML>

<HEAD>

<TITLE>Fitness Center</TITLE>

Page 83: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 83/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

</HEAD><BODY>

<xsl:call-template name="displayNameWithFont">

<xsl:with-param name="fontFace" select="'Impact'"/><xsl:with-param name="name"

select="/FitnessCenter/Member[1]/Name"/></xsl:call-template>

<BR/>

...

</BODY>

</HTML>

</xsl:template>

<xsl:template name="displayNameWithFont"><xsl:param name="fontFace" select="'Braggadocio'"/> <!-- default font --><xsl:param name="name"/><FONT face="{$fontFace}">

<xsl:value-of select="$name"/></FONT>

</xsl:template>(see html-example21)

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Call by Reference

How do we create a named template that returns a value?

Page 84: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 84/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Example: create a named template which, when passed anumber, it returns the number div 2.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

<xsl:template match="/">

<HTML>

<HEAD>

Page 85: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 85/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<TITLE>Fitness Center</TITLE></HEAD>

<BODY>

16 / 2 =

<xsl:variable name="result"><xsl:call-template name="NumDiv2"><xsl:with-param name="N" select="16"/>

</xsl:call-template></xsl:variable>

<xsl:value-of select="$result"/></BODY>

</HTML>

</xsl:template>

<xsl:template name="NumDiv2">

<xsl:param name="N"/>

<xsl:value-of select="$N div 2"/>

</xsl:template>(see html-example22)

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Problem: Determine if all <Number> valuesare <= 100

Page 86: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 86/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

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

<Number>23</Number>

<Number>41</Number>

<Number>70</Number><Number>103</Number>

<Number>99</Number>

<Number>6</Number></NumberList>

(see html-example22-1)

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

<xsl:template match="/">

<xsl:text>All numbers in the list are less than or equal to 100: </xsl:text>

<xsl:variable name="result">

<xsl:call-template name="AllLessThan100">

<xsl:with-param name="numberList" select="NumberList/Number"/>

/ l ll l

Pass to the named

template a list of 

b d

Page 87: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 87/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

</xsl:call-template></xsl:variable>

<xsl:value-of select="$result"/>

</xsl:template>

<xsl:template name="AllLessThan100">

<xsl:param name="numberList"/>

<xsl:choose><xsl:when test="not($numberList)">

<xsl:text>true</xsl:text>

</xsl:when>

<xsl:otherwise>

<xsl:choose>

<xsl:when test="$numberList[1] &gt; 100">

<xsl:text>false</xsl:text>

</xsl:when>

<xsl:otherwise>

<xsl:call-template name="AllLessThan100">

<xsl:with-param name="numberList" select="$numberList[position() &gt; 1]"/>

</xsl:call-template>

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

</xsl:otherwise>

</xsl:choose>

</xsl:template>

Do Lab3,Part 2

<Number> nodes,i.e., a nodelist.

This is a recursive routine

If the nodelist is empty

then return true

Check the first node onthe list. If it's greater

than 100, then return

false (and we're done).

Otherwise, recurse over theremaining nodes.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

generate-id()

Page 88: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 88/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Use this function to generate a unique string for a

node

Example. generate-id(/FitnessCenter/Member[1]) will return a unique id for the first Member

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Using generate-id() to Uniquely IdentifyElements In html-example08 we created two tables - a table

t i i th M b N d t t bl

Page 89: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 89/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

containing the Members Names, and a separate tablecontaining home Phone numbers. Each Name washyperlinked to his/her home Phone. We used the id

attribute on each Member element to link the two tablestogether.

Suppose there is no id attribute. We can use generate-id()

to create a unique identifier.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

<TABLE border="1" width="25%">

<TR><TH>Name</TH></TR>

<xsl:for-each select="/FitnessCenter/Member">

<TR>

Page 90: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 90/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<TR><TD>

<A href="#{generate-id(.)}">

<xsl:value-of select="Name"/></A>

</TD></TR>

</xsl:for-each>

</TABLE>

<BR/><BR/><BR/><BR/><BR/>

<TABLE border="1" width="25%"><TR><TH>Home Phone Number</TH></TR>

<xsl:for-each select="/FitnessCenter/Member">

<TR>

<TD>

<A name="{generate-id(.)}"><xsl:value-of select="Phone[@type='home']"/>

</A></TD>

</TR>

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

(see html-example23)

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Same (XML) Data, Multiple Views

In html-example24 I show how to create an HTML

d m t th t ll li t t i XML d m t i

Page 91: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 91/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

document that allows a client to view an XML document indifferent forms.

Look at html-example24. With the buttons on the left side

of the screen we can select which view is desired.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Multiple Stylesheets to provide the different views

Page 92: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 92/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

FitnessCenter.xml

ShowMembers.xsl ShowPlatinumMembers.xsl ShowGoldMembers.xsl ShowAll.xsl

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

FitnessCenter.html - comprised of two frames

Page 93: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 93/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

FitnessCenter.html

Controls.html Body.html

ShowMembers

ShowPlatinumMembers

ShowGoldMembers

ShowAll

RawXML

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

FitnessCenter.html

Page 94: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 94/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<HTML>

<HEAD><TITLE>Welcome</TITLE>

</HEAD>

<FRAMESET cols="20%,80%">

<FRAME src="controls.html" name="controlsFrame"><FRAME src="body.html" name="bodyFrame">

</FRAMESET>

</HTML>

FitnessCenter.html

Two columns.

The first columnis 20% of the width

of the screen. The

second column is

80% of the width

of the screen.

NOTE: do NOT have a <BODY> element (it won't work if you do)

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Body.html

<HTML>

Page 95: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 95/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<HTML><HEAD>

<TITLE>Fitness Center</TITLE>

</HEAD><BODY>

</BODY>

</HTML>Body.html

This document is very simple - it's empty!The body will be filled in with the HTML that

is generated by styling the XML document

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Controls.html(tracing through the actions that occur when a user selects "ShowGoldMembers")

Page 96: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 96/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Press Show Gold Members button

Load ShowGoldMembers.xsl

Invoke the Javascript function, ShowGoldMembers()

Transform (the previously loaded) FitnessCenter.xml

using ShowGoldMembers.xsl

Set the body of Body.html to the generated html

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Controls.html(code to ShowGoldMembers)

<FORM name="controlForm"><INPUT type "button" value "Show Gold Members" onclick "ShowGoldMembers

Page 97: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 97/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<INPUT type= button value= Show Gold Members onclick= ShowGoldMembers

</FORM>

<SCRIPT language="JScript" defer="true">var xml = new ActiveXObject("Msxml2.DOMDocument.3.0");

xml.async = false;

xml.load("FitnessCenter.xml");

function ShowGoldMembers()

{

var xsl = new ActiveXObject("Msxml2.DOMDocument.3.0");

xsl.async = false;

xsl.load("ShowGoldMembers.xsl");

parent.bodyFrame.document.body.innerHTML = xml.transformNode(xsl);

}

</SCRIPT>

This is preloading

the XML document

Load the XSL

document

Transform the XML

document using the

stylesheet, and assign

the body of Body.html

to the generated html.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Using SAXON in a Browser(rather the XSL Processor built into IE)

The last example utilized the XSL Processor built into IE (msxml) to do the

transformations.

Page 98: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 98/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

transformations. Suppose the you would like to use a different XSL Processor, e.g., SAXON?

• Why would you want to do this? Answer: SAXON has several capabilities thatmsxml does not have.

SAXON comes with a Java applet that you can use to do XSL processingwithin a browser.

See html-example-24-a for details on how to use SAXON's Java applet to

implement the multi-button example. See html-example-24-b for an example of how to use SAXON as the XSL

Processor within a browser.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Inserting spaces into HTML output

Page 99: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 99/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<xsl:text disable-output-escaping="yes"><![CDATA[&#160;&#160;&#160;&#160;]]></xsl:text>

Put one character

references for each

space required

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Debugging your Stylesheets usingxsl:message xsl:message is used to display a message, and

(optionally) terminate execution of the stylesheet

Page 100: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 100/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

(optionally) terminate execution of the stylesheet.

The message is sent to the screen, not to the output file.

This provides a very nice way to monitor the flow of yourstylesheet, without impacting the output file.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Example using xsl:message

Page 101: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 101/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<?xml version="1.0"?>

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

version="1.0">

<xsl:output method="html"/>

<xsl:template match="/">

<HTML>

<BODY>

<xsl:for-each select="FitnessCenter/Member">

<xsl:if test="MembershipFee &lt; 0">

<xsl:message terminate="yes"><xsl:text>Invalid MembershipFee</xsl:text>

</xsl:message>

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

<xsl:text>All the MembershipFee elements are valid</xsl:text>

</BODY>

</HTML>

</xsl:template>

</xsl:stylesheet>

Two possible values for

terminate - yes, or no.

terminate="yes" means that yo

want the message output to thescreen and then the program

stopped.

terminate="no" means that you

want the message output to the

screen and the program to

continue executing.

See html-example26

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

system-property() function

The system-property(property name ) function enables you

to obtain information about the XSL Processor that you are

Page 102: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 102/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

to obtain information about the XSL Processor that you areusing:

• xsl:vendor - if you specify this as the value for property name then

it will return the name of the XSL Processor vendor

• xsl:vendor - this provides the URL to the vendor's web site

• xsl:version - this indicates what version of the XSL spec is

implemented.

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Recommended Practice

Page 103: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 103/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<?xml version="1.0"?>

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

version="1.0">

<xsl:output method="html"/>

<xsl:template match="/">

<xsl:message>XSLT Processor: <xsl:value-of select="system-property('xsl:vendor')"/>

</xsl:message><HTML>

<HEAD><TITLE>Welcome</TITLE>

</HEAD>

<BODY>

Welcome!

</BODY>

</HTML></xsl:template>

</xsl:stylesheet>

It is very good practice to add this to the

start of every stylesheet. This will give

you a message indicating which XSL

Processor you are using.

When might you be uncertain which XSL

Processor is being used? Java comes

bundled with xalan. Suppose that you

want to write a Java program whichuses Saxon instead. You will definitely

want to use the above to ensure that you

are invoking Saxon and not the built-in

xalan. (I know. I have made the mistake

of thinking that I was using Saxon when

in fact I was using xalan.)

See html-example27

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Embedded Stylesheets

You can embed a stylesheet within an XML document.

Page 104: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 104/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<?xml version="1.0"?>

<!DOCTYPE FitnessCenter [

<!ATTLIST xsl:stylesheet id ID #REQUIRED>

]>

<?xml-stylesheet type="text/xml" href="#embed"?><FitnessCenter>

<Member level="platinum">

<Name>Jeff</Name>

<Phone type="home">555-1234</Phone>

<Phone type="work">555-4321</Phone>

<FavoriteColor>lightgrey</FavoriteColor>

</Member>

...<xsl:stylesheet id="embed"

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

version="1.0">

...

</xsl:stylesheet>

</FitnessCenter>

Stylesheet embedded within

the XML document

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Embedded Stylesheets

<?xml version="1 0"?>

Page 105: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 105/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

<?xml version= 1.0 ?>

<!DOCTYPE FitnessCenter [

<!ATTLIST xsl:stylesheet id ID #REQUIRED>]>

<?xml-stylesheet type="text/xml" href="#embed"?><FitnessCenter>

<Member level="platinum">

<Name>Jeff</Name>

<Phone type="home">555-1234</Phone>

<Phone type="work">555-4321</Phone>

<FavoriteColor>lightgrey</FavoriteColor></Member>

...

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

version="1.0">

...

</xsl:stylesheet>

</FitnessCenter>

Add an id attribute to the

xsl:stylesheet element.

The stylesheet PI referencesthe embeded stylesheet (as

indicated by the"#" sign)

You must indicate

that the id attribute

is of type ID.

See html-example28 (Note: not all XSL Processors support embedded stylesheets)

<? xml version=“1.0”> <material> XML Lectures Notes  <section id=“ 06 ”> XSLT </section> </material> 

Info

Course name:

Special Selected Topic in

Page 106: 06 - XML - XSLT

8/3/2019 06 - XML - XSLT

http://slidepdf.com/reader/full/06-xml-xslt 106/106

eXtensible Markup LanguageLectu rer : Phan Vo Minh Than g MSc.

Special Selected Topic in

Information System Section: XSLT

Number of slides: 106

Updated date: 12/02/2006

Contact: Mr.Phan Vo Minh Thang

([email protected])