Template Instructions
- xsl:apply-templates
- Find correct template to apply
- xsl:value-of
- Evaluates select and outputs value
- xsl:text
- Outputs text
- xsl:copy
- Copy target node from source to output
- xsl:copy-of
- Complete tree copy
- xsl:for-each
- Applies same template to multiple document nodes
- xsl:choose
- Conditional testing with otherwise and when
- xsl:if
- Conditional test
- xsl:call-template
- Calls a named template
- xsl:document
- Creates a new output file
- xsl:element
- Generates an element in the output with specified name
- xsl:attribute
- Generates attribute node and applies to element
- xsl:cdata
- Outputs a CDATA section in the output
- xsl:variable
- Defines a local or global variable
xsl:copy
<xsl:copy>
Copies the current node from the Source Tree to the Result Tree
Note: it does not copy the contents of the node, just the node itself
<?xml version='1.0'?> <xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" > <xsl:output method="xml" indent="no"/> Puts an XML declaration on the output, no indentation <xsl:template match="*" > Matches any node in the XML tree <xsl:copy> Copy this node to the output tree <xsl:apply-templates /> Apply any templates to the node's children </xsl:copy> </xsl:template> </xsl:stylesheet>
xsl:copy-of
For copying the whole tree, rather than specific nodes, to the output, use xsl:copy-of:
<xsl:template match="thought" > <xsl:copy-of select="."/> </xsl:template>
thought node, its attributes, its descendants and all their attributes are copied to the output tree
xsl:for-each
<xsl:for-each select="...">
- Selects a set of nodes for processing
- Performs the same processing on each node in the node-set
- The nodes in the node-set are processed in document order unless sort instructions appear
- Document order is the order of traversal of the tree
- A node comes before its attributes
- Its attributes come before its children
- No order of attributes is defined
- Template actively defines the order in which processing takes place
- A single template creates the complete Output Tree
Here is an example of an XML file:
- Many XML documents contain multiple records that are similar
- Templates need to be applied to all or some of these
<cdlist> <cd> <artist>Aatabou, Najat</artist> <title>The Voice of the Atlas</title> <label></label> <catalog>CDORBD 069</catalog> <filed>C05 World</filed> <playlist> <work>Baghi narajah</work> <work>Finetriki</work> <work>Shouffi rhirou</work> <work>Lila ya s'haba</work> <work>Ouardatte lajnane</work> <work>Ditih</work> </playlist> </cd> - - - </cdlist>
xsl:for-each
<xsl:template match="cdlist"> The context node <xsl:for-each select="cd"> Each cd becomes the current node in turn <hr /> <h2> <xsl:value-of select="artist"/></h2> child of current node <h2><xsl:value-of select="title"/></h2> <table summary="cd stats" width="80%"> <col width="20%" /><col width="20%" /><col width="20%" /><col width="20%" /> <thead> <tr><th>Label</th><th>Catalog Number</th><th>Stored At</th></tr> </thead><tbody> <tr> <td><xsl:value-of select="label"/></td> <td><xsl:value-of select="catalog"/></td> <td><xsl:value-of select="filed"/></td> </tr> </tbody></table> <h3>Playlist</h3> <ul> <xsl:for-each select="playlist/work"> Each work is the current node in turn <li><xsl:value-of select="." /></li> </xsl:for-each> </ul> </xsl:for-each> <hr /> </xsl:template>
Sorting
<xsl:sort select="xxx" order="" case-order="" data-type=""/>
- Instead of document order, specifies a different order for processing child nodes
- Can be a child of xsl:apply-templates or xsl:for-each
- Within xsl:for-each it must be before any other instructions
- Several xsl:sort are allowed and the first is the major sort, the minor sort on the next and so on
- select attribute defines the sort key. Default is the text value of the node-set selected
- order can have values ascending or descending
- case-order can have values upper-first or lower-first
- data-type can have values text or number to decide whether values are sorted alphabetically or numerically. Default is alphabetic
- data-type="text" puts 10 before 5 while data-type="number" puts 5 before 10
- The select value can be complex and include doing arithmetic so this is a sophisticated operation
xsl:if
<xsl:if test=".." > . . . </xsl:if>
- Contents of <xsl:if> element only obeyed if test is true:
- If test result is a node-set, true if node-set contains one or more nodes
- If test result is a string, true if string is non-empty
- If test result is a number, true if value is non-zero
- If test result is a truth value, depends on the truth value
- Only CDs satisfying the test are retained
<xsl:template match="cdlist"> <xsl:for-each select="cd"> <xsl:if test="filed[. = 'C05 World']"> <tr> <td style="color:blue;font-weight:bold"><xsl:value-of select="artist"/></td> <td style="color:red;font-weight:bold"><xsl:value-of select="title"/></td> <td style="color:darkgray;font-weight:bold"><xsl:value-of select="filed"/> </td> </tr> </xsl:if> </xsl:for-each> </xsl:template>
Filtering via <xsl:choose>
<xsl:choose> <xsl:when test=".."> ... </xsl:when> <xsl:when test=".."> ... </xsl:when> <xsl:otherwise> . . . </xsl:otherwise> </xsl:choose>
- xsl:choose provides either or processing
- Set of xsl:when elements tested in order
- If none match, xsl:otherwise is obeyed
For example, colour code the different types of CD
Colour Code the CDs
<xsl:for-each select="cd"> <tr> <xsl:choose> <xsl:when test="filed[. = 'C05 World']"> <td style="color:red"> <xsl:value-of select="artist"/> </td> <td> <xsl:value-of select="title"/> </td> </xsl:when> <xsl:when test="filed[. = 'C06 Country']"><td style="color:blue">...</xsl:when> <xsl:when test="filed[. = 'C11 Jazz Trad']"><td style="color:green">...</xsl:when> <xsl:when test="filed[. = 'C20 Fado']"><td style="color:violet">...</xsl:when> <xsl:when test="filed[. = 'C01 Pop']"><td style="color:gray">...</xsl:when> <xsl:when test="filed[. = 'C07 Cajun']"><td style="color:brown">...</xsl:when> <xsl:when test="filed[. = 'C03 Jazz Vocal']"><td style="color:yellow">... </xsl:when> <xsl:otherwise><td style="color:black">...</xsl:otherwise> </xsl:choose> </tr> </xsl:for-each>
There are other XSLT instructions but these give a flavour of what you can do.