<xsl:template match="/">
<xsl:variable name="a" select="'a'"/>
<xsl:variable name="b" select="'b'"/>
<xsl:variable name="c" select="'c'"/>
<xsl:variable name="result">
<xsl:choose>
<xsl:when test="$a !=''">
<xsl:value-of select="$a"/>
</xsl:when>
<xsl:when test="$b !=''">
<xsl:value-of select="$b"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$c"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
The result variable is populated, here i used the xsl choose statements. I just wanted to know if there is any other efficient or simple way to populate the result variable, based on the same logic as xsl choose.
Something like this
<xsl:variable name="result" select="$a | $b | $c" />
The above will work for nodesets, not for variables. Any inputs?xsl 1.0
This is wrong! The above
resultvariable is defined to contain the union of three node-sets — not just one of them, depending on whether some other(s) of them are empty.A correct XPath expression in this case is:
When These three variables aren’t node-sets but strings and if these string values can have any length (not only length of 1), the corresponding XPath expression is similar:
Here we use the fact that:
substring($s, $k)is the empty string''whenever
and
Also:
is
positive Infinity(
false()is converted to 0 when used in arithmetic operations).and by definition
for every integer
$k.Therefore,
and
Finally, if it is guaranteed that the three variables
$a,$band$ccan have as value only a single character, than we can use just: