XSLT 1.0 Idiom for ternary if?

前端 未结 2 1784
醉梦人生
醉梦人生 2021-01-17 23:53

This Java program makes use of a Ternary if, to map booleans to output strings: (a \"*\" for true, an empty string for false).

public class ternary {
  publi         


        
相关标签:
2条回答
  • 2021-01-18 00:16

    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.

    0 讨论(0)
  • 2021-01-18 00:19

    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

    *
    
    *
    
    0 讨论(0)
提交回复
热议问题