如何抓住xml中的第一个温度为高,第二个温度为低

时间:2014-07-18 13:32:25

标签: xml xslt xslt-1.0 weather

提供的xml是每天2天的预测。我需要将其他所有的特征设置为高温,其余的设置为低温。我是初学者,想知道我怎么能在xsl中这样做。我想在一天中做一些事情,但不确定。 感谢您的帮助。

以下xml的示例代码。

`

<forecast>
-<dailyForecastPeriods>
<cloudCoverPercent>58</cloudCoverPercent>
<dewPoint>51.8</dewPoint>
<iconCode>3</iconCode>
<precipCode>1</precipCode>
<precipProbability>0</precipProbability>
<relativeHumidity>50</relativeHumidity>
<summaryDescription>Partly Cloudy</summaryDescription>
<temperature>78</temperature>
<thunderstormProbability>0</thunderstormProbability><windDirectionDegrees>92</windDirectionDegrees>
<windSpeed>4.5</windSpeed>
<detailedDescription>Partly cloudy. Highs in the upper 70s. Southeast winds around 5 mph...Becoming east around 5 mph this afternoon. </detailedDescription><forecastDateLocalStr>2014-07-18T07:00:00</forecastDateLocalStr><forecastDateUtcStr>2014-07-18T11:00:00Z</forecastDateUtcStr><isNightTimePeriod>false</isNightTimePeriod>
</dailyForecastPeriods>-<dailyForecastPeriods>
<cloudCoverPercent>98</cloudCoverPercent>
<dewPoint>59</dewPoint>
<iconCode>81</iconCode>
<precipCode>1</precipCode>
<precipProbability>75</precipProbability>
<relativeHumidity>65</relativeHumidity>
<summaryDescription>30% Chance of Rain</summaryDescription>
<temperature>60</temperature>
<thunderstormProbability>10</thunderstormProbability><windDirectionDegrees>99</windDirectionDegrees>
<windSpeed>6.7</windSpeed>
<detailedDescription>Partly cloudy in the evening...Then mostly cloudy with a chance of showers after midnight. Lows around 60. East winds 5 to 10 mph...Becoming southeast after midnight. Chance of rain 30 percent. </detailedDescription><forecastDateLocalStr>2014-07-18T19:00:00</forecastDateLocalStr><forecastDateUtcStr>2014-07-18T23:00:00Z</forecastDateUtcStr><isNightTimePeriod>true</isNightTimePeriod>
</dailyForecastPeriods>-<dailyForecastPeriods>
<cloudCoverPercent>83</cloudCoverPercent>
<dewPoint>64.4</dewPoint>
<iconCode>89</iconCode>
<precipCode>1</precipCode>
<precipProbability>75</precipProbability>
<relativeHumidity>79</relativeHumidity>
<summaryDescription>50% Chance of Rain</summaryDescription>
<temperature>80</temperature>
<thunderstormProbability>30</thunderstormProbability><windDirectionDegrees>175</windDirectionDegrees>
<windSpeed>8.9</windSpeed>
<detailedDescription>Cloudy with a chance of showers. Highs in the upper 70s. Southeast winds 5 to 10 mph. Chance of rain 50 percent. </detailedDescription><forecastDateLocalStr>2014-07-19T07:00:00</forecastDateLocalStr><forecastDateUtcStr>2014-07-19T11:00:00Z</forecastDateUtcStr><isNightTimePeriod>false</isNightTimePeriod>
</dailyForecastPeriods>-<dailyForecastPeriods>
<cloudCoverPercent>62</cloudCoverPercent>
<dewPoint>64.4</dewPoint>
<iconCode>82</iconCode>
<precipCode>1</precipCode>
<precipProbability>45</precipProbability>
<relativeHumidity>84</relativeHumidity>
<summaryDescription>40% Chance of Rain</summaryDescription>
<temperature>64</temperature>
<thunderstormProbability>10</thunderstormProbability><windDirectionDegrees>169</windDirectionDegrees>
<windSpeed>4.5</windSpeed>
<detailedDescription>Mostly cloudy with a chance of showers. Lows in the lower 60s. Southeast winds 5 to 10 mph. Chance of rain 40 percent. </detailedDescription><forecastDateLocalStr>2014-07-19T19:00:00</forecastDateLocalStr><forecastDateUtcStr>2014-07-19T23:00:00Z</forecastDateUtcStr><isNightTimePeriod>true</isNightTimePeriod>
</dailyForecastPeriods>
</forecast>

`

