1
votes

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: enter image description here

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>
1
Are you struggling to find or know the right HTML structure you want? Or to create that with XSLT? Your XSLT groups on months it seems so it is not clear, if you want one row per month in the HTML result table, you can't change the code to do that. Or at least show the HTML code you want to create with XSLT, not simply screenshots. ā€“ Martin Honnen
Iā€™m currently grouping the comments together that is on the same month. I have attach my full code below to illustrate the output of my current program. I am unsure if how to move on to merge the comments together after increase the rowspan on my Month column (As shown as the first column). Yes and I believe I only want one row per month for all the comments in that month. P.S these are the only two codes i work with. ā€“ z.yea

1 Answers

3
votes

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