【发布时间】:2021-12-14 13:00:28
【问题描述】:
我正在尝试根据日期将 XML 中的天气详细信息显示到表格中。例如。 6 月 12 日星期六是 23-28 度等。但是,我无法将详细信息插入正确的列。我曾尝试使用 xsl:if 和 xsl:when 但无济于事。我不确定使用哪个函数将其指向正确的日期。
Sample output on how it should look like
XML 文件
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type = "text/xsl" href = "b2.xsl"?>
<forecast queryTime="30/7/2021 14:10:20" queryLocation=" Singapore ">
<weather yyyymmdd="20210617">
<year>2021</year>
<month>6</month>
<date>17</date>
<dayOfWeek>Thu</dayOfWeek>
<overall>Considerable clouds</overall>
<overallCode>cloudy</overallCode>
<highest>29</highest>
<lowest>19</lowest>
</weather>
<weather yyyymmdd="20210612">
<year>2021</year>
<month>6</month>
<date>12</date>
<dayOfWeek>Sat</dayOfWeek>
<overall>Cloudy with a thunderstorm</overall>
<overallCode>thunderstorm</overallCode>
<highest>28</highest>
<lowest>23</lowest>
</weather>
<weather yyyymmdd="20210709">
<year>2021</year>
<month>7</month>
<date>09</date>
<dayOfWeek>Fri</dayOfWeek>
<overall>A morning shower, then rain</overall>
<overallCode>rain</overallCode>
<highest>29</highest>
<lowest>23</lowest>
</weather>
<weather yyyymmdd="20210601">
<year>2021</year>
<month>6</month>
<date>01</date>
<dayOfWeek>Tue</dayOfWeek>
<overall>Partly sunny</overall>
<overallCode>partlySunny</overallCode>
<highest>31</highest>
<lowest>28</lowest>
</weather>
<weather yyyymmdd="20210802">
<year>2021</year>
<month>8</month>
<date>02</date>
<dayOfWeek>Mon</dayOfWeek>
<overall>Plenty of sunshine</overall>
<overallCode>sunny</overallCode>
<highest>35</highest>
<lowest>24</lowest>
</weather>
</forecast>
<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:template match = "/forecast">
<html>
<body>
<h1>
<xsl:value-of select="@queryLocation"/>
[ <xsl:value-of select="@queryTime"/>
]
</h1>
<table border="1" width="100%">
<tr id="days" bgcolor="#FFA500" align="center">
<th>Date</th>
<th>Mon</th>
<th>Tue</th>
<th>Wed</th>
<th>Thu</th>
<th>Fri</th>
<th>Sat</th>
<th>Sun</th>
</tr>
<xsl:for-each select = "weather">
<xsl:sort select="@yyyymmdd"/>
<tr>
<td bgcolor="#FFA500" align="center">
<xsl:value-of select = "date "/>
<xsl:variable name="month" select="month"/>
<xsl:choose>
<xsl:when test="$month=1"> January</xsl:when>
<xsl:when test="$month=2"> February</xsl:when>
<xsl:when test="$month=3"> March</xsl:when>
<xsl:when test="$month=4"> April</xsl:when>
<xsl:when test="$month=5"> May</xsl:when>
<xsl:when test="$month=6"> June</xsl:when>
<xsl:when test="$month=7"> July</xsl:when>
<xsl:when test="$month=8"> August</xsl:when>
<xsl:when test="$month=9"> September</xsl:when>
<xsl:when test="$month=10"> October</xsl:when>
<xsl:when test="$month=11"> November</xsl:when>
<xsl:when test="$month=12"> December</xsl:when>
</xsl:choose>
</td>
<tr>
<td>
<xsl:for-each select = "weather"/>
<xsl:value-of select = " lowest "/>
<f>°</f>
<span>- </span>
<xsl:value-of select = " highest "/>
<f>°</f>
</td>
</tr>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
【问题讨论】:
-
您的 XSL 嵌套了
tr标签 -
这个问题(或者更确切地说是作业)之前提出过:stackoverflow.com/q/67651265/3016153 当时不是很清楚,现在仍然不是。但是您可能会在这些 cmets 中找到一些指针。