Cheatography
https://cheatography.com
XSLT Cheat Sheet.
Information shared by w3schools.com.
This is a draft cheat sheet. It is a work in progress and is not finished yet.
Overview
XSLT stands for XSL Transformations |
XSLT uses XPath to navigate in XML documents |
XSLT transforms an XML document into another XML document |
To declare an XSL style sheet use one of the following - <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
- <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
|
Note: <xsl:stylesheet>
and <xsl:transform>
are completely synonymous and either can be used!
|
XSL Elements
<xsl:stylesheet> |
Defines this document as an XSLT style sheet |
<xsl:template match="/"> |
Used to build templates. Use the match attribute to select elements with XPath. Content defines HTML to output. |
<xsl:value-of select"/"> |
Used to extract the value of an XML element and add it to the output. Use the select attribute to select elements with XPath. |
<xsl:for-each select="/"> |
Used to select every XML element of a specified node-set. Wrap elements containing <xsl:value-of>
(Loop) |
<xsl:sort select="/"> |
Used to sort the output. |
<xsl:if test="expression"> |
Used to put a conditional if test against the content of the XML file. Add inside for-each tags to add a condition to the loop. |
<xsl:choose> |
Used in conjunction with <xsl:when>
and <xsl:otherwise>
to express multiple conditional tests. Wrap around these tags like if {...} else {...}. |
<xsl:when test="expression"> |
The IF or ELSE IF component of a choose statement. Multiple can be used back to back within <xsl:choose>
|
<xsl:otherwise> |
The ELSE component of a choose statement. |
<xsl:apply-templates select="/"> |
Used to apply a template to the current element or to the current element's child nodes. |
Note: You can filter the output from the XML file by adding criterion to the select attribute in the <xsl:for-each> element.
Example: <xsl:for-each select="catalog/cd[artist='Bob Dylan']">
Legal operators are =, !=, < (<), > (>)
|
|
Template/Apply-Templates Example
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="cd">
<p>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="artist"/>
</p>
</xsl:template>
<xsl:template match="title">
Title: <span style="color:#ff0000">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
<xsl:template match="artist">
Artist: <span style="color:#00ff00">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
|
|