The IN function in xslt test boolean expression
can't find the answer anywhere whether you can do test="$var in ('Val1','Val2开发者_JS百科','Val3')"
in XSLT instead of doing test="$var='Val1' or $var='Val2' or $var='Val3'"
?
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"
I. XPath 1.0 / XSLT 1.0
Use:
contains(concat($Sep, 'Val1', $Sep, 'Val2', $Sep, 'Val3', $Sep),
concat($Sep, $var, $Sep)
)
where $Sep
is a string that is guaranteed not to appear in any of the values.
The concatenation is nesessary to exclude false positives when one or more of the values start-with or end-with the value of $var
.
The comparison is even easier if 'Val1'
, 'Val2'
and 'Val3'
are text nodes (say of /a/b
):
$var = /a/b
II. XPath 2.0 (XSLT 2.0)
$var = ('Val1', 'Val2', 'Val3')
XSLT/XPath doesn't have support for IN expressions.
But you can to store those values into an XML fragment and search inside it.
精彩评论