How to check for NaN in XSLT? - xslt

How do I check in XPath / XSLT whether a certain value is NaN?
One way of doing it is string($x) = 'NaN', but maybe there is a nicer / more efficient solution - something like JavaScript's isNaN function?
Solutions in XSLT 1 and XSLT 3 are welcome.

Related

Find out the difference between two date times using xslt or xquery

I am new to xslt and xquery . can someone please guide me with below:
I want to find out the difference in two date times is less than 24 hours by using xslt 1.0 or xquery
For example: In response I receive 2022-03-10T10:57:53.746-05:00
I want to compare it with current datetime and make sure it is less than 24 hours
To add to Martin's comment, a pure XPath 2 version (meaning compatible with XSLT 2 and all XQuery) is:
current-dateTime() - xs:dateTime('2022-03-10T10:57:53.746-05:00')
lt xs:dayTimeDuration("PT24H")
This is in case you weren't sure how to express the "less than 24 hours" condition. (An equivalent duration would be P1D.)
But this expression isn't compatible with XSLT 1.0. For that, the link to the XSLT 1.0 solution from the comments would be required.

How to format number using format-number in xslt

I want to format float number in xslt? I know that I should use this function format-number, but don't really know.
example:
047.6000 to 47.60
You may use like this.
<xsl:value-of select="format-number(.,'#.00')"/>

replace the character in XSLT

I am using the xslt for transformation but from input 240 characters receiving in one element ,In that element different special characters receiving(eg :---> %,?,/,-,_,#,!,$,^) .
I need to replace the those characters.
It is possible in XSLT 1.0.If it is possible can you please give me the code with examples?.Thanks
Eg:
<remark> whfwlknf234#skl$ck?nvwkld^fnwlfn </remark>
It is possible in XSLT 1.0
Yes, it is possible. Use the translate() function to replace them with ... oh, you didn't say with what.

XSL format-number acting weird

I have a weird problem with the XSL number formatting function. The function will
return NaN if i do not have two elements.
I am using the XSLT version 1.0 and the libxslt XSLT processor.
Exempli gratia
This will return not a number
<Weight><xsl:value-of select='format-number(weight, "#.00")'/></Weight>
However if i do it like this
<Weight><xsl:value-of select='format-number(weight, "#.00")'/></Weight>
<Weight><xsl:value-of select="weight"/></Weight>
i get the returned values.
What could be causing this behaviour?
Thanks in Advance!
EDIT
I found out that i could make PHP function calls in XSLT so i used
the PHP number_format function inorder to get the desired result.
<xsl:value-of select="php:functionString('number_format', weight, 2, '.', '')"/>
It solved my problem but it doesn't answer my question about why XSLT format-number
function is returning a NaN value.

need to display char in xslt

Hi all
I am using xslt 1.0. I have the char code as FOA7 which has to displayed as a corresponding character. My input is
<w:sym w:font="Wingdings" w:char="F0A7"/>
my xslt template is
<xsl:template match="w:sym">
<xsl:variable name="char" select="#w:char"/>
<span font-family="{#w:fonts}">
<xsl:value-of select="concat('&#x',$char,';')"/>
</span>
</xsl:template>
It showing the error as ERROR: 'A decimal representation must immediately follow the "&#" in a character reference.'
Please help me in fixing this..Thanks in advance...
This isn't possible in (reasonable) XSLT. You can work around it.
Your solution with concat is invalid: XSLT is not just a fancy string-concatenator, it really transforms the conceptual tree. An encoded character such as  is a single character - if you were to somehow include the letters & # x f 0 a 7 ; then the XSLT processor would be required to include these letters in the XML data - not the string! So that means it will escape them.
There's no feature in XSLT 1.0 that permits converting from a number to a character with that codepoint.
In XSLT 2.0, as Michael Kay points out, you can use codepoints-to-string() to achieve this.
There are two solutions. Firstly, you could use disable-output-escaping. This is rather nasty and not portable. Avoid this at all costs if you can - but it will probably work in your transformer, and it's probably the only general, simple solution, so you may not be able to avoid this.
The second solution would be to hardcode matches for each individual character. That's a mess generally, but quite possible if you're dealing with a limited set of possibilities - that depends on your specific problem.
Finally, I'd recommend not solving this problem in XSLT - this is typically something you can do in pre/post processing in another programming environment more appropriately. Most likely, you've an in-memory representation of the XML document to be able to use XSLT in the first place, in which case this won't even take much CPU time.
<span font-family="{#w:font}">
<xsl:value-of select="concat('&#x', #w:char, ';')"
disable-output-escaping="yes"/>
</span>
Though check #Eamon Nerbonne's answer, why you shouldn't do it at all.
If you were using XSLT 2.0 (which you aren't), you could write a function to convert hex to decimal, and then use codepoints-to-string() on the result.
use '&' for '&' in output:
<xsl:value-of select="concat('&#x',$char,';')"/>