92
opyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) (http://www.w3.org/TR/XSLT) Roger L. Costello copyright @1999

Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Embed Size (px)

Citation preview

Page 1: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 1

XSL by Example(Recommendation 16 November 1999)

(http://www.w3.org/TR/XSLT)

Roger L. Costello

copyright @1999

Page 2: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 2

Today - Focus on Generating HTML

HTML

XML(content)

XSL(presentation)

XSLProcessor

Page 3: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 3

Transformation Language

• XSL is a transformation language --> it transforms a document written in one language (XML) into a document of another language (e.g., HTML)

Transformation Engine(XSL Parser)

XSL

XML HTML

Page 4: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 4

Recall XML

• Recall from XML that an XML document is composed of ‘elements’. For a BookCatalogue XML document we have the following elements: BookCatalogue (the ‘root’ element), Book, Title, Author, Date, ISBN, and Publisher.

Page 5: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 5

XSL - all about (Template) “Rules”

• “Hey xsl processor, when you encounter the root element (e.g., BookCatalogue) do [action1]”

• “Hey xsl processor, when you encounter the Book element do [action2]”

• “Hey xsl processor, when you encounter the Title element do [action3]”

• And so forth

Page 6: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 6

XSL - all about (Template) “Rules”

• Each template rule has two parts:– A pattern or matching part, that identifies the

XML node in the source document to which the action part is to be applied. Matching information is contained in an attribute.

– An action or formatting, styling and processing part that details the transformation and styling of the resulting node

Page 7: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 7

XSL Document Structure

xsl:stylesheet

template rule for the document

(template rule for a child element)*

action on the document

action on the child element

Page 8: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 8

XSL Document Structure

<?xml version=“1.0”?><xsl:stylesheet> <xsl:template match=“/”> [action] </xsl:template> <xsl:template match=“BookCatalogue”> [action] </xsl:template> <xsl:template match=“Book”> [action] </xsl:template> ...</xsl:stylesheet>

Page 9: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 9

Things to Note

• XSL documents are full-fledged XML documents– As a consequence, if in [action] you want to, say,

output an HTML paragraph break you must have both <p> and </p>*, even though in an HTML document the closing element (</p>) is optional (recall that in XML all elements must have a closing element)

– The root element of all XSL documents is xsl:stylesheet• To indicate that a template rule is to be applied to the XML

document you use “/”**

* Alternatively, <p/>** Where does “/” come from? From Unix, where the root directory is designated by “/”.

Page 10: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 10

Template Rules

Template rules take the following general form:

<xsl:template match=“pattern”> [ action ]

</xsl:template>

Page 11: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 11

Template Rules (Example)

<xsl:template match=“Book”> <xsl:apply-templates/></xsl:template>

“Hey XSL processor, as you parse through the XML documentand you get to a <Book> element use this template rule.”

<xsl:template match=“Book”>

“Go to each of my children and apply the template rules to them.”<xsl:apply-templates/>

Page 12: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 12

XT

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

XT

XML XSL

HTML

Invoking from the command line:java -Dcom.jclark.xsl.sax.parser=com.ibm.xml.parser.SAXDriver com.jclark.xsl.sax.Driver BookCatalogue.xml BookCatalogue.xsl BookCatalogue.html

Page 13: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 13

Using XT

(1) Download both XT and XP (XML Parser) from James Clark’s Web site (http://www.jclark.com)

(2) Set the classpath:

set CLASSPATH=%CLASSPATH%;xt.jar;xp.jar;xml4j_1_1_16.jar

(3) Execute the Java XT program:

java -Dcom.jclark.xsl.sax.parser=com.ibm.xml.parser.SAXDriver com.jclark.xsl.sax.Driver xml-file xsl-file html-file

Page 14: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 14

Example - Create XSL for BookCatalogue.xml

<?xml version="1.0"?><!DOCTYPE BookCatalogue SYSTEM "file://localhost/xml-course/xsl/BookCatalogue.dtd"><BookCatalogue> <Book> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>July, 1998</Date> <ISBN>94303-12021-43892</ISBN> <Publisher>McMillin Publishing</Publisher> </Book> <Book> <Title>Illusions The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0-440-34319-4</ISBN> <Publisher>Dell Publishing Co.</Publisher> </Book> <Book> <Title>The First and Last Freedom</Title> <Author>J. Krishnamurti</Author> <Date>1954</Date> <ISBN>0-06-064831-7</ISBN> <Publisher>Harper &amp; Row</Publisher> </Book></BookCatalogue>

BookCatalogue.xml

Page 15: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 15

First Example

• This example will show how an XSL Processor parses your xml document and uses the template rules defined in your xsl document to do something for each thing that is found in the xml document

Page 16: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 16

TerminologyIn BookCatalogue.xml we have (snippet):

<BookCatalogue> <Book> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>July, 1998</Date> <ISBN>94303-12021-43892</ISBN> <Publisher>McMillin Publishing</Publisher> </Book> ...</BookCatalogue>

“Book is a child element of the BookCatalogue element. Title, Author,Date, ISBN, and Publisher are children elements of the Book element.”

Page 17: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 17

Creating a Stylesheet - Step 1

• Draw a tree diagram of your XML document

Page 18: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 18

Document/

PI<?xml version=“1.0”?>

DocumentType<!DOCTYPE BookCatalogue ...>

ElementBookCatalogue

ElementBook

ElementBook

ElementBook

ElementTitle

ElementAuthor

ElementDate

ElementISBN

ElementPublisher

... ...

TextMy Life ...

TextPaul McCartney

TextJuly, 1998

Text94303-12021-43892

TextMcMillin Publishing

Page 19: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 19

Creating a Stylesheet - Step 2

• Create a template rule for every type of node in your tree*– e.g., create one template rule for all the <Book>

nodes, not one template rule for each of the nodes.

* Except for the PI and DOCTYPE nodes. The spec does not allow you to have a template rule for the DOCTYPE node. We will look at creating template rules for PI nodes in a later slide.

Page 20: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 20<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <xsl:apply-templates/> </xsl:template> <xsl:template match="BookCatalogue"> <xsl:apply-templates/> </xsl:template> <xsl:template match="Book"> <xsl:apply-templates/> </xsl:template> <xsl:template match="Title"> <xsl:apply-templates/> </xsl:template> <xsl:template match="Author"> <xsl:apply-templates/> </xsl:template> <xsl:template match="Date"> <xsl:apply-templates/> </xsl:template> <xsl:template match="ISBN"> <xsl:apply-templates/> </xsl:template> <xsl:template match="Publisher"> <xsl:apply-templates/> </xsl:template> <xsl:template match="text()"> <xsl:value-of select="."/> </xsl:template></xsl:stylesheet>

Page 21: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 21

Explanation

“Hey XSL Processor, return the value of the thing that is selectedhere” In our example the “thing” that is selected is a text node, so the“thing” that is returned is the value of the text node, i.e., the text.

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

Page 22: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 22

Creating a Stylesheet - Step 3

• For those nodes in the tree that we want to immediately return (i.e., not process any of its children) – remove <xsl:apply-templates/>

– remove the template rules for the child nodes (those template rules will never be used, so why have them?)

• For our example, we want the XSL Processor to traverse the entire XML document tree so we won’t do this step.

Page 23: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 23

Creating a Stylesheet - Step 4

• Put an <HTML> wrapper around the content

Page 24: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 24

<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <HTML> <HEAD> <TITLE>Book Catalogue</TITLE> </HEAD> <BODY> <xsl:apply-templates/> </BODY> </HTML> </xsl:template> <xsl:template match="BookCatalogue"> <xsl:apply-templates/> </xsl:template> <xsl:template match="Book"> <xsl:apply-templates/> </xsl:template> <xsl:template match="Title"> <xsl:apply-templates/> </xsl:template> <xsl:template match="Author"> <xsl:apply-templates/> </xsl:template> <xsl:template match="Date"> <xsl:apply-templates/> </xsl:template> <xsl:template match="ISBN"> <xsl:apply-templates/> </xsl:template> <xsl:template match="Publisher"> <xsl:apply-templates/> </xsl:template> <xsl:template match="text()"> <xsl:value-of select="."/> </xsl:template></xsl:stylesheet>

BookCatalogue1a.xsl

added these

Page 25: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 25

Go Ahead and Run It

• Go ahead and run BookCatalogue1a.xsl through the XSL Processor

• Look at the generated HTML document (BookCatalogue1a.html)– Notice that all the data in the XML document

has been extracted• It ain’t real pretty, but we’ll work on styling the data

in succeeding examples. – We have, however, created our first stylesheet!

Page 26: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 26

Creating a Stylesheet - Step 5

• Tell the XSL Processor that when it encounters a <Book> element to output “I am at a Book element. Processing its children now.” Do this for each element:

Page 27: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 27

Hint

• Before running your XSL document through the XSL Processor, drop it into IE 5.0 to check for well-formedness. It will save you a lot of debugging grief! (Stated from this author’s own experience!)

Page 28: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 28<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <HTML> <HEAD> <TITLE>Book Catalogue</TITLE> </HEAD> <BODY> <xsl:apply-templates/> </BODY> </HTML> </xsl:template> <xsl:template match="BookCatalogue"> I am at BookCatalogue. Processing its children now.<br/> <xsl:apply-templates/> </xsl:template> <xsl:template match="Book"> I am at Book. Processing its children now.<br/> <xsl:apply-templates/> </xsl:template> <xsl:template match="Title"> I am at Title. Here's the title:<br/> <xsl:apply-templates/><br/> </xsl:template> <xsl:template match="Author"> I am at Author. Here's the author's name:<br/> <xsl:apply-templates/><br/> </xsl:template> <xsl:template match="Date"> I am at Date. Here's the date:<br/> <xsl:apply-templates/><br/> </xsl:template> <xsl:template match="ISBN"> I am at ISBN. Here's the ISBN:<br/> <xsl:apply-templates/><br/> </xsl:template> <xsl:template match="Publisher"> I am at Publisher. Here's the publisher:<br/> <xsl:apply-templates/><br/> </xsl:template> <xsl:template match="text()"> <xsl:value-of select="."/> </xsl:template></xsl:stylesheet>

BookCatalogue1b.xsl

Page 29: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 29

See how the XSL Processor works?

• Run BookCatalogue1b through the XSL Processor

• Look at the generated HTML– It you look at the tree diagram that we created

in step 1 while looking at the HTML results, this should give you a real good idea of how the XSL Processor traverses the tree using your template rules

Page 30: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 30

Default Template Rules

• Every xsl document has two default template rules

• These rules are applied when the XSL Processor cannot find a template rule to use from what you wrote

• Here are the two default template rules:

<xsl:template match=“/ | *”><xsl:apply-templates/>

</xsl:template>

<xsl:template match=“text()”><xsl:value-of select=“.”/>

</xsl:template>

“Match on the document orany element. Returnresult of applying the templaterules to my children.”

“Match on a text node. Returnthe value of the text node, i.e., thetext.”

Page 31: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 31

Multiple Applicable Rules

Suppose that the XSL Processor is processing BookCatalogueand it gets to the <Book> element. Why does it use: <xsl:template match=“Book”>...and not the default template rule: <xsl:template match=“/ | *”>...??? After all, both apply.

Answer: given two rules that apply, the more specific rule wins.--> Clearly, “*” is much more general than “Book”. “*” matches on any element. “Book” just matches

on the Book element.

Page 32: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 32

Example 2

• “Hey XSL Processor, when you encounter a book element output: ‘<p/>Here is a book<br/>’ and then the data in its children elements.”

Page 33: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 33

This is what we want the XSL Processor to send to BookCatalogue.html

<HTML><HEAD><TITLE>Book Catalogue</TITLE></HEAD><BODY>Here is a book: all the data from the first bookHere is a book: all the data from the second bookHere is a book all of the data from the third book</BODY></HTML>

Page 34: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 34

BookCatalogue2.xsl<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="BookCatalogue"> <HTML> <HEAD> <TITLE>Book Catalogue</TITLE> </HEAD> <BODY> <xsl:apply-templates/> </BODY> </HTML> </xsl:template> <xsl:template match="Book"> <p/>Here is a book:<br/> <xsl: apply-templates/> </xsl:template></xsl:stylesheet>

Page 35: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 35

Now Let’s Put the Data into an HTML Table<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="BookCatalogue”> <HTML> <HEAD> <TITLE>Book Catalogue</TITLE> </HEAD> <BODY> <TABLE BORDER="1" WIDTH="100%"> <xsl:apply-templates/> </TABLE> </BODY> </HTML> </xsl:template> <xsl:template match="Book"> <TR> <xsl:apply-templates/> </TR> </xsl:template> <xsl:template match="Title | Author | Date | ISBN | Publisher"> <TD> <xsl:apply-templates/> </TD> </xsl:template></xsl:stylesheet>

BookCatalogue3.xsl

Page 36: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 36

Single Rule Applied to Multiple Elements

• Notice in the last example that a single rule is applied to multiple elements

<xsl:template match="Title | Author | Date | ISBN | Publisher"> <TD> <xsl:apply-templates/> </TD> </xsl:template>

This template rule is to be used for the Titleelement, the Author element, the Date element, etc.

Page 37: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 37

Control How the XSL Processor Traverses the XML Tree?

• Thus far we haven’t had much control over how the XSL Processor processes the XML document

– Namely, it processes it in “document order”

• The first Book in the XML document is processed first, then the second book in the XML document is processed second, and so forth

• The Title is processed first, and then the Author, and then the Data, etc.

• How do we control the order in which things are done?

– Suppose that we want to output Author first and then Title?

Page 38: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 38

<xsl:apply-templates select=“pattern”>

• The xsl:apply-templates element (without an attribute) tells the XSL Processor to apply the template rules to all children (in document order)

• The xsl: apply-templates element can have an attribute that tells the XSL Processor to process only the child element that matches “pattern”. – This apply-templates rule enables us to specify the

order in which the children are processed

Page 39: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 39

Specify the order of the table contents<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="BookCatalogue"> <HTML> <HEAD> <TITLE>Book Catalogue</TITLE> </HEAD> <BODY> <TABLE BORDER="1" WIDTH="100%"> <xsl:apply-templates/> </TABLE> </BODY> </HTML> </xsl:template> <xsl:template match="Book"> <TR> <xsl:apply-templates select="Author"/> <xsl:apply-templates select="Title"/> <xsl:apply-templates select="Date"/> <xsl:apply-templates select="Publisher"/> <xsl:apply-templates select="ISBN"/> </TR> </xsl:template> <xsl:template match="Title | Author | Date | ISBN | Publisher"> <TD> <xsl:apply-templates/> </TD> </xsl:template></xsl:stylesheet>

BookCatalogue4.xsl

process the Author elementfirst (in previous examplesthe Title element was processed first)

Page 40: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 40

<xsl:for-each select=“pattern”>

<xsl:for-each select=“Book”>[action]

</xsl:for-each>

This says that for every Book element, do [action]

Page 41: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 41

For each Book do ...<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="BookCatalogue"> <HTML> <HEAD> <TITLE>Book Catalogue</TITLE> </HEAD> <BODY> <TABLE BORDER="1" WIDTH="100%"> <xsl:for-each select="Book"> <TR> <xsl:apply-templates/> </TR> </xsl:for-each> </TABLE> </BODY> </HTML> </xsl:template> <xsl:template match="Title | Author | Date | ISBN | Publisher"> <TD> <xsl:apply-templates/> </TD> </xsl:template></xsl:stylesheet>

BookCatalogue5.xsl

for each Book createa row and processthe Book’s children

Page 42: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 42

Patterns

• Thus far we have seen very simple patterns: we simply match against an element name. XSL provides a rich pattern matching capability.

• We’ve seen:

<xsl:template match=“/”> “When you start processing the document do ...”

<xsl:template match=“Book”> “When you get to a Bookelement do ...”

<xsl:template match=“Title | Author”> “When you get to eithera Title element or an Authorelement do ...”

matchdocument

matchby name

matchseveralnames

Page 43: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 43

Patterns<xsl:template match=“Book/Title”> “When you get to a Title

element that has a Bookelement as a parent do ...”

<xsl:template match=“BookCatalogue//Title”> “When you get to aTitle element that has aBookCatalogue as an ancestordo ...”

<xsl:template match=“Book/*”> “When you get to any elementthat is an immediate child of Book do ....”

<xsl:template match=“id(J.K)”> “When you get to an elementthat has an id attribute whose

value is “J.K.” do ...”

matchwithimmediateancestor

matchwithanancestor

wildcardmatch

matchby id

Page 44: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 44

Patterns (matching by attribute)<xsl:template match=“Book”> “When you get to a Book element

do ...”

<xsl:template match=“Book[@inStock]”> “When you get to aBook element that has anattribute called inStock do ...”

<xsl:template match=“Book[@inStock =‘true’]”> “When you get toa Book element that has anattribute called inStock and its valueis ‘true’ do ...”

<xsl:template match=“BookCatalogue/Book[@inStock=‘true’]”> “When you get to a Book element that has an attribute called inStock whose value is ‘true’ and the element has a parent called BookCatalogue do ...

Page 45: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 45

Patterns (matching by position)<xsl:template match=“Book[position() = 1]”> “When you get to the first Book

element do ...”

<xsl:template match=“Book[position() = last()]”> “When you get to the last Book element do ...”

<xsl:template match=“Book[not(last())]”> “Use this rule for all but the last Book element”

Page 46: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 46

Named Attribute Sets

• XSL allows you to create a set of attributes and assign a name to it.– What value is this? Reuse!

<xsl:attribute-set name=“title-attributes”> <xsl:attribute name=“size”>+1</xsl:attribute> <xsl:attribute name=“color”>red</xsl:attribute> <xsl:attribute name=“face”>Braggadocio</xsl:attribute></xsl:attribute-set>

Page 47: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 47<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:attribute-set name="title-attributes"> <xsl:attribute name=“size”>+1</xsl:attribute> <xsl:attribute name=“color>red</xsl:attribute> <xsl:attribute name=“face>Braggadocio</xsl:attribute> </xsl:attribute-set> <xsl:template match="BookCatalogue”> <HTML> <HEAD> <TITLE>Book Catalogue</TITLE> </HEAD> <BODY> <TABLE BORDER="1" WIDTH="100%"> <xsl:apply-templates/> </TABLE> </BODY> </HTML> </xsl:template> <xsl:template match="Book"> <TR> <xsl:apply-templates/> </TR> </xsl:template> <xsl:template match="Author | Date | ISBN | Publisher"> <TD> <xsl:apply-templates/> </TD> </xsl:template> <xsl:template match="Title"> <TD> <FONT xsl:use-attribute-sets="title-attributes"> <xsl:apply-templates/> </FONT> </TD> </xsl:template></xsl:stylesheet>

Here’s where we use thenamed attribute set. Thus, size=“+1”, color=“red”, andface= “Braggadocio” will become attributes of FONT.BookCatalogue6.xsl

Here’s wherewe define thenamed attributeset

Page 48: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 48

Numbering

• XSL has the capability to number your elements

<xsl:number/>.<xsl:apply-templates select="Title"/>

This will print out a number followed by a periodfollowed by the results of processing theTitle element. The number is based upon the positionof the element in the XML document. (Example: if the above is in a template rule for match=“Book” then xsl:numberwill produce 1 for the first Book, 2 for the second Book, etc)See xsl2.ppt for a much deeper discussion of this topic.

Page 49: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 49

Creating a Numbered Title List<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="BookCatalogue"> <HTML> <HEAD> <TITLE>Book Catalogue</TITLE> </HEAD> <BODY> <xsl:apply-templates/> </BODY> </HTML> </xsl:template> <xsl:template match="Book"> <xsl:number/>.<xsl:apply-templates select="Title"/><br/> </xsl:template> <xsl:template match="Title"> <xsl:apply-templates/> </xsl:template></xsl:stylesheet>

BookCatalogue7.xsl

Here’s wherewe are usingthe numbercapability

Page 50: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 50

Sorting

• Sorting is achieved by adding xsl:sort elements as children of xsl:apply-templates or xsl:for-each.

• The first xsl:sort child specifies the primary sort key, the second xsl:sort specifies the secondary sort key, and so on.

<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="BookCatalogue"> <xsl:apply-templates select=“Book”> <xsl:sort select=“Title” order="ascending"/> </xsl:apply-templates> </xsl:template> <xsl:template match="Book"> <xsl:apply-templates select="Title"/><br/> </xsl:template> <xsl:template match="Title"> <xsl:apply-templates/> </xsl:template></xsl:stylesheet>

Page 51: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 51

if test<xsl:template match="Book"> <!-- If this is the first "Book" element, print table header --> <xsl:if test="position() = 1"> <B><FONT COLOR="#0000FF">TO</FONT></B> <TR><TD><FONT COLOR="#0000FF">Title</FONT></TD></TR> </xsl:if> <TR><TD><xsl:apply-templates /></TD></TR></xsl:template>

<xsl:template match="Date"> <!-- If there is no month element, print a dash --> <xsl:if test="not(month)">-</xsl:if> <xsl:apply-templates /></xsl:template>

Page 52: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 52

Common Mistake

<xsl:template match="Book"> <!-- Set href equal to the value of Book’s id attribute --> <a href=“#<xsl:value-of select=‘(@id)’/>”></xsl:template>

<xsl:template match="Book"> <!-- Set href equal to the value of Book’s id attribute --> <a href=“#{@id}”></xsl:template>

Wrong

Correct

Attributes valuescannot contain“<” or “>”

Scenario: suppose that the element Book has an id attribute. We would like to create a hyperlink using the value of the id attribute as the value of the hyperlink’s href.

Page 53: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 53

Formatting Objects (fo)

• Formatting objects are a predefined set of macros for formatting.– e.g., use a list fo to format a list

• Note: when you use a fo you need to specify what kind of fo is desired - an HTML fo, or a paper fo, etc

Page 54: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 54

Awesome Potential of using fo’s!(The Ultimate Promise of XSL)

PDFfo plug-in

XSLProcessor

XSLDocument

(written using fo’s)

XMLDocument

PDF

RTFfo plug-in

Wordfo plug-in ...

To change the output just change the plug-in! Wow!!!

Page 55: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 55

Example - Specifying a Stylesheet within an XML Document

<?xml version="1.0"?><?xml-stylesheet type=“text/xsl” href=“file://localhost/xml-course/xsl/BookCatalogue.xsl”?><!DOCTYPE BookCatalogue SYSTEM "file://localhost/xml-course/xsl/BookCatalogue.dtd"><BookCatalogue> <Book> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>July, 1998</Date> <ISBN>94303-12021-43892</ISBN> <Publisher>McMillin Publishing</Publisher> </Book> <Book> <Title>Illusions The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0-440-34319-4</ISBN> <Publisher>Dell Publishing Co.</Publisher> </Book> <Book> <Title>The First and Last Freedom</Title> <Author>J. Krishnamurti</Author> <Date>1954</Date> <ISBN>0-06-064831-7</ISBN> <Publisher>Harper &amp; Row</Publisher> </Book></BookCatalogue>

BookCatalogue.xml

Note this!This PI indicateswhat XSLdocument to use.

Page 56: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 56

Which Stylesheet is Used?

• Suppose that:

– The XML document has an xml:stylesheet PI and,

– we specify a stylesheet on the command line (when invoking the XSL Processor)

• Which stylesheet will the XSL Processor use?

– Answer: the one on the command line

• Note: if you don’t specify a stylesheet on the command line it will look in the XML document for a stylesheet PI and use the stylesheet specified there

Page 57: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 57

Stylesheet PIThe old way for specifying in an XML document the stylesheet is:

<?xml:stylesheet type=“text/xsl” href=“URL”?>

The new way for specifying in an XML document the stylesheet is:

<?xml-stylesheet type=“text/xsl” href=“URL”?>

Note the difference, xml:stylesheet versus xml-stylesheet.

If the XSL processor that you are using uses an old XML Parser then you will need to usethe old method, i.e., xml:stylesheet.

Page 58: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 58

Template Rule for PIs

“Hey XSL Processor, as you parse through the XML documentand you get to any processing instruction (PI) use this template rule.”

<xsl:template match=“//processing-instruction()”>

Page 59: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 59

id() function

• This function is used for XML documents which use the ID datatype (recall that an attribute may be declared to be of type ID. Thus, all values of that attribute must be unique - the XSL Processor will enforce this)

<Book id=“P.M”> …</Book><Book id=“R.B.”> …</Book>

XML

id(“R.B.”) selects the element with the unique ID ‘R.B.’

Page 60: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 60

Creating Hyperlinks to Each IDREFS Attribute Value

Suppose that a DTD defines an element, manager, with an attribute, subordinates. subordinatesis of type IDREFS:

<!ELEMENT manager EMPTY><!ATTLIST manager subordinates IDREFS #REQUIRED>

Here’s an snippet of an XML document:

<manager subordinates=“smith johnson”/>

Problem: create an XSL stylesheet that generates an HTML hyperlink from the manager to each subordinate, i.e.,

Manager subordinates are: <br><a href=“#smith”>smith</a><br><a href=“#johnson”>johnson</a><br>

Page 61: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 61

Creating Hyperlinks to Each IDREFS Attribute Value

<xsl:template match="manager"> Manager subordinates are: <br/> <xsl:for-each select="id(@subordinates)"> <a href="#{@id}"><xsl:value-of select="@id"/></a><br/> </xsl:for-each> </xsl:template>

Here’s the solution:

id(@subordinates) in the for-each statement says to tokenize (using whitespace astoken delimiters) the value of the subordinates attribute. Then for each token findthe unique identifier in the XML document that has that token’s value.

Page 62: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 62

<?xml version="1.0"?><!DOCTYPE company [ <!ELEMENT company (manager, employee*)><!ELEMENT manager EMPTY><!ATTLIST manager subordinates IDREFS #REQUIRED><!ELEMENT employee (#PCDATA)><!ATTLIST employee id ID #REQUIRED>]><company> <manager subordinates="smith johnson"/> <employee id="smith">Joan Smith</employee> <employee id="johnson">Steve Johnson</employee></company>

Manager.xml

Page 63: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 63<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="company"> <HTML> <HEAD> <TITLE>Company</TITLE> </HEAD> <BODY> <xsl:apply-templates/> </BODY> </HTML> </xsl:template> <xsl:template match="manager"> Manager subordinates are: <br/> <xsl:for-each select="id(@subordinates)"> <a href="#{@id}"><xsl:value-of select="@id"/></a><br/> </xsl:for-each> </xsl:template> <xsl:template match="employee"> <a name="{@id}"/> <xsl:apply-templates/> <br/> </xsl:template></xsl:stylesheet>

Manager.xsl

Page 64: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 64

<?xml version="1.0"?><HTML> <HEAD> <TITLE> Company </TITLE> </HEAD> <BODY> Manager subordinates are:<br></br> <a href="#smith">smith</a><br></br> <a href="#johnson">johnson</a><br></br>

<a name="smith"></a> Joan Smith<br></br> <a name="johnson"></a> Steve Johnson<br></br> </BODY></HTML>

Manager.html

Page 65: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 65

Filtering Classified Data

• I have an XML document that contains sensitive material. I would like to filter out the Top-Secret parts before serving it up to the client. How do I do this?

Page 66: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 66

<?xml version="1.0"?><!DOCTYPE company_data [ <!ELEMENT company_data (data*)><!ELEMENT data (#PCDATA)><!ATTLIST data classification (unclassified | classified | secret | top-secret) #REQUIRED>]><company_data> <data classification="unclassified">item1</data> <data classification="top-secret">item2</data> <data classification="unclassified">item3</data> <data classification="secret">item4</data> <data classification="classified">item5</data> <data classification="top-secret">item6</data> <data classification="classified">item7</data></company_data>

Classified.xml

Page 67: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 67

<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="company_data"> <HTML> <HEAD> <TITLE>Company Data</TITLE> </HEAD> <BODY> <xsl:apply-templates/> </BODY> </HTML> </xsl:template> <xsl:template match="data[@classification='top-secret']"/> <xsl:template match="data"> <xsl:apply-templates/> <br/> </xsl:template></xsl:stylesheet>

Classified.xsl

This (empty) template rule will filter out anydata that is labeled top-secret.

Page 68: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 68

Sorting Data into Releasable for Public, Press, or Company-Confidential

• This is an extension of the last example

• Suppose that the Classification attribute can contain multiple values, e.g., Classification=“public press”– “This item can be released to both the public

and the press”.

• Write a stylesheet which sorts the data based upon its releasability

Page 69: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 69

<?xml version="1.0"?><!DOCTYPE company_data [ <!ELEMENT company_data (data*)><!ELEMENT data (#PCDATA)><!ATTLIST data classification CDATA #REQUIRED>]><company_data> <data classification="public press">item1</data> <data classification="company-confidential">item2</data> <data classification="public">item3</data> <data classification="press">item4</data> <data classification="public press">item5</data> <data classification="company-confidential">item6</data> <data classification="public press">item7</data></company_data>

ClassifiedVersion2.xml

Page 70: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 70

<xsl:template match="data"> <xsl:if test="contains(@classification, 'public')"> Release <xsl:value-of select="."/> to the public.<br/> </xsl:if> <xsl:if test="contains(@classification, 'press')"> Release <xsl:value-of select="."/> to the press.<br/> </xsl:if> <xsl:if test="contains(@classification, 'company-confidential')"> Do not release <xsl:value-of select="."/>! Company confidential.<br/> </xsl:if> </xsl:template>

ClassifiedVersion2.xsl (snippet)

Page 71: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 71

Select All Vendors Except Me

• Problem: I have an XML document (XML_Expo.xml) that contains a list of vendors and their url. I would like to print a list of the vendors and their url. However, I (Costello International, Inc.) am one of the vendors! How do I select all the vendors except myself?

• This problem involves filtering based upon element value, rather than attribute value

Page 72: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 72<?xml version="1.0"?><!DOCTYPE XML_Expo [ <!ELEMENT XML_Expo (vendor*)><!ELEMENT vendor (name, url)><!ELEMENT name (#PCDATA)><!ELEMENT url (#PCDATA)>]><XML_Expo> <vendor> <name>Microsoft</name> <url>http://www.microsoft.com/xml</url> </vendor> <vendor> <name>IBM</name> <url>http://www.alphaworks.ibm.com/formula/xml</url> </vendor> <vendor> <name>Robin Cover</name> <url>http://www.oasis-open.org/cover/xml.html</url> </vendor> <vendor> <name>Costello International, Inc.</name> <url>http://www.xfront.org</url> </vendor> <vendor> <name>SUN Microsystems, Inc.</name> <url>http://www.javasoft.com/xml</url> </vendor></XML_Expo>

XML_Expo.xml

Everything but these

Page 73: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 73

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

<xsl:variable name="my_company"> Costello International, Inc. </xsl:variable> <xsl:template match="XML_Expo"> <HTML> <HEAD> <TITLE>XML_Expo</TITLE> </HEAD> <BODY> <xsl:apply-templates select="vendor[not(name/text()=$my_company)]"/> </BODY> </HTML> </xsl:template> <xsl:template match="vendor"> <xsl:number value=“position()”/>.<xsl:apply-templates select="name"/> <br/> <xsl:apply-templates select="url"/> <br/> </xsl:template></xsl:stylesheet>

XML_Expo.xsl

“If the <name> element’scontent is not ‘CostelloInternational, Inc.’ thendo ...”

Page 74: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 74

Retrieving an XML Fragment

• Problem: how do I create an XSL document that extracts a fragment of the XML document?

• Note: the solution that follows should give you hints on how to use an XSL Processor as a search engine!

Page 75: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 75

<?xml version="1.0"?><!DOCTYPE PLAY [ <!ELEMENT PLAY (TITLE, PERSONAE)><!ELEMENT TITLE (#PCDATA)><!ELEMENT PERSONAE (PERSONA*)><!ELEMENT PERSONA (#PCDATA)>]><PLAY> <TITLE>All&apos;s Well That Ends Well</TITLE> <PERSONAE> <PERSONA>KING OF FRANCE</PERSONA> <PERSONA>DUKE OF FLORENCE</PERSONA> <PERSONA>BERTRAM, Count of Rousillon.</PERSONA> <PERSONA>LAFEU, an old lord.</PERSONA> </PERSONAE></PLAY>

AllsWellThatEndsWell.xml

Extract this fragment

Page 76: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 76

<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <xsl:apply-templates select="PLAY/PERSONAE"/> </xsl:template> <xsl:template match="*|@*|comment()|processing-instruction()|text()"> <xsl:copy> <xsl:apply-templates select="*|@*|comment()| processing-instruction()|text()"/> </xsl:copy> </xsl:template></xsl:stylesheet>

AllsWellThatEndsWell.xsl

Select the desiredsubtree (fragment)

Copy verbatim what isin the XML document,including elements anddata.

Page 77: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 77

<?xml version="1.0"?><PERSONAE> <PERSONA>KING OF FRANCE</PERSONA> <PERSONA>DUKE OF FLORENCE</PERSONA> <PERSONA>BERTRAM, Count of Rousillon.</PERSONA> <PERSONA>LAFEU, an old lord.</PERSONA></PERSONAE>

AllsWellThatEndsWellFragment.xml

Here’s the fragment that we have extracted from the original XML document

Page 78: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 78

Using XSL as a Search Engine

<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <xsl:apply-templates select="pattern"/> </xsl:template> <xsl:template match="*|@*|comment()|text()"> <xsl:copy> <xsl:apply-templates select="*|@*|comment()|text()"/> </xsl:copy> </xsl:template></xsl:stylesheet>

“From user’s search criteria, create an XSL select pattern. Embed this pattern into SearchTemplate.xsl

SearchTemplate.xsl

1

XSLProcessor

2

3 Results

Page 79: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 79

Notes on Last Slide

• The XSL document is being dynamically generated, based upon the user’s inputs!

• Imagine a user at a browser typing in search criteria, "I want the Shakespeare play, All's Well That Ends Well. I want a list of characters in the play." Behind the scenes an XSL select pattern string is being dynamically created based upon the user's search criteria. Then that pattern is dropped into an XSL search template (shown on previous page), where 'pattern' is replaced with the pattern that was just dynamically created. Now this XSL document is run through the XSL Processor to produce the desired fragment, which is then delivered to the user. Pretty cool, aye?

Page 80: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 80

Example

• Problem: write an xsl template rule that creates an HTML hyperlink <A> element where the value for the HREF attribute (of the <A> element) may be found in the child element.

Page 81: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 81

<?xml version="1.0"?><!DOCTYPE company [ <!ELEMENT company (manager*, employee*)><!ELEMENT manager (#PCDATA)><!ATTLIST manager id ID #REQUIRED><!ELEMENT employee (name, boss)><!ELEMENT name (#PCDATA)><!ELEMENT boss (#PCDATA)>]><company> <manager id="smith">John Smith</manager> <employee> <name>Bill Sinclair</name> <boss>smith</boss> </employee> <employee> <name>Sally Dunlop</name> <boss>smith</boss> </employee></company>

create an xsl stylesheetsuch that at this elementit creates a hyperlinkto the employee’s boss, using the valuein the <boss> childelement

Employee.xml

Page 82: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 82<?xml version="1.0"?><xsl: xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="company"> <HTML> <HEAD> <TITLE>Company</TITLE> </HEAD> <BODY> <xsl:apply-templates/> </BODY> </HTML> </xsl:template> <xsl:template match="manager"> <A name="{@id}"/> Manager name is: <xsl:apply-templates/><br/> </xsl:template> <xsl:template match="employee"> <A> <xsl:attribute name="HREF"> #<xsl:apply-templates select="boss"/> </xsl:attribute> <xsl:apply-templates select="name"/>'s boss </A> <BR/> </xsl:template></xsl:stylesheet>

This will generate:<A HREF=“#smith”> Sally Dunlap’s boss</A>(and a similar one forBill Sinclair)

Employee.xsl

Page 83: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 83

<?xml version="1.0"?><HTML> <HEAD> <TITLE>Company</TITLE> </HEAD> <BODY> <A name="smith"></A> Manager name is:John Smith<br> </br> <A HREF="#smith">Bill Sinclair's boss</A><BR> </BR> <A HREF="#smith">Sally Dunlop's boss</A><BR> </BR> </BODY></HTML>

Employee.html

Page 84: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 84

Accessing Elements that are not Descendents

• There are many cases where your template rule needs the value of an element that is “up the tree and over”, i.e., the needed value is not a descendent of your current location. This example shows how to deal with such situations.

Page 85: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 85

Getting title-cost pairs

<?xml version="1.0"?><!DOCTYPE movie [ <!ELEMENT movie (title+, cost)><!ELEMENT title (#PCDATA)><!ELEMENT cost (#PCDATA)>]><movie> <title>Meet Joe Black</title> <title>Entreau Joe Blacke</title> <cost>$12.95</cost></movie>

movie.xml

Problem: write a stylesheet which generates a table of title and cost. Each row of the table has the name (title) of the movie and the cost.

Page 86: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 86

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

<xsl:template match="movie"> <HTML> <HEAD> <TITLE>Book Catalogue</TITLE> </HEAD> <BODY> <TABLE BORDER="1" WIDTH="100%"> <xsl:apply-templates select="title"/> </TABLE> </BODY> </HTML> </xsl:template>

<xsl:template match="title"> <TR> <TD> <xsl:apply-templates/> </TD> <TD> <xsl:apply-templates select="../cost"/> </TD> </TR> </xsl:template>

<xsl:template match="cost"> <xsl:apply-templates/> </xsl:template>

</xsl:stylesheet>

movie.xsl

See next slide for explanation

Page 87: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 87

Getting the cost within the title template

movie

title title cost

Meet Joe Black Entreau Joe Blacke $12.95

Notice how thetemplate rule accessesthe value that is “upand over”

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

Page 88: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 88

Using XSL with IE5

• IE5 implements the Dec. 16 XSL spec ... almost– It doesn’t implement the full spec– It has a few quirks

• Check the following URL for the features that are not implemented: http://msdn.microsoft.com/xml/XSLGuide/conformance.asp

• On the next few slides I will tell you the “gotchas” that I discovered while using XSL in IE5 (and are not mentioned in the conformance notes)

Page 89: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 89

Must have at the top of your XSL file:<!-- XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX --> <!-- --> <!-- The following three template rules are required for IE5 --> <!-- (This is a quirk of IE5.) --> <!-- --> <!-- XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX --> <xsl:template match="/">

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

<xsl:template match="*"> <xsl:apply-templates/>

</xsl:template>

<xsl:template match="text()"> <xsl:value-of select="."/>

</xsl:template>

Failure to put these template rules at the top of you XSL file, before any other template rules, will result in the IE5 XSL processor not working!

Page 90: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 90

Alternative Symbol (“|”) Doesn’t Work

• You can’t use the alternative symbol in your match patterns. All the following won’t work in IE5!

<xsl:template match="* | /"> <xsl:apply-templates/> </xsl:template>

On the previous slide I said that you musthave a template rule for the root element “/”and for any other element “*”. You mightbe tempted to combine them using the “|”operator. Don’t do it!

<xsl:template match="Title | Author | Date | ISBN | Publisher"> <TD> <xsl:apply-templates/> </TD> </xsl:template>

This template rule won’t work in IE5.You will need to create 5 template rules,one for Title, one for Author, etc.

Page 91: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 91

Curly Braces {...} Not Implemented

<xsl:template match="Book"> <!-- Set href equal to the value of Book’s id attribute --> <a href=“#{@id}”></xsl:template>

Cannot do the folllowing:

So, what do you do? You use xsl:attribute as follows

<xsl:template match="Book"> <!-- Set href equal to the value of Book’s id attribute --> <a> <xsl:attribute name=“href”>#<xsl:value-of select="@id"/></xsl:attribute> </a> </xsl:template>

Important! All of this must be on the same line! Otherwise, it will not work in IE5

Page 92: Copyright @1999 Costello 1 XSL by Example (Recommendation 16 November 1999) ( Roger L. Costello copyright @1999

Copyright @1999 Costello 92

Check out the Example

• I have a nice example that runs in IE5. Check it out!

Cellphone.xmlCellphone.dtdCellphone.xsl Written to work with IE5