Formatting number in xslt - xslt

I am trying to format a number in XSLT, but I always get NaN as a result.
Original example number is: 1 321.94
Code:
<xsl:value-of select="format-number(number(string(.)), '### ##0,00', 'format1')"/>
Seems like number(string(.)) doesn't work. How can I remove the space from the original number to cope with NaN?

Using translate() should work for both XSLT 1.0 and 2.0. You could also use replace() in 2.0.
Here's an example of translate() (broken up into multiple lines for readability):
<xsl:value-of
select="format-number(
number(translate(.,' ','')),
'### ##0,00','format1')"/>

Related

XSLT mapping giving alphanumeric for decimal value

I have a requirement where i should get absolute value for -0.0000000005 .I have tried with <xsl:value-of select='abs(-0.0000000005)' /> and <xsl:value-of select="translate(-0.0000000005, '-', '')" /> .But nothing is working and getting always alpha numeric value as 5.0E10.Please help here to get correct absolute value form any decimal number.
need code for correct absolute value to get from decimal numbers
Depending upon the XSLT processor, this might work for you:
<xsl:value-of select="xs:decimal(abs(-0.0000000005))" />
You could try using the format-number function to explicitly specify how the number is formatted. For example something like this:
<xsl:value-of select="format-number(abs(-0.0000000005), '0.##########')" />
(Note that the abs function is not part of XPath 1.0/XSLT 1.0. Users of XSLT 1.0 may want to check if their XSLT processor supports the math:abs extension function from EXSLT.)

How to remove dashes in numbers WITHOUT removing leading zeros

The input (account numbers) I have are currently in the format 005-947864-296, I'm using the translate function to remove dashes as follows: <xsl:value-of select="translate(($account_number), '-', '')"/> The problem is that the output I'm getting in the csv is 5947864296 (which is removing Leading Zeros). How do I remove the dashes WITHOUT removing the leading zeros?
I'm using XSLT 2.0 and I tried both translate and replace functions but getting the same result!
Perhaps you are viewing the generated CSV by loading it into a spreadsheet program such as Microsoft Excel? Excel (notoriously) assumes that if a field is all-numeric, leading zeroes are insignificant and can be discarded (which is not the case for things such as account numbers).
The problem isn't with your XSLT code generating the CSV, it's with the application you are using to read/process the CSV.
If you're using XSLT 2.0 or later, you can use the replace function with a regex:
<xsl:value-of select="replace($account_number, '-', '')"/>
If you're using XSLT 1.0, you can use the translate function with a character map:
<xsl:value-of select="translate($account_number, '-0123456789', '0123456789')"/>
This will replace all dashes with zeros, but will preserve any leading zeros.

normalize-space() not working

There is xslt code for version 1.0 but I want to convert it to version 2.0.
<xsl:value-of select="normalize-space(round((. - $var1) div $var2))"/>
But when I try to run it, the SAXON output is:
F [Saxon-HE 9.5.0.2] XPTY0004: Required item type of first argument of normalize-space() is xs:string; supplied value has item type numeric
help me in finding and solve this issue? Thanks in advance.
As the error message mentions, normalize-space expects a string as an argument, but round returns a numeric value. Numbers don't actually have spaces in though, so there is no need to use normalize-space on the result.
This should work instead:
<xsl:value-of select="round((. - $var1) div $var2)"/>

XSLT2 format-dateTime - AM/PM without dots/periods

This is a question for XSLT 2 format-dateTime function.
Please can anyone tell me how to make A.M./P.M. display with not dots/periods (AM/PM)?
<xsl:variable name="ampm" select="format-dateTime(DATE, '[PN]')"/>
The below code returns blank?
<xsl:value-of select="replace($ampm,'.','')"/>
Thanks, Will
format-dateTime(DATE, '[PN,2-2]')
will output AM/PM on some XSLT 2 implementations.
The format string specifies that the output should be exactly 2 character.
translate works
<xsl:value-of select="translate($ampm,'.','')"/>

lower case the first character of a string using only xslt 1.0

I have seen patterns for translating a string into lower (or upper case) using the translate function for folks stuck using xslt 1.0.
Is there a elegant way of just making the first letter of a string lowercase?
TestCase => testCase
If your string were, for example, in an attribute called name:
<xsl:value-of select="concat(translate(substring(#name, 1, 1), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), substring(#name, 2))"/>
You should be able to combine substring and concat with translate to do it like so:
concat(translate(substring(s,1,1), $smallcase, $uppercase),substring(s,2))
Use the XPath translate function, having separated the string into first character and the rest. This will require somewhat long winded XSLT using multiple variables to hold intermediate results.
XSLT has a substring function, so you could use that pattern with the substring function to get what you want.