Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The IN function in xslt test boolean expression

Tags:

xml

xslt

can't find the answer anywhere whether you can do test="$var in ('Val1','Val2','Val3')" in XSLT instead of doing test="$var='Val1' or $var='Val2' or $var='Val3'"?

like image 267
Ruslan Avatar asked Feb 01 '26 20:02

Ruslan


1 Answers

In XSLT 1.0, you can use the contains() function:

test(contains('Val1,Val2,Val3',$var))`

It returns a boolean result, testing whether the first string contains the second string.

A common way to help reduce false-positive results for partial string matches is to use a delimiter and pad the values with that delimiter:

test="contains(' Val1 Val2 Val3 ', concat(' ',$var,' '))

That way, if the value of $var was "Val", it would only return true if "Val" were added to the list of values being tested.

In XSLT 2.0, you could use:

test="$var = ('Val1','Val2','Val3')"

It will return true if $var is equal to any of the items in the sequence (which is what you define when you have a comma separated list of values inside of parenthesis).

Another XSLT 2.0 solution:

test="some $value in ('Val1','Val2','Val3') satisfies $var=$value"
like image 195
Mads Hansen Avatar answered Feb 04 '26 08:02

Mads Hansen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!