Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do string operations in XSLT?

Tags:

string

xslt

xpath

I have the following code block in my xslt;

      <xsl:when test="StatusData/Status/Temperature > 27">
        <td bgcolor="#ffaaaa">              
          <xsl:value-of select="StatusData/Status/Temperature" />              
        </td>
      </xsl:when>

But as you might guess when the value is 34,5 instead of 34.5 it is recognised as a string which makes integer comparison not possible. I thought replacing , with . would be solution that needs a char replace. My question is how I can do this or It would be great to know more about string operations in XSLT...

like image 794
yusuf Avatar asked Nov 06 '08 09:11

yusuf


People also ask

What is string XSLT?

XSLT string length is defined as a string function and takes a single string argument and returns an integer value representing the number of characters in the string. It converts any declared type into a string except that an empty parenthesis cannot be converted. The whitespaces are taken into the count.

How do I compare strings in XSLT?

To see if two elements are the same, XSLT compares their string values using the equals sign ("=").

What is string length in XSLT?

string-length() Function — Returns the number of characters in the string passed in as the argument to this function. If no argument is specified, the context node is converted to a string and the length of that string is returned.


1 Answers

There is a translate() function in XPath:

test="translate(StatusData/Status/Temperature, ",", ".") > 27"

Additionally you should make use of the number function, which converts it's argument to a number (or NaN, if that fails):

test="number(translate(StatusData/Status/Temperature, ",", ".")) > 27.0"

See the documentation for translate() and the documentation for number() at w3.org.

like image 106
Tomalak Avatar answered Oct 01 '22 15:10

Tomalak