XSLT Tutorial
Worksheet 3: xsl:template & xsl:apply-templates
The following example shows how the xsl:template allows you to
define rules for a node without being concerned about the context.
In the Worksheet_2 directory, our data is in the penguin.xml
file.
<article>
<date>28/06/2000 12:30</date>
<title>Rescued penguins swim home</title>
<para><place>Cape Town</place> Some 150 penguins
unaffected by the oil spill
began their long swim from Port Elizabeth in the Eastern Cape back to
their breeding
habitat at Robben Island near Cape Town on Wednesday. </para>
<para>The penguins, who have all been tagged, were transported in
a truck hired
by the<company>South African National Conservation of Coastal
Birds
(Sanccob)</company> to Port Elizabeth on Tuesday night.
</para>
....
</article>
View XML Source
Q: How do we convert it to the
following HTML format?:
28/06/2000 12:30
Rescued penguins swim home
Cape Town - Some 150 penguins
unaffected by the oil spill began their long swim from Port Elizabeth
in the Eastern Cape back to their breeding habitat at Robben Island
near Cape Town on Wednesday.
The penguins, who have all been tagged, were transported in a truck
hired by the South African National Conservation of Coastal Birds
(Sanccob) to Port Elizabeth on Tuesday night.
Its not known how many more birds will be released from Port
Elizabeth after receiving treatment.
More than 400 tons of fuel oil escaped from the bulk
ore carrier Treasure before divers were able to seal the holds.
The benefit of using rule-based programming, is that each element is
outputted independently of the context.
Here's what to do:
Using your MarrowSoft Xselerator
XSLT IDE tool open the penguin.xml file from the
Worksheet_3 directory into the XML screen and then open the article.xsl file
into the XSL screen
Here's the article.xsl file:
<xsl:template match="article">
<html><basefont face="Verdana"
size="2"/><body>
<!--
Go and apply all the templates for the root
-->
<xsl:apply-templates/>
</body></html>
</xsl:template>
<xsl:template match="date">
<p><font face="verdana" size="1"><xsl:apply-templates/></font></p>
</xsl:template>
<xsl:template match="para">
<p><xsl:apply-templates/></p>
</xsl:template>
View XSL Source
Run this transformation (F5) in your XSLT Transforming tool and
you'll see the above HTML output.
This is how it works:
- <xsl:template
match="article">: the processor finds the document
element and starts the processing
- <xsl:apply-templates/>: An
instruction to apply templates to all the elements under the root, i.e.
find all direct child nodes under the root and process them.
- <xsl:template match="date">,
<xsl:template match="para">, etc:
As the processor finds the new elements, it looks for any matching child
pattern. If it is found, it processes that output.
- If you look at the <xsl:template match="para">
it also has a command to <xsl:apply-templates />, this
allows that the child elements under the para element are then
processed.
Here's what to do, next:
Go through each of the following. When you've completed each
task, revert back to the original XSL, to continue.
- Remove the <xsl:template
match="link"> template and see what
happens.
- Remove the line, <font
face="verdana" size="2"><b><xsl:apply-templates/></b>
- </font> from the template
<xsl:template match="place"> and see what happens.
- Under the
<xsl:template match="/"> instruction, change the <xsl:apply-templates/> to only process the
'para' and 'source' elements. (See
Expressions, return a node set using a compute(union))
A: Solution
|