我对XML和XSLT文档还比较陌生。我创建了一个XML文档,其中包含跨越几个月的天气数据记录。我想使用XSLT文档将每个表行中同一月份的记录分组,并在跨越同一月份所有行的列上打印它们的月份(如果月份=2,则打印Feb)。我是否可以使用if-else语句来输出相应的月份并如何合并具有相同月份值的表行?
XML:
这是我目前的XSL文件:
我试图实现的输出结果如下所示:
XML:
<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="S3(1).xsl" ?>
<forecast xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="S3.xsd"
qTime="28/10/20 10:00 PM">
<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="20200710">
<year>2020</year>
<month>07</month>
<date>10</date>
<comment>Partly sunny</comment>
<code>partlySunny</code>
<highest>33.1</highest>
<lowest>29.2</lowest>
</weather>
<weather yyyymmdd="20200616">
<year>2020</year>
<month>06</month>
<date>16</date>
<comment>Considerable clouds</comment>
<code>cloudy</code>
<highest>30.5</highest>
<lowest>25.4</lowest>
</weather>
<weather yyyymmdd="20200612">
<year>2020</year>
<month>06</month>
<date>12</date>
<comment>Cloudy with a thunderstorm</comment>
<code>thunderstorm</code>
<highest>29.1</highest>
<lowest>23.2</lowest>
</weather>
<weather yyyymmdd="20200421">
<year>2020</year>
<month>04</month>
<date>21</date>
<comment>Plenty of sunshine</comment>
<code>sunny</code>
<highest>32.2</highest>
<lowest>29.8</lowest>
</weather>
<weather yyyymmdd="20200628">
<year>2020</year>
<month>06</month>
<date>28</date>
<comment>A morning shower, then rain</comment>
<code>rain</code>
<highest>30.2</highest>
<lowest>22.7</lowest>
</weather>
<weather yyyymmdd="20200502">
<year>2020</year>
<month>05</month>
<date>02</date>
<comment>Cloudy with a thunderstorm</comment>
<code>thunderstorm</code>
<highest>28.1</highest>
<lowest>26.9</lowest>
</weather>
<weather yyyymmdd="20200428">
<year>2020</year>
<month>04</month>
<date>28</date>
<comment>A morning shower</comment>
<code>rain</code>
<highest>28.8</highest>
<lowest>22.2</lowest>
</weather>
<weather yyyymmdd="20200410">
<year>2020</year>
<month>04</month>
<date>10</date>
<comment>Partly sunny</comment>
<code>partlySunny</code>
<highest>33.7</highest>
<lowest>29.3</lowest>
</weather>
<weather yyyymmdd="20200730">
<year>2020</year>
<month>07</month>
<date>30</date>
<comment>Plenty of sunshine</comment>
<code>sunny</code>
<highest>32.3</highest>
<lowest>28.4</lowest>
</weather>
<weather yyyymmdd="20200706">
<year>2020</year>
<month>07</month>
<date>06</date>
<comment>Plenty of sunshine</comment>
<code>sunny</code>
<highest>34.5</highest>
<lowest>30.6</lowest>
</weather>
</forecast>
这是我目前的XSL文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="forecast">
<h1>
<span> <xsl:value-of select="@qLocation"/></span>
<span>[<xsl:value-of select="@qTime"/>]</span>
</h1>
<table style="border:1px solid black;">
<tr>
<th>Date</th>
<th>Weather data</th>
<th>Highest</th>
<th>Lowest</th>
</tr>
<xsl:for-each select="weather">
<xsl:sort select="month" order="ascending"/>
<xsl:sort select="date" order="ascending"/>
<tr>
<td>
<span><xsl:value-of select="date"/>/</span>
<span><xsl:value-of select="month"/></span>
</td>
<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>
<td>
<xsl:value-of select="highest"/>C
<!-- <img src="img1.jpg"> -->
</td>
<td>
<xsl:value-of select="lowest"/>C
<!-- <img src="img2.jpg"> -->
</td>
</tr>
</xsl:for-each>
</table>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我试图实现的输出结果如下所示:
xsl:for-each-group
会让事情变得更容易,但在1.0中不可用。 - Mads Hansenxsl:for-each-group
并按其月份值对行进行排序。 - user14512303