normalize-space() not working - xslt

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)"/>

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.)

Formatting number in 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')"/>

XPath duplicates syntax error

I am using the XSL below to test if self axis exists in the following axis, if it does, then select the first preceding value. I am getting a syntax error help please. Reference to what I am trying to achieve:
Remove duplicates xslt/xpath
<xsl:if test="self::*/#Cat=following::*/#Cat">
<xsl:value-of select="preceding-sibling::*/#Cat[1]=[self::*/#Cat=following::*/#Cat]"/>
</xsl:if>
The syntax error is the square brackets around the expression that follows "[1]=".
Are you sure you want to be using "following" rather than "following-sibling"?
Also, if there is an attribute #Cat then there will only be one such attribute, so selecting the first is pointless.

XSLT substring and hash tag

Can someone tell me why this is not working?
Throughout my document I have several column breaks marked as follows: <cb ed="#S" n="45rb"/>
The hash tag is there to refer to another element where the source document is identified.
Now I want to display the column break in the following document. So that it looks like this:
|S45rb|. I thought I could use a simple substring function to get rid of the hash tag like so.
<xsl:template match="TU:cb">
<xsl:variable name="hashms"><xsl:value-of select="//TU:cb/#ed"/></xsl:variable>
<xsl:variable name="ms"><xsl:value-of select="substring($hashms,1,1)"/></xsl:variable>
<span>| <xsl:value-of select="$ms"/> <xsl:value-of select="//TU:cb/#n"/> |</span>
</xsl:template>
When I do it this way I get the following result: |#75ra|. Shouldn't the first 1 in the argument refer to the first character of the string and then the second 1 tell it to move over one character and leave me with the desired S? Instead I don't get the S but only the hash tag. Is there something about hash tags and strings I do not know?
Thanks for your help.
XSL is not 0 base, if you want to start at the second character (after the #), it should be 2 rather than 1.
http://www.w3schools.com/Xpath/xpath_functions.asp
substring($hashms,2,1) = S
You can also omit the length, which might be of benefit for extensibility later on. Especially if whatever comes after the # is the identifier, why limit your code?
substring($hashms,2) = S
substring('#S2',2) = S2

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,'.','')"/>