Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if parameter is one of several values in XSLT [duplicate]

Possible Duplicate:
XSLT expression to check if variable belongs to set of elements

Given a parameter, $name, what is a short and clean way to check if its value is equal to one of a finite number of values?

For example, say I wanted to merge the first 3 of these when tests because their contents were exactly the same.

<choose>
  <when test="$name='Alice'">
  <when test="$name='Bob'">
  <when test="$name='Cindy'">
  <when test="$name='Dave'">
  <otherwise>

Something like test="$name in ['Alice','Bob','Cindy']", except actually valid :P

like image 876
Svish Avatar asked Sep 05 '25 08:09

Svish


2 Answers

With XSLT 2.0 it suffices to use $name = ('Alice', 'Bob', 'Cindy').

like image 140
Martin Honnen Avatar answered Sep 13 '25 05:09

Martin Honnen


Use (both in XSLT 1.0 and in XSLT 2.0):

contains('|Alice|Bob|Cindy|', concat('|', $name, '|'))

and, as Martin Honnen already mentioned, in XPath 2.0 (XSLT 2.0) only, one can simply write:

$name = ($seqOfNames)
like image 42
Dimitre Novatchev Avatar answered Sep 13 '25 04:09

Dimitre Novatchev