Reputation: 73
I just started learning XSLT and I ran into a small issue when trying to merge cells of the same group.
This is what I got so far with the following codes below:
My question is: How I can merge the cells of 18/02/2020 and 19/02/2020 since they are the same month?
Thank you so much for your help !
My current XML dataset:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="weatherScale.xsl"?>
<forecast qTime="28/10/20 10:00 PM" qLocation="Singapore">
<weather yyyymmdd="20200430">
<year>2020</year>
<month>04</month>
<date>30</date>
<comment>Plenty of sunshine</comment>
<code>sunny</code>
<highest>32.6</highest>
<lowest>28.4</lowest>
</weather>
<weather yyyymmdd="20200218">
<year>2020</year>
<month>02</month>
<date>18</date>
<comment>Plenty of sunshine</comment>
<code>sunny</code>
<highest>34.6</highest>
<lowest>30.5</lowest>
</weather>
<weather yyyymmdd="20200219">
<year>2020</year>
<month>02</month>
<date>19</date>
<comment>Raining cats and dogs</comment>
<code>thunderstorm</code>
<highest>30.6</highest>
<lowest>25.4</lowest>
</weather>
</forecast>
This is my xsl stylesheet:
<?xml version="1.0"?>
<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" encoding="UTF-8"/>
<xsl:key name="yearGrpBy" match="weather" use="month"/>
<xsl:template match="/forecast">
<html>
<head>
<title>Forecast</title>
</head>
<body>
<h1> <xsl:value-of select="@qLocation"/>
[<xsl:value-of select="@qTime"/>] </h1>
<table border="1" style="border:1px solid black;">
<xsl:for-each select="//weather[generate-id(.)=generate-id(key('yearGrpBy', month)[1])]">
<xsl:sort select="month"/>
<xsl:for-each select="key('yearGrpBy', month)">
<xsl:sort select="day"/>
<tr>
<xsl:if test="position() = 1">
<td>
<xsl:attribute name="rowspan">
<xsl:value-of select="count(key('yearGrpBy', month))"/>
</xsl:attribute>
<xsl:value-of select="month"/>
</td>
</xsl:if>
<td>
<li>
<xsl:value-of select="date"/>/
<xsl:value-of select="month"/>/
<xsl:value-of select="year"/>,
from
<xsl:value-of select="lowest"/>C
to
<xsl:value-of select="highest"/>C,
<xsl:value-of select="comment"/>
</li>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1
Views: 379
Reputation: 167561
It sounds as if you simply want to reduce the code in the template to
<xsl:template match="/forecast">
<html>
<head>
<title>Forecast</title>
</head>
<body>
<h1> <xsl:value-of select="@qLocation"/>
[<xsl:value-of select="@qTime"/>] </h1>
<table border="1" style="border:1px solid black;">
<xsl:for-each select="//weather[generate-id(.)=generate-id(key('yearGrpBy', month)[1])]">
<xsl:sort select="month"/>
<tr>
<th>
<xsl:value-of select="month"/>
</th>
<td>
<ul>
<xsl:for-each select="key('yearGrpBy', month)">
<xsl:sort select="day"/>
<li>
<xsl:value-of select="date"/>/
<xsl:value-of select="month"/>/
<xsl:value-of select="year"/>,
from
<xsl:value-of select="lowest"/>C
to
<xsl:value-of select="highest"/>C,
<xsl:value-of select="comment"/>
</li>
</xsl:for-each>
</ul>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
https://xsltfiddle.liberty-development.net/ei5R4u8
Upvotes: 3