tags:

views:

1211

answers:

1

Hello

I have code like this:

  <xsl:if test="$k='7' or $k = '8' or $k = '9'">

Is there any way to put this expression in a form, like, for instance SQL

   k IN (7, 8, 9)

Ty :)

+1  A: 

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>

You can use other separators if needed.

In XSLT / XPath 2.0 you could do something like:

<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>

If you can use document structure to define your list, you could do:

<!-- 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>
Tomalak
Ty m8. Its overkill for simple scenario like I have but its OK for long lists... I was more hopping that xpath has some form of integrated solution....
majkinetor
Not XPath 1.0 - when you can't use node-sets to solve your problem you're down to string functions or maybe some extension function. XPath 2.0's sequences make thinks easier in any case.
Tomalak
Ty Tomalak :) .
majkinetor