Professional Documents
Culture Documents
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
XSLT Process
XSLT Status
W3C Recommendation - November 16, 1999
Version 1.0
Related Recommendations
XPath XSL
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>
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>
<otherwise>
Do something for other elements </otherwise> </choose>
<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 />
<sort select=expression>
<variable>
<param>
<output>
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