xslt - skilladvance · 2006. 3. 1. · non-xslt elements and text are moved to the result tree....

26
XSLT Student Workbook

Upload: others

Post on 06-Oct-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

XSLT

Student Workbook

Page 2: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

Page ii Rev 3.1.1 © 2002 ITCourseware, LLC

XSLT

XSLT

Rick Sussenbach and Todd Wright

Published by itcourseware, LLC, 7245 South Havana Street, Suite 100, Englewood, CO 80112

Editor: Jan Waleri

Editorial Assistant: Mark Walters

Special thanks to: Many instructors whose ideas and careful review have contributed to the quality of thisworkbook.

Copyright © 2002 by itcourseware, LLC. All rights reserved. No part of this book may be reproducedor utilized in any form or by any means, electronic or mechanical, including photocopying, recording, or byan information storage retrieval system, without permission in writing from the publisher. Inquiries should beaddressed to itcourseware, LLC, 7245 South Havana Street, Suite 100, Englewood, CO, 80112.(303) 302-5280.

All brand names, product names, trademarks, and registered trademarks are the property of their respectiveowners.

Page 3: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

© 2002 ITCourseware, LLC Rev 3.1.1 Page iii

XSLT

Contents

Chapter 1 - Course Introduction ............................................................................................................... 7

Course Objectives .............................................................................................................................. 8Course Overview .............................................................................................................................. 10Using the Workbook ......................................................................................................................... 11Suggested References ....................................................................................................................... 12

Chapter 2 - Introduction to XSLT ........................................................................................................... 15

Standards.......................................................................................................................................... 16Stylesheets ........................................................................................................................................ 18Processors ........................................................................................................................................ 20Processor Implementations ................................................................................................................ 22Results .............................................................................................................................................. 24What is XPath? ................................................................................................................................. 26Labs ................................................................................................................................................. 28

Chapter 3 - Getting Started ..................................................................................................................... 31

The Goal ........................................................................................................................................... 32Simplified Stylesheets ........................................................................................................................ 34Composite Stylesheet Blueprint ......................................................................................................... 36Root Template .................................................................................................................................. 38xsl:value-of ........................................................................................................................................ 40Other Templates ................................................................................................................................ 42xsl:apply-templates ............................................................................................................................ 44xsl:output ........................................................................................................................................... 46Labs ................................................................................................................................................. 48

Page 4: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

Page iv Rev 3.1.1 © 2002 ITCourseware, LLC

XSLT

Chapter 4 - XPath Nodetypes ................................................................................................................ 51

XPath Expressions ............................................................................................................................ 52XPath Context .................................................................................................................................. 54XPath Location Steps ....................................................................................................................... 56Element and Root Nodes .................................................................................................................. 58Text and Attribute Nodes .................................................................................................................. 60Comment and Processing Instruction Nodes ..................................................................................... 62Namespace Nodes ........................................................................................................................... 64Wildcards and Whitespace ................................................................................................................ 66Labs ................................................................................................................................................. 68

Chapter 5 - XPath Axes and Predicates .................................................................................................. 71

What Is An Axis? .............................................................................................................................. 72Peer Axis Types ................................................................................................................................ 74More Peer Axis Types ...................................................................................................................... 76Descendant Axis Types ..................................................................................................................... 78Ancestor Axis Types ......................................................................................................................... 80Predicates ......................................................................................................................................... 82Functions .......................................................................................................................................... 84id() Function ..................................................................................................................................... 86Labs ................................................................................................................................................. 88

Chapter 6 - XSLT Flow Control ............................................................................................................. 91

XPath Data Types ............................................................................................................................. 92Named Templates ............................................................................................................................. 94Mode ................................................................................................................................................ 96xsl:if .................................................................................................................................................. 98xsl:choose ....................................................................................................................................... 100xsl:for-each ..................................................................................................................................... 102Variables ......................................................................................................................................... 104Attribute Value Templates ................................................................................................................ 106Labs ............................................................................................................................................... 108

Page 5: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

© 2002 ITCourseware, LLC Rev 3.1.1 Page v

XSLT

Chapter 7 - More XSLT Elements ........................................................................................................ 111

xsl:sort ............................................................................................................................................ 112The document() ............................................................................................................................... 114Using Other Stylesheets .................................................................................................................. 116Template Rule Conflicts ................................................................................................................... 118Parameters ...................................................................................................................................... 120Recursion ........................................................................................................................................ 122XSLT Extensions ............................................................................................................................ 124Fallback Processing ........................................................................................................................ 126Labs ............................................................................................................................................... 128

