Equivalent to greater than or less than test on strings

Here should go questions about transforming XML with XSLT and FOP.
ra0543
Posts: 80
Joined: Wed Jan 14, 2009 12:50 pm

Equivalent to greater than or less than test on strings

Post by ra0543 »

In an XSL transformation I find

Code: Select all

<xsl:if test="@num>preceding-sibling::element/@num"> ... </xsl:if>
works fine as a test so long as the num attributes referenced have numerical values (e.g. when @num is 2 and the equivalent in the preceding sibling is 3, the result is false).

Is there some easy way of doing the same kind of test where the num attributes are, say, single lower-case letters of the alphabet i.e. match [a-z]? I should like to be able to do the same test but where the result is based on alphabetical order, e.g. when the first value is 'a' and the second is 'b' (i.e. to evaluate 'a'<'b' and get false) or vice versa ('b'>'a' to get true). Is there some alternative to the < and > operators that work with strings, or what can be done as a workround?
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Re: Equivalent to greater than or less than test on strings

Post by sorin_ristache »

Hello,

Use the compare function:

<xsl:if test="compare(@num, preceding-sibling::element/@num)"> ... </xsl:if>


Regards,
Sorin
Post Reply