Transformasjon fra XML til HTML
Øvelsen er slik:
Transformasjonen nedenfor er basert på flere templates. Det er generelt en god ide å bryte opp koden i templates. Foruten at det hjelper oss å rydde i koden, så legger det til rette for vedlikehold og gjenbruk.
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/> <xsl:template match="/"> <html> <head> <link rel="STYLESHEET" href="olymp5.css"/> <title>Olympics</title> </head> <body> <h1>Resultater sprint</h1> <hr/> <xsl:apply-templates select="IOC/OlympicGame"> <xsl:sort select="@year" order="ascending"/> </xsl:apply-templates> </body> </html> </xsl:template> <xsl:template match="OlympicGame"> <table cellpadding="10"> <tr> <td> <xsl:element name="img"> <xsl:attribute name="src"> <xsl:value-of select="@place"/>.gif</xsl:attribute> <xsl:attribute name="alt"> <xsl:value-of select="@place"/></xsl:attribute> </xsl:element> </td> <td> <h1><xsl:value-of select="@place"/> <br/> <xsl:value-of select="@year"/></h1> </td> </tr> </table> <table cellpadding="10" border="0" cellspacing="0"> <tr> <xsl:apply-templates select="event"/> </tr> </table> </xsl:template> <xsl:template match="//event"> <td valign="top"> <h2><xsl:value-of select="@track"/></h2> <xsl:apply-templates select="athlet"> <xsl:sort data-type="number" select="result"/> </xsl:apply-templates> </td> </xsl:template> <xsl:template match="athlet"> <p><xsl:value-of select="name"/><br/> <xsl:value-of select="nation"/><br/> <xsl:value-of select="result"/></p> </xsl:template> </xsl:stylesheet>
Merk at resultatene er sorteres.