Chapter 8 - XSLT for Other Output Types............................................................................................ 131

XML Output ................................................................................................................................... 132HTML Output ................................................................................................................................. 134Delimited Text Output ..................................................................................................................... 136Extended Output Types ................................................................................................................... 138Labs ............................................................................................................................................... 140

Solutions - XSLT .................................................................................................................................. 143

Index..................................................................................................................................................... 159

Page 6: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

Page vi Rev 3.1.1 © 2002 ITCourseware, LLC

XSLT

Page 7: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

Chapter 3 Getting Started

© 2002 ITCourseware, LLC Rev 3.1.1 Page 31

Chapter 3 - Getting Started

Objectives

� Write an XSLT simplified stylesheet.

� Write an XSLT composite stylesheet.

� Process XML documents with yourstylesheet.

Page 8: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

XSLT

Page 32 Rev 3.1.1 © 2002 ITCourseware, LLC

� XSLT is about transforming XML documents into something else.

� The "something else" can be another XML document, an HTMLdocument, or plain text.

� As the developer, you write a stylesheet.

� The stylesheet is loaded and parsed by an XSLT processor.

� The processor also loads and parses the source document into the sourcetree.

� The processor applies the stylesheet to the source tree and builds theresult tree.

� The result tree is then written out to the result document.

The Goal

Page 9: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

Chapter 3 Getting Started

© 2002 ITCourseware, LLC Rev 3.1.1 Page 33

The source document:shoppingcart.xml<?xml version="1.0" encoding="utf-8"?><shoppingcart>

...<clerk>Jamie</clerk><item>

<upc>3402349233</upc><name>Marigold, 6-pk</name><quantity>3</quantity><price>4.50</price><lineTotal>13.50</lineTotal>

</item><item>

<upc>3402346532</upc><name>Petunia, 4-pk</name><quantity>6</quantity><price>3.00</price><lineTotal>18.00</lineTotal>

</item><total>31.50</total>

</shoppingcart>

The result document:

Page 10: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

XSLT

Page 34 Rev 3.1.1 © 2002 ITCourseware, LLC

� There are two syntaxes for XSLT stylesheets: simple and composite.

� Simple stylesheets have an implied <xsl:template match="/">element.

� The transformation is specified in attributes of the root element of the stylesheet.

� XSLT is XML — even simple stylesheets must have a single root element.

� This is called the literal result element.

� The attributes must include the XSLT version that the stylesheet requires.

� The document element for a simple stylesheet is often <html>.

� If it is <html>, the processor serializes the result as well-formed HTML.

� A literal result element cannot contain a top-level XSL element.

� <xsl:template> is a top-level element.

� Thus, there can only be one template in a simplified stylesheet.

� Simple stylesheets are just that — simple.

� The prohibition against top-level elements prevents several things,including repeated application of templates for multiple child elements.

Simplified Stylesheets

Page 11: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

Chapter 3 Getting Started

© 2002 ITCourseware, LLC Rev 3.1.1 Page 35

Hands On:

Create a new file, checkout.xslt, and add the following elements:

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

<head><title>Sam's Garden Center</title></head><body><center>

<h1><font color="green">Sam's Garden Center</font></h1></center><center><h2>checkout</h2></center></body>

</html>

Try It: Use this stylesheet to transform shoppingcart.xml to checkout.html.

Page 12: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

XSLT

Page 36 Rev 3.1.1 © 2002 ITCourseware, LLC

� The composite stylesheet syntax allows much more complex types ofstylesheets.

� The root element of a composite stylesheet is <xsl:stylesheet>.

� Include the XSLT version and namespace declaration.

� Alternatively, you could use <xsl:transform> as the root element.

� There is no difference between <xsl:stylesheet> and<xsl:transform>.

� There are many allowable child elements of stylesheet.

� These are called top-level elements and can include:

� include and import elements to use other stylesheets.

� An output element to specify the type of output document.

� template elements.

� Only top-level XSLT elements can be children of the stylesheetelement.

Composite Stylesheet Blueprint

Page 13: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

Chapter 3 Getting Started

© 2002 ITCourseware, LLC Rev 3.1.1 Page 37

Hands On:

Add a stylesheet element to your checkout.xslt document and remove the attributes from the htmlelement:

