如何使用 XSL 合并同一组的单元格?
How do I merge cells of the same group using XSL?
我刚开始学习 XSLT,在尝试合并同一组的单元格时 运行 遇到了一个小问题。
到目前为止,这是我使用以下代码得到的结果:
我的问题是:如何合并 18/02/2020 和 19/02/2020 的单元格,因为它们是同一个月?
非常感谢您的帮助!
我当前的 XML 数据集:
<?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>
这是我的 xsl 样式表:
<?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>
听起来好像您只是想将模板中的代码减少到
<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>
我刚开始学习 XSLT,在尝试合并同一组的单元格时 运行 遇到了一个小问题。
到目前为止,这是我使用以下代码得到的结果:
我的问题是:如何合并 18/02/2020 和 19/02/2020 的单元格,因为它们是同一个月?
非常感谢您的帮助!
我当前的 XML 数据集:
<?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>
这是我的 xsl 样式表:
<?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>
听起来好像您只是想将模板中的代码减少到
<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>