XML and XSL
Introducing templates
The XML contains a single stock element which in turn contains four items. We use this structure to investigate how XSL processes nodes. The template is tested against every node in the input tree. When a match is made then the content of the template is output. Once a match is made the child nodes will not be considered unless an explicit instruction to do so is given. This is a simple transformation, it outputs a single constant value for every node matched.
XML
<?xml version="1.0"?>
<stock>
<item price="50" legend="Pr-Burger" BarCode="E1"/>
<item price="15" legend="Crisp S+V" BarCode="E5"/>
<item price="15" legend="Crisp C+O" BarCode="E6"/>
<item price="50" legend="Flat Cola" BarCode="E7"/>
</stock>
XSL
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="stock">
<p>Found something!</p>
</xsl:template>
</xsl:stylesheet>
Investigating xsl:value-of
Typically we need to consider two issues in a template:
- Which nodes do we want to match?
- What do we want to output?
We can decide which nodes match
using match attribute of the xsl:template
node - the content of this node determines what gets output.
In this example we output the bar code and the description of each item.
XML
<?xml version="1.0"?>
<stock>
<item price="50" legend="Pr-Burger" BarCode="E1"/>
<item price="15" legend="Crisp S+V" BarCode="E5"/>
<item price="15" legend="Crisp C+O" BarCode="E6"/>
<item price="50" legend="Flat Cola" BarCode="E7"/>
</stock>
XSL
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="item">
<p>
<b><xsl:value-of select="@BarCode"/></b>:
<xsl:value-of select="@legend"/>
</p>
</xsl:template>
</xsl:stylesheet>
Deciding what to show
We can use the match attribute to decide which nodes are considered. There are a number of ways of performing matches.
- Use
match="item[@price=15]"
to show only items costing 15p - Use
match="item[position()=1]"
to show only the first item - Use
match="item[position()=last()]"
to show only the last item - Use
match="item[starts-with(@legend,'Crisp')]"
to show… Show only those items with anr
in the legend (you will probably need to refer to the XPath specification).
XML
<?xml version="1.0"?>
<stock>
<item price="50" legend="Pr-Burger" BarCode="E1"/>
<item price="15" legend="Crisp S+V" BarCode="E5"/>
<item price="15" legend="Crisp C+O" BarCode="E6"/>
<item price="50" legend="Flat Cola" BarCode="E7"/>
</stock>
XSL
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="item">
<p>
<b><xsl:value-of select="@legend"/></b>
</p>
</xsl:template>
</xsl:stylesheet>
Setting and using a variable
We create a variable and do some arithmetic on it.
- Observe that the sheet calculates the price of the Blur album plus shipping.
- Change the sheet so that it calculates the price of Travis plus shipping plus tax - the 10 means add 10% of the album price. The total should be 17.3 How to divide: See XPath specification, 3.5 Number Expressions.
XML
<catalogue shipping="3" tax="10">
<album id="Blur" price="12"/>
<album id="Travis" price="13"/>
</catalogue>
XSL
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="s" select="//catalogue/@shipping"/>
<xsl:template match="album[@id='Blur']">
<xsl:value-of select="@price + $s"/>
</xsl:template>
</xsl:stylesheet>