2017-04-11 48 views
0

請原諒我缺乏XSLT的經驗。
這是我的XSL樣本:我正在尋找一種替代方案來實現XSL

<xsl:when test="/can/be/a/very/long/location/path/@value > 0 "> 
    <xsl:value-of select="/can/be/a/very/long/location/path/@value" /> 
</xsl:when> 

這似乎是<value-of select="...">可以更簡潔。
這怎麼可能呢?

回答

0

對於這個特殊的例子,你可以簡單地寫:

<xsl:value-of select="/can/be/a/very/long/location/path/@value[. > 0]" /> 

如果沒有值選擇

+0

不錯,輸出什麼,什麼時期在這種情況下,是什麼意思? – Rod

+0

它指的是上下文項。方括號內的謂詞就像一個函數,它應用於選定序列中的每個項目以返回true或false,並且「。」是函數的參數,也就是被測試的項目。 –

2

這也可以做這樣的 -

<xsl:variable name="location" select="/can/be/a/very/long/location/path/@value"/> 

<xsl:if test="$location"> 
    <xsl:value-of select="$location" /> 
</xsl:if> 

<xsl:when>當你有多個條件,這樣做的正確方法大多采用的是

<xsl:choose> 
    <xsl:when test="condition1"> </xsl:when> 
    <xsl:when test="condition2"> </xsl:when> 
    <xsl:when test="condition3"> </xsl:when> 
    <xsl:otherwise> </xsl:otherwise> 
</xsl:choose> 

感謝

2

你可以簡單地使用帶謂詞的<xsl:template>執行此任務:

<xsl:template match="/can/be/a/very/long/location/path[@value &gt; 0]"> 
    <xsl:value-of select="@value" />  <!-- current axis is 'path' --> 
</xsl:template>