Get it on Google Play

XML i XSLT

27-04-2007
 

Exemple d’us d’un formulari XSLT per a donar format a un document XML. Haig de dir que la tecnologia XSLT m’ha decepcionat molt per la seva manca de funcionalitat. No es poden fer totes les transformacions que un voldria i a mes es la óstia de poc elegant.

Document XML: pac.xml

<“xml version=”1.0″”>
<“xml-stylesheet href=”pac.xslt” type=”text/xsl””>
<Fer>
<VarGame>
<Team1>Barcelona</Team1>
<Team2>Bilbao</Team2>
<Goals_Team1>2</Goals_Team1>
<Goals_Team2>1</Goals_Team2>
</VarGame>

<VarGame>
<Team1>Madrid</Team1>
<Team2>Salamanca</Team2>
<Goals_Team1>0</Goals_Team1>
<Goals_Team2>0</Goals_Team2>
</VarGame>

<VarGame>
<Team1>Salamanca</Team1>
<Team2>Barcelona</Team2>
<Goals_Team1>0</Goals_Team1>
<Goals_Team2>4</Goals_Team2>
</VarGame>

<VarGame>
<Team1>Bilbao</Team1>
<Team2>Madrid</Team2>
<Goals_Team1>1</Goals_Team1>
<Goals_Team2>1</Goals_Team2>
</VarGame>

<VarGame>
<Team1>Barcelona</Team1>
<Team2>Madrid</Team2>
<Goals_Team1>5</Goals_Team1>
<Goals_Team2>3</Goals_Team2>
</VarGame>

<VarGame>
<Team1>Salamanca</Team1>
<Team2>Bilbao</Team2>
<Goals_Team1>3</Goals_Team1>
<Goals_Team2>4</Goals_Team2>
</VarGame>
</Fer>

Document XSLT: pac.xslt

<“xml version=”1.0″ encoding=”UTF-8″”>
<xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>

<xsl:template match=’/’>

<html>
<head>
<title>CLASSIFICACIO</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />
</head>

<body>

<h1>CLASSIFICACIO</h1>
<br/>
<table border=’1’>
<tr>
<th>Equip</th>
<th>Partits jugats</th>
<th>Gols en contra</th>
<th>Gols a favor</th>
<th>Punts</th>
</tr>

<xsl:apply-templates select=”Fer”>
<xsl:with-param name=”equip”>Barcelona</xsl:with-param>
</xsl:apply-templates>

<xsl:apply-templates select=”Fer”>
<xsl:with-param name=”equip”>Madrid</xsl:with-param>
</xsl:apply-templates>

<xsl:apply-templates select=”Fer”>
<xsl:with-param name=”equip”>Bilbao</xsl:with-param>
</xsl:apply-templates>

<xsl:apply-templates select=”Fer”>
<xsl:with-param name=”equip”>Salamanca</xsl:with-param>
</xsl:apply-templates>
</table>

</body>

</html>
</xsl:template>

<xsl:template match=”Fer”>
<xsl:param name=”equip”/>

<tr>
<td> <xsl:value-of select=’$equip’ /> </td>
<td> <xsl:value-of select=”count( VarGame/Team1[ ../Team1=$equip ] )+count( VarGame/Team2[ ../Team2=$equip ] ) ” /> </td>
<td> <xsl:value-of select=”sum( VarGame/Goals_Team1[ ../Team2=$equip ] ) + sum( VarGame/Goals_Team2[ ../Team1=$equip ] )” /> </td>
<td> <xsl:value-of select=”sum( VarGame/Goals_Team1[ ../Team1=$equip ] ) + sum( VarGame/Goals_Team2[ ../Team2=$equip ] )” /> </td>
<td> <xsl:value-of select=”count( VarGame/Team1[ ../Team1=$equip and ../Goals_Team1>../Goals_Team2 ] )*3 + count(VarGame/Team1[ ../Team2=$equip and ../Goals_Team2>../Goals_Team1 ] )*3 + count( VarGame/Team1[ ../Team1=$equip and ../Goals_Team1=../Goals_Team2 ] ) + count(VarGame/Team1[ ../Team2=$equip and ../Goals_Team2=../Goals_Team1 ] )” /> </td>
</tr>

</xsl:template>

</xsl:stylesheet>

Leave a Reply

© Albert Coronado Calzada