XSLT for-each循环分频器元素?

时间:2016-05-16 03:23:05

标签: xml xslt html-table

我目前正在尝试为我的公司SharePoint页面为以下XML编写一个XSLT,但目前仍然存在。

我希望以表格的形式呈现数据 here

我能够正确显示日期,但不能显示表格中的其他数据。请指导我,因为我是这个xslt编程的总菜鸟,第一次尝试编写xslt代码。

以下是XML:

<?xml version="1.0"?>
<channel>
  <title>Singapore - Nowcast and Forecast </title>
  <source>Meteorological Service Singapore</source>
  <item>
    <title>4 Day Forecast</title>
    <forecastIssue time="04:41 AM" date="16-May-2016"/>
    <weatherForecast>
      <day>Tuesday</day>
      <forecast>Warm. Afternoon thundery showers.</forecast>
      <icon>TL</icon>
      <temperature unit="Degrees Celsius" low="25" high="35"/>
      <relativeHumidity unit="Percentage" low="55" high="95"/>
      <wind speed="5 - 20" direction="SE"/>
      <day>Wednesday</day>
      <forecast>Late morning and early afternoon thundery showers.</forecast>
      <icon>TL</icon>
      <temperature unit="Degrees Celsius" low="25" high="34"/>
      <relativeHumidity unit="Percentage" low="55" high="95"/>
      <wind speed="10 - 20" direction="S"/>
      <day>Thursday</day>
      <forecast>Late morning and early afternoon thundery showers.</forecast>
      <icon>TL</icon>
      <temperature unit="Degrees Celsius" low="25" high="34"/>
      <relativeHumidity unit="Percentage" low="55" high="95"/>
      <wind speed="10 - 20" direction="S"/>
      <day>Friday</day>
      <forecast>Late morning and early afternoon thundery showers.</forecast>
      <icon>TL</icon>
      <temperature unit="Degrees Celsius" low="25" high="34"/>
      <relativeHumidity unit="Percentage" low="55" high="95"/>
      <wind speed="10 - 20" direction="SW"/>
    </weatherForecast>
  </item>
</channel>

这是我写的XSLT:

<?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>
    <h2>
      <xsl:value-of select="channel/item/title"/>
    </h2>
    <table border="1">
      <tr bgcolor="#FFCF00">
        <th>Day</th>
        <th>Icon</th>
        <th>Forecast</th>
        <th>Low Temp.</th>
        <th>High Temp.</th>
      </tr>
      <xsl:for-each select="channel/item/weatherForecast/day">
        <tr>
          <td>
            <xsl:value-of select="."/>
          </td>
          <td>
            <xsl:value-of select="ancestor::weatherForecast/icon"/>
          </td>
          <td>
            <xsl:value-of select="ancestor::weatherForecast/forecast"/>
          </td>
          <td>
            <xsl:value-of select="ancestor::weatherForecast/temperature/@low"/>
            <xsl:text>°C </xsl:text>
          </td>
          <td>
            <xsl:value-of select="ancestor::weatherForecast/temperature/@high"/>
            <xsl:text>°C </xsl:text>
          </td>
        </tr>
      </xsl:for-each>
    </table>
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

根据您的XML,您希望跟随兄弟到每个day元素。

所以,而不是

<xsl:value-of select="ancestor::weatherForecast/icon"/>

使用

<xsl:value-of select="following-sibling::icon[1]"/>

您可以对forecasttemperature元素进行类似的更改。