<?xml version="1.0"?><xsl:stylesheet version="1.0"

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

<head><title>Sam's Garden Center</title></head><body>

<center><h1><font color="green">Sam's Garden Center</font></h1>

</center><center><h2>checkout</h2></center>

</body></html>

</xsl:stylesheet>

You will not be able to use your stylesheet at this time. Why?

Page 14: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

XSLT

Page 38 Rev 3.1.1 © 2002 ITCourseware, LLC

� Most stylesheets have at least one template element — the root template.

� The value of the match attribute is "/", which is the XPath syntax for theroot node.

<xsl:template match="/">

� The root template is applied to the root node of the source tree.

� Non-XSLT elements and text are moved to the result tree.

� XSLT elements are recognized by the namespace prefix.

� XSLT child elements are processed, as we will see later.

� When we add a root template to checkout.xslt, we get the same effect as we hadwith the simple stylesheet.

� A simple stylesheet has an implied root template.

Root Template

Page 15: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

Chapter 3 Getting Started

© 2002 ITCourseware, LLC Rev 3.1.1 Page 39

Hands On:

Add the root template to your checkout.xslt document. At this point you can process shoppingcart.xmland should get the same result that you had with the simple stylesheet.

<?xml version="1.0"?><xsl:stylesheet version="1.0"

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

<html><head><title>Sam's Garden Center</title></head><body>

<center><h1><font color="green">Sam's Garden Center</font></h1>

</center><center><h2>checkout</h2></center>

</body></html>

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

Page 16: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

XSLT

Page 40 Rev 3.1.1 © 2002 ITCourseware, LLC

� We want to add the name of the checkout clerk and the order total to thecheckout.html page.

� These are text data of the clerk and total child elements of theshoppingcart element.

� To retrieve the text data from an element, use the <xsl:value-of> element.

<xsl:value-of select="clerk">

� The <xsl:value-of> element is a child of an xsl:template.

� The <xsl:value-of> element will be replaced in the result tree with thetext data of the element specified in the select attribute.

� The value of the select attribute is an XPath expression to locate the chosenelement from the perspective of the template that is being processed.

� The root node is actually the parent of the document element of our XMLdocument, so we need to include the document element in our XPathexpression.

xsl:value-of

Page 17: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

Chapter 3 Getting Started

© 2002 ITCourseware, LLC Rev 3.1.1 Page 41

Hands On:

Add a horizontal rule element after the two heading lines, then add an <xsl:value-of> element to retrievethe name of the clerk. Build an HTML table that has columns for item, upc code, price, quantity and line total.At this point just include a total row with the total amount of the order in the far-right column.

<?xml version="1.0"?><xsl:stylesheet version="1.0"

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

<html><head><title>Sam's Garden Center</title></head><body>

<center><h1><font color="green">Sam's Garden Center</font></h1>

</center><center><h2>checkout</h2></center><hr/><center>

Your checker is <xsl:value-of select="shoppingcart/clerk"/></center>

<table width="60%" align="center" border="1"><tr><th width="40%">item</th><th width="15%">code</th>

<th width="10%">price</th><th width="10%">quantity</th><th width="25%">total</th></tr>

<tr><td>Grand Total</td><td></td><td></td><td></td><td align="right">

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

</tr></table><p>Thanks for your business!</p>

</body></html>

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

Page 18: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

XSLT

Page 42 Rev 3.1.1 © 2002 ITCourseware, LLC

� We can remove the need for the two-step selects in our <xsl:value-of>elements by adjusting the match of our template.

� If we change the template to match="shoppingcart", clerk andtotal are now immediate children.

� We can add another <xsl:value-of> element to display the line items in theshopping cart, but this will only display the first one.

� To get all of the line items, we need to add another template.

� Set match="item".

� An <xsl:template> element can only be a top-level child of the<xsl:stylesheet> element.

� <xsl:template> order does not matter.

Other Templates

Page 19: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

Chapter 3 Getting Started

© 2002 ITCourseware, LLC Rev 3.1.1 Page 43

Hands On:

First, change your template match to "shoppingcart" and remove the shoppingcart entries from the<xsl:value-of> selects.

<?xml version="1.0"?><xsl:stylesheet version="1.0"

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

Your checker is <xsl:value-of select="clerk"/>...<tr><td>Grand Total</td><td></td><td></td><td></td>

<td align="right"><xsl:value-of select="total"/></td></tr>...

