xpath - XSLT 1.0 Idiom for ternary if? -
xpath - XSLT 1.0 Idiom for ternary if? -
this java programme makes utilize of ternary if, map booleans output strings: (a "*" true, empty string false).
public class ternary { public static void main(string[] args) { boolean flags[]={true,false,true}; (boolean f : flags) { system.out.println(f?"*":""); } } } so output *, [empty], *.
i have input xml document, 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 have next xslt template maps true '*' , false '' (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 more concise version of ?
a) can automatically value of boolean true|false straight string 'true|false' ? b) there (xpath?) build map boolean true|false '*', '' ?
a) can automatically value of boolean true|false straight string 'true|false' ?
b) there (xpath?) build map boolean true|false '*', '' ?
this xpath question.
i. xpath 1.0
there more 1 xpath expressions evaluation of produces wanted result:
substring('*', 2 -(@flag = 'true')) this answers b) -- notice strings 'true' , 'false' aren't boolean values! 2 boolean values true() , false() , not strings.
in above look utilize fact in xpath 1.0 if boolean in context number needed, automatically converted number. definition:
number(true()) 1
and
number(false()) 0
thus sec argument of phone call substring() above:
2 - (@flag = 'true') is evaluated 1 when @flag = 'true' , 2 otherwise.
a more general xpath 1.0 look produces string $s1 if $val "x" , produces string $s2 if $val "y" :
concat(substring($s1, 1 div ($val = "x")), substring($s2, 1 div ($val = "y")) ) this produces string $s1 when $val = "x", string $s2 when $val = "y", , empty string -- if none of these 2 conditions holds.
the above xpath 1.0 look can generalized produce n different string results $s1, $2, ..., $sn when $val 1 of values $v1, $v2, ..., $vn because function concat() can have number of arguments.
ii. xpath 2.0 (xslt 2.0)
'*'[current()/@flag = 'true'] and more generally, given 2*n atomic values $s1, $s2, ... $sn , $v1, $v2, ..., $vn, such $vi values different, result of evaluating xpath 2.0 expression:
($s1, $s2, ..., $sn)[index-of(($v1, $v2, ..., $vn), $v)] is $sk when $v eq $vk.
xslt xpath boolean ternary
Comments
Post a Comment