Reputation: 5
i need some help with add information to table in xsl inside for-each loop.
i want to add Link to Wikipedia for every row in table, link for each album, but im in "for-each" loop.
look for my question in section code in "a href...." tag.
for now all the links is to "Empire Burlesque".
i think its very simple, but how i do this? Thank you very much for your help
XML Code:
<?xml version="1.0" encoding="ISO8859-1"?>
<?xml-stylesheet href="style.xsl" type="text/xsl" ?>
<Albums>
<Album>
<Name>Empire Burlesque</Name>
<Artist>Bob Dylan</Artist>
<Country>USA</Country>
<Company>Columbia</Company>
<Price>10.20</Price>
<Date>19880610</Date>
</Album>
<Album>
<Name>Hide your heart</Name>
<Artist>Bonnie Tylor</Artist>
<Country>UK</Country>
<Company>CBS Records</Company>
<Price>9.90</Price>
<Date>19880509</Date>
</Album>
<Album>
<Name>Greatest Hits</Name>
<Artist>Dolly Parton</Artist>
<Country>USA</Country>
<Company>RCA</Company>
<Price>9.90</Price>
<Date>1982</Date>
</Album>
<Album>
<Name>Still got the blues</Name>
<Artist>Gary More</Artist>
<Country>UK</Country>
<Company>Virgin redords</Company>
<Price>10.20</Price>
<Date>1990</Date>
</Album>
<Album>
<Name>Eros</Name>
<Artist>Eros Ramazzotti</Artist>
<Country>EU</Country>
<Company>BMG</Company>
<Price>9.90</Price>
<Date>1997</Date>
</Album>
<Album>
<Name>25</Name>
<Artist>Adele</Artist>
<Country>UK</Country>
<Company>XL Recordings</Company>
<Price>9.90</Price>
<Date>20151120</Date>
</Album>
<Album>
<Name>1000 Forms of Fear</Name>
<Artist>Sia</Artist>
<Country>USA</Country>
<Company>RCA Records</Company>
<Price>9.90</Price>
<Date>20140704</Date>
</Album>
<Album>
<Name>Rattle and Hum</Name>
<Artist>U2</Artist>
<Country>EU</Country>
<Company>Island</Company>
<Price>9.90</Price>
<Date>19881010</Date>
</Album>
</Albums>
XSL Code:
<?xml version='1.0' encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="country" match="Album" use="Country" />
<xsl:template match="/Albums">
<html>
<body>
<center>
<xsl:for-each select="Album[generate-id() = generate-id(key('country',Country)[1])]">
<u style="color:#39c639" ><h2 id="{generate-id(Country)}">
<xsl:value-of select="Country" /></h2></u>
<table border="2" bgcolor="transparent">
<tr bgcolor="grey">
<th>Artist</th>
<th>Album</th>
<th>Company</th>
<th>Link</th>
<th>Price</th>
</tr>
<xsl:for-each select="key('country',Country)">
<xsl:sort select="Date" order="ascending"/>
<tr>
<td style="color:#ff0000"><xsl:value-of select="Artist" /></td>
<td><xsl:value-of select="Name" /></td>
<td><xsl:value-of select="Company" /></td>
<td><a href="https://en.wikipedia.org/wiki/Empire_Burlesque">Click Me</a>
</td>
<td><xsl:value-of select="Price" /></td>
</tr>
</xsl:for-each>
<tr >
<td bgcolor="yellow">Total Price: </td><td ></td><td ></td><td ></td>
<td bgcolor="yellow"><xsl:value-of select="sum(key('country',
Country)/Price)" /></td>
</tr>
<tr >
<td bgcolor="yellow">Total Albums: </td><td ></td><td ></td><td ></td>
<td bgcolor="yellow"><xsl:value-of select="count(key('country', Country)/.)"
/> </td>
</tr>
</table>
</xsl:for-each>
</center>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Here the Result:
Upvotes: 0
Views: 43
Reputation: 86
Not sure if I understood correctly but I think this may work:
<?xml version='1.0' encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="country" match="Album" use="Country" />
<xsl:template match="/Albums">
<html>
<body>
<center>
<xsl:for-each select="Album[generate-id() = generate-id(key('country',Country)[1])]">
<u style="color:#39c639" ><h2 id="{generate-id(Country)}">
<xsl:value-of select="Country" /></h2></u>
<table border="2" bgcolor="transparent">
<tr bgcolor="grey">
<th>Artist</th>
<th>Album</th>
<th>Company</th>
<th>Link</th>
<th>Price</th>
</tr>
<xsl:for-each select="key('country',Country)">
<xsl:sort select="Date" order="ascending"/>
<tr>
<td style="color:#ff0000"><xsl:value-of select="Artist" /></td>
<td><xsl:value-of select="Name" /></td>
<td><xsl:value-of select="Company" /></td>
<td><a href="{concat('https://en.wikipedia.org/wiki/', translate(Name, ' ', '_'))}">Click Me</a>
</td>
<td><xsl:value-of select="Price" /></td>
</tr>
</xsl:for-each>
<tr >
<td bgcolor="yellow">Total Price: </td><td ></td><td ></td><td ></td>
<td bgcolor="yellow"><xsl:value-of select="sum(key('country',
Country)/Price)" /></td>
</tr>
<tr >
<td bgcolor="yellow">Total Albums: </td><td ></td><td ></td><td ></td>
<td bgcolor="yellow"><xsl:value-of select="count(key('country', Country)/.)"
/> </td>
</tr>
</table>
</xsl:for-each>
</center>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1