Re: [xsl] comparing attributes if missing at times

Subject: Re: [xsl] comparing attributes if missing at times
From: David Carlisle <davidc@xxxxxxxxx>
Date: Wed, 26 Mar 2014 13:04:15 +0000
see the comments about != in the recent thread on text() (if you followed it that far:-)

Applying != to sequences that you do not know have exactly 1 element is well defined but rarely useful.


<elem x="1"/>
<elem x="1"/>
<elem x="2"/>
<elem x="3"/>

compare the results of

<xsl:stylesheet version="2.0" xmlns:xsl="";>

<xsl:template match="elem"> <elem

test1="{@x != preceding-sibling::elem[1]/@x}"
test2="{not(@x = preceding-sibling::elem[1]/@x)}"
test3="{string(@x) != preceding-sibling::elem[1]/string(@x)}"
test4="{not(string(@x) = preceding-sibling::elem[1]/string(@x))}"
<xsl:copy-of select="@*"/>

you get:

<elem test1="false" test2="true" test3="false" test4="true" x="1"/>
<elem test1="false" test2="false" test3="false" test4="false" x="1"/>
<elem test1="true" test2="true" test3="true" test4="true" x="2"/>
<elem test1="false" test2="true" test3="true" test4="true"/>
<elem test1="false" test2="true" test3="true" test4="true" x="3"/>

Note that string() ensures that you get exactly one value (a possibly empty string) rather than @x which may return a sequence of length 0 or 1.


Current Thread