XSLT 1.0成语三元如果? [英] XSLT 1.0 Idiom for ternary if?

查看:147
本文介绍了XSLT 1.0成语三元如果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个Java程序使用了一个三元,如果到布尔值映射到输出字符串:(一个*进行真,空假字符串)

 公共类三元{
  公共静态无效的主要(字串[] args){
    布尔标志[] = {真,假,真};
    对于(布尔F:标志){
      的System.out.println(F*:?);
    }
  }
}

所以输出为* [空],*。

我有一个输入XML文档,是这样的:

 <?XML版本=1.0&GT?;
<?xml样式表类型=文/ XSL的href =change.xsl&GT?;
<根和GT;
  <改变标志=真/>
  <改变标志=假/>
  <改变标志=真/>
< /根>

和我有真正的映射到'*'和假下面的XSLT模板''(它的工作原理):

 <的xsl:模板匹配=变与GT;
  < XSL:变量名=旗帜选择=翻译(子(@标志,1,1),'TF','*')/>
  <的xsl:value-of的选择=$旗/>
< / XSL:模板>

时有这种更简洁的版本?

一)我可以自动获得布尔值true值|直接从字符串'|假真假的?
B)是否有一个(的XPath)构建映射布尔值true |?假以'*',''


解决方案

  直接从假|

一)我可以自动获得的布尔值true
  字符串'真|假?


  
  ?

B)是否有一个(的XPath)构建映射布尔值true | false来
  '*',''?


这实际上是一个XPath问题

我。 XPath 1.0中

有一个以上的XPath前pressions其中产生所需的结果的评价:

 子(*,2  - (@标志=真))

这也回答了B) - 注意到的字符串真正的'假'不是布尔值!只有两个布尔值真()假(),他们是不是字符串。

在上述前pression我们使用的事实,XPath 1.0中,如果一个布尔是在需要数字的情况下,它会自动转换成数字。根据定义:

数(真()) 1

号(假()) 0

因此​​,调用的第二个参数子()上面:

  2  - (@flag =真)

被评为 1 @flag =真正的 2 其他。

一个更普遍的XPath 1.0 EX pression产生字符串 $ S1 如果 $ VAL ×并产生字符串 $ S2 如果 $ VAL Y

  CONCAT(子($ S1,1格($ VAL =X))
       子($ S2,1格($ VAL =Y))
      )

这生成字符串 $ S1 $ VAL =X,字符串 $ S2 $ VAL =Y和空字符串 - 如果没有这两个条件持有

以上的XPath 1.0 EX pression可以推广到产生N个不同的字符串结果 $ S1 $ 2 ... $ SN 什么时候 $ VAL 是一个值 $ V1 $ V2 ... $ VN <​​/ code>因为函数 CONCAT()可以拥有任意数量的参数。


II。 XPath 2.0中(XSLT 2.0)

 '*'[电流()/ @标志=真]

和更普遍的,给予2 * N原子值 $ S1 $ S2 ... $ SN $ V1 $ V2 ,..., $ VN <​​/ code>,这样所有的 $六值是不同的,评估这个XPath 2.0 EX pression的结果

 ($ S1,S2 $ ...,$ SN)指数的(($ V1,V2 $ ...,$ VN),$ V)]

$ SK 什么时候 $ V $ EQ VK

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 {
  public static void main(String[] args) {
    boolean flags[]={true,false,true};
    for (boolean f : flags) {
      System.out.println(f?"*":"");
    }
  }
}

So the output is *, [empty], *.

I have an input XML document, something like:

<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="change.xsl"?>
<root>
  <change flag="true"/>
  <change flag="false"/>
  <change flag="true"/>
</root>

And I have the following XSLT template which maps true to '*' and false to '' (it works):

<xsl:template match="change">
  <xsl:variable name="flag" select="translate(substring(@flag,1,1),'tf','*')"/>
  <xsl:value-of select="$flag"/>
</xsl:template>

Is there is more concise version of this ?

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 '*', '' ?

解决方案

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.

这篇关于XSLT 1.0成语三元如果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