2009-06-17 17 views

Odpowiedz

19

XSLT/XPath 1.0:

<!-- a space-separated list of valid values --> 
<xsl:variable name="list" select="'7 8 9'" /> 

<xsl:if test=" 
    contains( 
    concat(' ', $list, ' '), 
    concat(' ', $k, ' ') 
) 
"> 
    <xsl:value-of select="concat('Item ', $k, ' is in the list.')" /> 
</xsl:if> 

W razie potrzeby można użyć innych separatorów.

W XSLT/XPath 2.0 można zrobić coś takiego:

<xsl:variable name="list" select="fn:tokenize('7 8 9', '\s+')" /> 

<xsl:if test="fn:index-of($list, $k)"> 
    <xsl:value-of select="concat('Item ', $k, ' is in the list.')" /> 
</xsl:if> 

Jeśli można użyć struktury dokumentu, aby zdefiniować listę, mógłby zrobić:

<!-- a node-set defining the list of currently valid items --> 
<xsl:variable name="list" select="/some/items[1]/item" /> 

<xsl:template match="/"> 
    <xsl:variable name="k" select="'7'" /> 

    <!-- test if item $k is in the list of valid items --> 
    <xsl:if test="count($list[@id = $k])"> 
    <xsl:value-of select="concat('Item ', $k, ' is in the list.')" /> 
    </xsl:if> 
</xsl:template> 
+0

Ty m8. Jego przesada dla prostego scenariusza, jak to mam, ale jest OK dla długich list ... Byłem bardziej skaczący, że xpath ma jakąś formę zintegrowanego rozwiązania ... – majkinetor

+0

Nie XPath 1.0 - kiedy nie możesz użyć zestawów węzłów do rozwiązania problem polegał na funkcji ciągów lub może jakiejś funkcji rozszerzenia. Sekwencje XPath 2.0 ułatwiają myślenie. – Tomalak

+0

Ty Tomalak :). – majkinetor

1

W XSLT 2.0, można spróbuj użyć operatora zasięgu. Nie jesteś pewien, czy możesz przesyłać do ciągów w linii?

<xsl:if test="$k = string(7 to 9)"> 

Być może odwrotnie i obsadzić numer $k?

<xsl:if test="number($k) = (7 to 9)"> 
Powiązane problemy