Reputation: 5
Hi, I found this, but isn't what I want. I want this output
Driver B 27
Driver A 18
This is the XML:
<grid>
<driver>
<name>Driver B</name>
<points>
<imola>10</imola>
<monza>9</monza>
<silverstone>8</silverstone>
</points>
</driver>
<driver>
<name>Driver A</name>
<points>
<imola>7</imola>
<monza>6</monza>
<silverstone>5</silverstone>
</points>
</driver>
</grid>
And this is the XSLT:
<xsl:template match="/grid">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Championship</title>
</head>
<body>
<h1>Classification</h1>
<xsl:apply-templates select="driver" />
</body>
</html>
</xsl:template>
<xsl:template match="driver">
<xsl:for-each select=".">
<p>
<xsl:value-of select="name" />
<xsl:value-of select="sum(???)" /> <!-- Here, I don't know the code to sum the points of the races-->
</xsl:for-each>
</xsl:template>
I'm sure the solution is easy, but I can't find it. Thanks.
Upvotes: 0
Views: 55
Reputation: 101700
You can do so like this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/grid">
<html>
<head>
<title>Championship</title>
</head>
<body>
<h1>Classification</h1>
<xsl:apply-templates select="driver" />
</body>
</html>
</xsl:template>
<xsl:template match="driver">
<p>
<xsl:value-of select="concat(name, ' ', sum(points/*))" />
</p>
</xsl:template>
</xsl:stylesheet>
Here, *
means "match any child element".
This produces the output:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Championship</title>
</head>
<body>
<h1>Classification</h1>
<p>Driver B 27</p>
<p>Driver A 18</p>
</body>
</html>
Upvotes: 2