Now, add another template below your first one to add a table row for each line item in the shopping cart:

...</xsl:template><xsl:template match="item">

<tr><td><xsl:value-of select="name"/></td><td><xsl:value-of select="upc"/></td><td align="right"><xsl:value-of select="price"/></td><td align="right"><xsl:value-of select="quantity"/></td><td align="right"><xsl:value-of select="lineTotal"/></td>

</tr></xsl:template>

</xsl:stylesheet>

Are your line items displayed?

Page 20: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

XSLT

Page 44 Rev 3.1.1 © 2002 ITCourseware, LLC

� The XSLT processor reads the XML source document from top to bottom,applying matching templates as it goes.

� On the previous page you added another template to match the multiple itemelements from the source document.

� The template was not used.

� The shoppingcart template had already been applied to that part of thedocument.

� Even if we place the item template before the shoppingcart template inthe stylesheet, the processor finds the shoppingcart element first in thesource tree.

� We can use the <xsl:apply-templates> element to specify when to apply atemplate within another template.

� When the XSLT processor identifies an <xsl:apply-templates>element, it applies any templates that match the select attribute.

� Because there are multiple item elements within the source tree, the processorrepeats the template for each item.

xsl:apply-templates

Page 21: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

Chapter 3 Getting Started

© 2002 ITCourseware, LLC Rev 3.1.1 Page 45

Hands On:

Add an <xsl:apply-templates> element to your stylesheet to invoke the item template that youadded previously:

...<table width="60%" align="center" border="1">

<tr><th width="40%">item</th><th width="15%">code</th><th width="10%">price</th><th width="10%">quantity</th><th width="25%">total</th>

</tr><xsl:apply-templates select="item"/><tr><td>Grand Total</td><td></td><td></td><td></td>

<td align="right"><xsl:value-of select="total"/></td></tr>

</table>...

Page 22: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

XSLT

Page 46 Rev 3.1.1 © 2002 ITCourseware, LLC

� You may have noticed that we used an empty XML element for the horizontalrule in our stylesheet: <hr/>.

� Unlike HTML, all XML tags must be closed.

� If our output document is HTML, we probably want a stand-alone <hr> tag.

� The extra space in the stylesheet element is to allow for browsers that donot understand xHTML.

� To specify HTML as our result document type, use an <xsl:output> element.

� <xsl:output> is a top-level XSLT element.

� Place it as a child of the <xsl:stylesheet> element.

� Specify the output type with the method attribute:

<xsl:output method="html"/>

� The processor will automatically convert your <hr/> element in thestylesheet to an <hr> tag in the result.

� This may have already been occurring because your processorrecognized that the result was an HTML document.

� You can also specify "text" as your output method.

<xsl:output method="text"/>

xsl:output

Page 23: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

Chapter 3 Getting Started

© 2002 ITCourseware, LLC Rev 3.1.1 Page 47

Hands On:

Specify the result document type with an <xsl:output> element in your stylesheet:

<?xml version="1.0"?><xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html"/><xsl:template match="shoppingcart">

...

Page 24: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

XSLT

Page 48 Rev 3.1.1 © 2002 ITCourseware, LLC

� Write an XSLT stylesheet that extracts the shipping information from shoppingcart.xml and createsa shipping label.Hint: <xsl:text> prevents the processor from formatting the data within the element.(Solution: shipping.xslt)

� Write an XSLT stylesheet that will transform the source XML document hamlet.xml to an HTMLplaybill that lists the acts and scenes.(Solution: playbill.xslt)

Labs

Page 25: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

Chapter 3 Getting Started

© 2002 ITCourseware, LLC Rev 3.1.1 Page 49

The labs use an XML version of Shakespeare's Hamlet which has been graciously released into the publicdomain by Jon Bosak (http://www.ibiblio.org/bosak/). The play is located in a file called hamlet.xml.The document type is defined in play.dtd.

When using text output, it is sometimes necessary to include linefeeds, spaces and other formattingcharacters in the output. Use the <xsl:text> element to represent these literal values.

For example:

...<xsl:template match="/"><xsl:text></xsl:text></xsl:template>...

will send a new line to the output document.

Page 26: XSLT - SkillAdvance · 2006. 3. 1. · Non-XSLT elements and text are moved to the result tree. XSLT elements are recognized by the namespace prefix. XSLT child elements are processed,

XSLT

Page 50 Rev 3.1.1 © 2002 ITCourseware, LLC