More On XSLT (Templates)
More On XSLT (Templates)
More On XSLT (Templates)
</xsl:stylesheet>
Example
• You decide to make the following changes to your XSLT: Module code is
displayed at the top of each module with a silver background.
• The values for credit and level are displayed in bold and blue colour.
• Use of templates to promote code reuse and modularise your XSLT file.
• NO SORTING - Present modules in same order as in XML file
• Below is a sample of the output you want
The code
• Source codes are found in the XSLTEx1WT
archive.
• The main template (<xsl:template
match="/">) is broken into smaller templates
where each element/attribute is processed by
its own template.
• The smaller templates usually have a match
attribute which contains the node name of the
element/attribute to process.
Code from the XSLT
• The <xsl:apply-templates/> in the main template will cause the
smaller templates to be applied to elements/attributes found in the
XML tree.
• If the apply-templates instruction has a select attribute, then only
the element/attribute that match the select attribute will be
processed. E.g., only the attribute code will be processed by the
following
• <xsl:apply-templates select="@code"/>
• The elements credit and level will be processed by the template
below which matches their path. The | will match both.
• <xsl:template match="credit|level">
• The code attribute will be processed by the following template
• <xsl:template match="@code">
The <xsl:copy> Element
<xsl:template match=”/”>
<Persons><xsl:apply-templates select=”/Persons/Person”
/></Persons>
</xsl:template>
<xsl:template match=”Person”>
<xsl:copy><xsl:attribute name=”FirstName”><xsl:value-of
select=”FirstName”/></xsl:attribute>
<xsl:attribute name=”LastName”><xsl:value-of
select=”LastName”/></xsl:attribute></xsl:copy></xsl:templ
ate></xsl:stylesheet>
Result – Personsout.xml
• <xsl:stylesheet version="1.0"
• xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
• <xsl:template match="class"> //context is class
• <html><body><h3>List of Students</h3>
• <ol><xsl:for-each select="student">
• <li><xsl:value-of select="."/></li></xsl:for-each>
• </ol>
• </body></html>
• </xsl:template>
• </xsl:stylesheet>
Classwork 4