You are on page 1of 25

XSLT

Susanne Sherba October 12, 2000

Overview
What is XSLT? Related Recommendations

XSLT Elements
Associating Stylesheets with Documents

Additional Information

What is XSLT?
A language for transforming XML documents into other XML documents

Designed for use both as part of XSL and independently of XSL


Not intended as a completely generalpurpose XML transformation language. [W3C XSLT Recommendation]

XSLT Process

XSLT Status
W3C Recommendation - November 16, 1999

Version 1.0

Related Recommendations
XPath XSL

XML Stylesheet Recommendation

XSLT Stylesheet Element


<stylesheet version = 1.0> <transform> allowed as synonym

XSLT Template Element


<template match = expression name = name priority = number mode = name>

Applying Templates
<apply-templates select = expression mode = name> <call-template name = name>

Example 1
<?xml version="1.0"?> <?xml-stylesheet href="style1.xsl type="text/xsl"?> <items> <item partNum="123-AB"> <productName>Porsche</productName> <quantity>1</quantity> </item> <item> <productName>Ferrari</productName> <quantity>2</quantity> </item> </items> <?xml version="1.0"?> <xsl:stylesheet version=1.0 xmlns:xsl="http://www.w3.org/TR /WD-xsl"> <xsl:template match="/"> <html> <head></head> <body> <ol> <li>Root</li> <xsl:apply-templates/> </ol> </body> </html> </xsl:template> <xsl:template match="items"> <li>Items</li> <xsl:apply-templates/> </xsl:template> <xsl:template match="item"> <li>Item: <xsl:value-of select="productName"/></li> </xsl:template> </xsl:stylesheet>

Example 1

Example 2
<?xml version="1.0"?> <?xml-stylesheet href="style2.xsl type="text/xsl"?> <items> <item partNum="123-AB"> <productName>Porsche</productName> <quantity>1</quantity> </item> <item> <productName>Ferrari</productName> <quantity>2</quantity> </item> </items> <?xml version="1.0"?> <xsl:stylesheet version=1.0 xmlns:xsl="http://www.w3.org/TR /WD-xsl"> <xsl:template match="/"> <html> <head></head> <body> <ol> <xsl:apply-templates/> <li>Root</li> </ol> </body> </html> </xsl:template> <xsl:template match="items"> <xsl:apply-templates/> <li>Items</li> </xsl:template> <xsl:template match="item"> <li>Item: <xsl:value-of select="productName"/></li> </xsl:template> </xsl:stylesheet>

Example 2

Repetition
<for-each select = item>

Do something here ...


</for-each>

Example 3
<?xml version="1.0"?> <?xml-stylesheet href="style3.xsl type="text/xsl"?> <items> <item partNum="123-AB"> <productName>Porsche</productName> <quantity>1</quantity> </item> <item> <productName>Ferrari</productName> <quantity>2</quantity> </item> </items> <?xml version="1.0"?> <xsl:stylesheet version=1.0 xmlns:xsl="http://www.w3.org/TR /WD-xsl"> <xsl:template match="/"> <html> <head></head> <body> <ol> <li>Root</li> <xsl:apply-templates/> </ol> </body> </html> </xsl:template> <xsl:template match="items"> <li>Items</li> <xsl:for-each select="item"> <li>Item: <xsl:value-of select="productName"/> </li> </xsl:for-each> </xsl:template> </xsl:stylesheet>

Example 3

Conditional Processing
<if test = position()=last()>

Do something
</if>

Conditional Processing
<choose> <when test = position()=last()> Do something for last element </when>

<when test = position()=first()> Do something for first element


</when>

<otherwise>
Do something for other elements </otherwise> </choose>

Creating the result tree


<value-of select=expression> <element name=string>

<attribute name=string>
<processing-instruction name=string> <comment> <text>

Example 4
<xsl:template match="/"> <html> <head></head> <xsl:comment> Set the background to red </xsl:comment> <xsl:element name="body"> <xsl:attribute name="bgcolor"> red </xsl:attribute> <ol> <li>Root</li> <xsl:apply-templates/> </ol> </xsl:element> </html> </xsl:template> ...

XSL output:
<html> <head></head> <!--Set the background to red--> <body bgcolor="red"> <ol> <li>Root</li> <li>Items</li> <li>Item: Porsche</li> <li>Item: Ferrari</li> </ol> </body> </html>

Numbering
<number count = pattern from = pattern value = number-expression format = string />

Combining Stylesheets
<include href = uri /> <import href = uri />

Some Other Elements


<copy> <copy-of>

<sort select=expression>
<variable>

<param>
<output>

Associating Stylesheets with Documents


W3C Stylesheets Recommendation Version 1.0 In the xml document:

<?xml-stylesheet href=uri type=text/xsl?>

Additional Information
XSLT Recommendation Version 1.0 http://www.w3.org/TR/xslt

XSLT.com - http://www.xslt.com
XSLT Reference http://www.zvon.org/xxl/XSLTreference/ Output/index.html

You might also like