Sample of xsl
`

<xsl:template match="//forecast">
    <xsl:choose>
      <xsl:when test="//CUSTOM/WEATHER/TYPE='Compact'" />
      <xsl:otherwise>
        <xsl:if test="//CUSTOM/WEATHER/HIDE_EXTFORECAST='false'">
          <div class="rnd_grouping" style="cursor:pointer;text-align:center;" onclick="javascript:ShowHideSubmodule('extendedForecast', 'upDownForecast', '//MODULE[@id=\'B253588A-437D-4C8E-8BB4-CACA5506567D\']/SUBMODULE/@expanded', true);">
            <xsl:text>Extended Forecast</xsl:text>
          </div>
          <table width="100%" border="0" cellSpacing="0" cellPadding="0" id="extendedForecast">
            <xsl:variable name="startIndex">
              <xsl:choose>
                <xsl:when test="//CUSTOM/WEATHER/SHOWTODAY='true'">
                  0
                </xsl:when>
                <xsl:otherwise>
                  1
                </xsl:otherwise>
              </xsl:choose>
            </xsl:variable>
            <xsl:variable name="showToday">
              <xsl:choose>
                <xsl:when test="//CUSTOM/WEATHER/SHOWTODAY='true'">
                  0
                </xsl:when>
                <xsl:otherwise>
                  1
                </xsl:otherwise>
              </xsl:choose>
            </xsl:variable>
            <xsl:variable name="numDays">
              <xsl:choose>
                <xsl:when test="$devicetype='mobile'">

                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="//CUSTOM/WEATHER/NUMDAYS"/>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:variable>
            <xsl:if test="//SUBMODULE[@type='Weather']/@expanded=0">
              <xsl:attribute name="style">
                display:none;
              </xsl:attribute>
            </xsl:if>
            <tr>
              <xsl:for-each select="//forecast/dailyForecastPeriods">
                <xsl:if test="position() > $startIndex">
                  <xsl:if test="position() &lt;= ($numDays)+$startIndex">
                    <td title="{detailedDescription}" align="center" valign="top" class="weatherforecastday">
                      <xsl:attribute name="width">
                        <xsl:value-of select="(100 div ($numDays)-$showToday)"/>
                        <xsl:text disable-output-escaping="yes">%</xsl:text>
                      </xsl:attribute>
                      <xsl:choose>
                        <xsl:when test="position()=1">Today</xsl:when>
                        <xsl:otherwise>
                          <!--<xsl:value-of select="dayoftheweek"></xsl:value-of>-->
                        </xsl:otherwise>
                      </xsl:choose>
                    </td>
                  </xsl:if>
                </xsl:if>
              </xsl:for-each>
            </tr>
            <tr>
              <xsl:for-each select="//forecast/dailyForecastPeriods">
                <xsl:if test="position() > $startIndex">
                  <xsl:if test="position() &lt;= ($numDays)+$startIndex">
                    <td title="{detailedDescription}" align="center" valign="top" class="weatherforecastimage">
                      <xsl:attribute name="width">
                        <xsl:value-of select="(100 div ($numDays)-$showToday)"/>
                        <xsl:text disable-output-escaping="yes">%</xsl:text>
                      </xsl:attribute>
                      <img src="{img_small}" />
                    </td>
                  </xsl:if>
                </xsl:if>
              </xsl:for-each>
            </tr>
            <tr>
              <xsl:for-each select="//forecast/dailyForecastPeriods">
                <xsl:if test="position() > $startIndex">
                  <xsl:if test="position() &lt;= ($numDays)+$startIndex">
                    <td title="{detailedDescription}" align="center" valign="top" class="weatherforecasttext">
                      <xsl:attribute name="width">
                        <xsl:value-of select="(100 div ($numDays)-$showToday)"/>
                        <xsl:text disable-output-escaping="yes">%</xsl:text>
                      </xsl:attribute>
                      <xsl:value-of select="summaryDescription"></xsl:value-of>
                    </td>
                  </xsl:if>
                </xsl:if>
              </xsl:for-each>
            </tr>
            <tr>
              <xsl:for-each select="//forecast/dailyForecastPeriods">
                <xsl:if test="position() > $startIndex">
                  <xsl:if test="position() &lt;= ($numDays)+$startIndex">
                    <td title="{detailedDescription}" align="center" valign="top" class="weatherforecasttemp">
                      <span class="weatherforecasthigh">
                        <xsl:value-of select="temperature[1]"/>
                        <xsl:text>°</xsl:text>                     
                      </span>
                      <xsl:text>&#160;</xsl:text>
                      <span class="weatherforecastlow">
                        <xsl:value-of select="temperature[2]" />
                        <xsl:text>°</xsl:text>
                      </span>
                    </td>
                  </xsl:if>
                </xsl:if>
              </xsl:for-each>
            </tr>

`

1 个答案:

答案 0 :(得分:0)

  

提供的xml是每天2天的预测。我需要设置每一个   其他温度到高温,其余为低温。

这可以通过以下方式轻松实现:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
    <table>
        <tr>
            <th>Date</th>
            <th>High</th>
            <th>Low</th>
        </tr>
        <tr>
            <td><xsl:value-of select="substring-before(forecast/dailyForecastPeriods[1]/forecastDateLocalStr, 'T')"/></td>
            <td><xsl:value-of select="forecast/dailyForecastPeriods[1]/temperature"/></td>
            <td><xsl:value-of select="forecast/dailyForecastPeriods[2]/temperature"/></td>
        </tr>
        <tr>
            <td><xsl:value-of select="substring-before(forecast/dailyForecastPeriods[3]/forecastDateLocalStr, 'T')"/></td>
            <td><xsl:value-of select="forecast/dailyForecastPeriods[3]/temperature"/></td>
            <td><xsl:value-of select="forecast/dailyForecastPeriods[4]/temperature"/></td>
        </tr>
    </table>
</xsl:template>

</xsl:stylesheet>

或者如果你愿意(这将容纳任何偶数的dailyForecastPeriods):

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
    <table>
        <tr>
            <th>Date</th>
            <th>High</th>
            <th>Low</th>
        </tr>
        <xsl:for-each select="forecast/dailyForecastPeriods[position() mod 2 = 1]">
            <tr>
                <td><xsl:value-of select="substring-before(forecastDateLocalStr, 'T')"/></td>
                <td><xsl:value-of select="temperature"/></td>
                <td><xsl:value-of select="following-sibling::dailyForecastPeriods[1]/temperature"/></td>
            </tr>
        </xsl:for-each>     
    </table>
</xsl:template>

</xsl:stylesheet>

根据您编辑的输入,结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<table>
   <tr>
      <th>Date</th>
      <th>High</th>
      <th>Low</th>
   </tr>
   <tr>
      <td>2014-07-18</td>
      <td>78</td>
      <td>60</td>
   </tr>
   <tr>
      <td>2014-07-19</td>
      <td>80</td>
      <td>64</td>
   </tr>
</table>

呈现为:

enter image description here