Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT expression to check if variable belongs to set of elements

Tags:

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

like image 657
majkinetor Avatar asked Jun 17 '09 13:06

majkinetor


People also ask

How do I write an if statement in XSLT?

The <xsl:if> Element To put a conditional if test against the content of the XML file, add an <xsl:if> element to the XSL document.

What is current group () in XSLT?

Returns the contents of the current group selected by xsl:for-each-group. Available in XSLT 2.0 and later versions. Available in all Saxon editions. current-group() ➔ item()*

What is Number () in XSLT?

Definition and Usage The <xsl:number> element is used to determine the integer position of the current node in the source. It is also used to format a number.


1 Answers

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> 
like image 115
Tomalak Avatar answered Sep 28 '22 11:09

Tomalak