Hej,
Jag har ett script som skapar ett flöde som ser ut såhär:
<?xml version="1.0" encoding="utf-8"?>
<feed>
<tournament id="139791833" name="Friendly Club Matches 2009">
<match id="153683723" startdate="2009-08-14 20:00:00">
<participants>
<participant id="301" type="home">Sevilla FC</participant>
<participant id="302" type="away">Valencia CF</participant>
</participants>
<odds>
<outcome id="44720145" type="1">2.40</outcome>
<outcome id="44720131" type="X">3.20</outcome>
<outcome id="44720142" type="2">2.60</outcome>
</odds>
</match>
<match id="156978934" startdate="2009-08-15 20:00:00">
<participants>
<participant id="299" type="home">Real Sociedad</participant>
<participant id="298" type="away">Real Madrid</participant>
</participants>
<odds>
<outcome id="45756509" type="1">6.60</outcome>
<outcome id="45756645" type="X">3.85</outcome>
<outcome id="45756510" type="2">1.45</outcome>
</odds>
</match>
</tournament>
</feed>
Av detta vill jag få ut (x)html kod som ser ut såhär:
<table>
<tr>
<td colspan="6">Friendly Club Matches 2009</td>
</tr>
<tr>
<th>Start</th>
<th colspan="2">Match</th>
<th>Home</th>
<th>Draw</th>
<th>Away</th>
</tr>
<tr>
<td>2009-08-14 20:00</td>
<td>Sevilla FC</td>
<td>Valencia CF</td>
<td>2.40</td>
<td>3.20</td>
<td>2.60</td>
</tr>
...
</table>
Men jag har kört fast ordentligt och vet inte hur jag ska börja. Tacksam för lite hjälp. Då flödet är dynamsikt så kan tournament förekomma x antal gånger och match likaså.
Tack!
Nu har jag provat lite och har nästan löst det..
<?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>
<body>
<table cellpadding="0" cellspacing="0" style="width: 100%; border: 1px #333 solid;">
<xsl:apply-templates select="feed"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="feed">
<xsl:for-each select="tournament">
<thead>
<tr>
<th bgcolor="#cc0000" align="left" colspan="6"><span style="color: #fff;"><xsl:value-of select="@name"/></span></th>
</tr>
<tr>
<th align="left">Time</th>
<th align="left" colspan="2">Match</th>
<th align="left">Home</th>
<th align="left">Draw</th>
<th align="left">Away</th>
</tr>
</thead>
<tbody>
<xsl:for-each select="match">
<tr class="GridRow_Default">
<td><xsl:value-of select="@startdate"/></td>
<xsl:for-each select="participants">
<td><xsl:value-of select="participant"/></td>
</xsl:for-each>
<xsl:for-each select="odds">
<td><xsl:value-of select="outcome"/></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</tbody>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Det ger följande kod:
<table cellspacing="0" cellpadding="0" style="border: 1px solid rgb(51, 51, 51); width: 100%;">
<thead>
<tr>
<th bgcolor="#cc0000" align="left" colspan="6"><span style="color: rgb(255, 255, 255);">Friendly Club Matches 2009</span></th>
</tr>
<tr>
<th align="left">Time</th>
<th align="left" colspan="2">Match</th>
<th align="left">Home</th>
<th align="left">Draw</th>
<th align="left">Away</th>
</tr>
</thead>
<tbody>
<tr class="GridRow_Default">
<td>2009-08-14 20:00:00</td>
<td>Sevilla FC</td>
<td>2.40</td>
</tr>
<tr class="GridRow_Default">
<td>2009-08-15 20:00:00</td>
<td>Real Sociedad</td>
<td>6.60</td>
</tr>
</tbody>
</table>
Av någon skum anledning skrivs bara ett lag (participant) och ett odds (outcome) ut.
Varför? Bör inte loopen köras igenom "varje varv"?
Yes! Lyckades få till det:
<xsl:for-each select="match">
<tr class="GridRow_Default">
<td><xsl:value-of select="@startdate"/></td>
<xsl:for-each select="participants/participant">
<td><xsl:value-of select="."/></td>
</xsl:for-each>
<xsl:for-each select="odds/outcome">
<td align="center"><xsl:value-of select="."/></td>
</xsl:for-each>
</tr>
</xsl:for-each>