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>
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
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
Ty Tomalak :). – majkinetor