2014-09-04 48 views
-1

xml我有白天和黑夜時間,所以我需要使用mod 2 = 1來獲取每個集合日期的奇數dailyforecast。但是當我這樣做時,它給了我整個10天的預測,我只想要5天。要設置5天,我知道我必須使用position &lt; 5,但我不知道如何暗示與mod 2 = 1。任何幫助都可以做到,希望這個細節足夠詳細。以實施如何設置位置爲< 5和mod 2 = 1

<forecast> 
     <dailyforecast> 
     <hightemp> 
     </hightemp> 
     </dailyforecast> 
     <lowtemp> 
     </lowtemp> 
     <dailyforecast> 
     <hightemp> 
     </hightemp> 
     </dailyforecast> 
     <dailyforecast> 
     <lowtemp> 
     </lowtemp> 
     </dailyforecast> 
     <dailyforecast> 
     <hightemp> 
     </hightemp> 
     </dailyforecast> 
     <dailyforecast> 
     <lowtemp> 
     </lowtemp> 
     </dailyforecast> 
     <dailyforecast> 
     <hightemp> 
     </hightemp> 
     </dailyforecast> 
     <dailyforecast> 
     <lowtemp> 
     </lowtemp> 
     </dailyforecast> 
     <dailyforecast> 
     <hightemp> 
     </hightemp> 
     </dailyforecast> 
     <dailyforecast> 
     <lowtemp> 
     </lowtemp> 
     </dailyforecast> 
     <dailyforecast> 
     <hightemp> 
     </hightemp> 
     </dailyforecast> 
</forecast> 

回答

0

這XPATH應該工作:

/forecast/dailyforecast[position() < 10 and position() mod 2 =1] 
+0

並不讓我使用'<' – smiller48 2014-09-04 15:42:00

+0

但它的確讓我用'的'謝謝我只是不知道該怎麼寫它! – smiller48 2014-09-04 15:45:11

0

一種方法是:

<xsl:template match="forecast"> 
    <xsl:apply-templates mode="top" select="dailyforecast[position() mod 2 = 1]"> 
    <xsl:with-param name="count" select="5" /> 
    </xsl:apply-templates> 
</xsl:template> 

<!-- generic TOP template --> 
<xsl:template match="*" mode="top"> 
    <xsl:param name="count" select="1" /> 
    <xsl:if test="position() &lt;= $count"> 
    <xsl:apply-templates select="." /> 
    </xsl:if> 
</xsl:template> 

<xsl:template match="dailyforecast"> 
    <!-- format current node (day) and following-sibling::dailyforecast[1] (night) --> 
</xsl:template> 

這工作,因爲position()是指在選擇的節點集的位置。因此,如果您只選擇模板1中的奇數節點,則將在模板2中獲得新的位置計數器。

當然,您可以將通用「頂部」功能摺疊到dailyforecast模板中,但不能重複使用了。

<xsl:template match="dailyforecast"> 
    <xsl:if test="positon() &lt;= 5"> 
    <!-- format current node (day) and following-sibling::dailyforecast[1] (night) --> 
    </xsl:if> 
</xsl:template>