XSLT 1.0 Idiom for ternary if?

﹥>﹥吖頭↗ 提交于 2019-12-01 16:11:57

a) Can I automatically get a value of boolean true|false directly from the string 'true|false' ?

b) Is there an (xpath?) construct to map the boolean true|false to '*', '' ?

This is actually an XPath question.

I. XPath 1.0

There are more than one XPath expressions the evaluation of which produces the wanted result:

substring('*', 2 -(@flag = 'true'))

This also answers b) -- notice that the strings 'true' and 'false' aren't boolean values! The only two boolean values are true() and false() and they are not strings.

In the above expression we use the fact that in XPath 1.0 if a boolean is in a context where a number is needed, it is automatically converted to number. By definition:

number(true()) is 1

and

number(false()) is 0

Thus the second argument of the call to substring() above:

2 - (@flag = 'true')

is evaluated as 1 when @flag = 'true' and to 2 otherwise.

A more general XPath 1.0 expression that produces the string $s1 if $val is "x" and produces the string $s2 if $val is "y" :

concat(substring($s1, 1 div ($val = "x")),
       substring($s2, 1 div ($val = "y"))
      )

This produces the string $s1 when $val = "x", the string $s2 when $val = "y", and the empty string -- if none of these two conditions holds.

The above XPath 1.0 expression can be generalized to produce N different string results $s1, $2, ..., $sN exactly when $val is one of the values $v1, $v2, ..., $vN because the function concat() can have any number of arguments.


II. XPath 2.0 (XSLT 2.0)

'*'[current()/@flag = 'true']

And more generally, given 2*N atomic values $s1, $s2, ... $sN and $v1, $v2, ..., $vN, such that all $vi values are different, the result of evaluating this XPath 2.0 expression:

 ($s1, $s2, ..., $sN)[index-of(($v1, $v2, ..., $vN), $v)]

is $sK exactly when $v eq $vK.

You could utilise simple pattern matching in templates for this.

<xsl:template match="change[@flag = 'true']">

<xsl:template match="change">

So, the first one matches true entries, and the other one matches all other cases (which in your cases is just false

So, given the following stylesheet

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="text" indent="yes"/>

   <xsl:template match="change[@flag = 'true']">
      <xsl:text>*&#13;</xsl:text>
   </xsl:template>

   <xsl:template match="change">
      <xsl:text>&#13;</xsl:text>
   </xsl:template>

   <xsl:template match="@*|node()">
      <xsl:apply-templates select="@*|node()"/>
   </xsl:template>
</xsl:stylesheet>

When applied to your sample XML, the following is output

*

*
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!