2016-05-16 105 views
1

我目前正在嘗試爲我的公司SharePoint頁面編寫以下XML的XSLT,但目前有些卡住了。XSLT for-each loop over divider elements?

我想呈現數據以表格的形式像 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

<?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> 

回答

0

,你想要的以下同級到每個day元素。

所以,與其

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

使用

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

也可以做類似的變化爲forecasttemperature元素。

+0

感謝您的幫助。 – Xpose