Xml 如何使用XSL合并同一组的单元格?
我刚开始学习XSLT,在尝试合并同一组的单元格时遇到了一个小问题 这是我目前使用以下代码得到的结果: 我的问题是:我如何合并2020年2月18日和2020年2月19日的单元格,因为它们是同一个月 非常感谢你的帮助 我当前的XML数据集:Xml 如何使用XSL合并同一组的单元格?,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,我刚开始学习XSLT,在尝试合并同一组的单元格时遇到了一个小问题 这是我目前使用以下代码得到的结果: 我的问题是:我如何合并2020年2月18日和2020年2月19日的单元格,因为它们是同一个月 非常感谢你的帮助 我当前的XML数据集: <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="weatherScale.xsl"?> <fo
<?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>
听起来好像您只是想将模板中的代码简化为
<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>
您是否在努力寻找或了解所需的正确HTML结构?还是用XSLT创建它?您的XSLT按月份分组似乎不太清楚,如果您希望在HTML结果表中每个月有一行,则无法更改代码来实现这一点。或者至少显示您希望使用XSLT创建的HTML代码,而不仅仅是屏幕截图。我目前正在将同一月份的注释分组在一起。我在下面附上了我的完整代码,以说明我当前程序的输出。我不确定在增加“我的月份”列上的行跨度(如第一列所示)后,是否继续合并注释。是的,我相信我每个月只想要一行的评论。这是我唯一使用的两个代码。你是否在努力寻找或知道你想要的正确的HTML结构?还是用XSLT创建它?您的XSLT按月份分组似乎不太清楚,如果您希望在HTML结果表中每个月有一行,则无法更改代码来实现这一点。或者至少显示您希望使用XSLT创建的HTML代码,而不仅仅是屏幕截图。我目前正在将同一月份的注释分组在一起。我在下面附上了我的完整代码,以说明我当前程序的输出。我不确定在增加“我的月份”列上的行跨度(如第一列所示)后,是否继续合并注释。是的,我相信我每个月只想要一行的评论。注:这是我唯一使用的两种代码。它有效!非常感谢你的建议,马丁!它起作用了!非常感谢你的建议,马丁!