What is the correct way to test xs: decimal in XSL?

I am trying to display different information depending on the input. If it is an integer, I want to display only the number, if it is decimal, I want to use the pattern 0.00 #. I, I know, got it a little confused, but this is a development specification .:>

I have the following XSL for this specific section, but I do not see to get past xsl: when the error message

"The expected end of the expression, found by" Conjure. "Number (SAVG) β†’ cast <- as xs: decimal"

<xsl:choose> <xsl:when test="number(SAVG) > 0"> <xsl:choose> <xsl:when test="number(SAVG) castable as xs:decimal"> <xsl:value-of select="format-number(SAVG, '###,###,##0.00#')"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="format-number(SAVG, '###,###,##0.###')"/> </xsl:otherwise> </xsl:choose> </xsl:when> <xsl:when test="number(SAVG) = 0"> <xsl:text disable-output-escaping="yes">&amp;lt;</xsl:text>1 </xsl:when> <xsl:otherwise>N/A</xsl:otherwise> </xsl:choose> 

I tried searching / shoving the answers, and I tried the β€œinstance”, I tried using xsl: if, etc., but I can't get this to work. Any help would be greatly appreciated.

Thanks.

From the comments:

Yes, we use 1.0. I'm sorry I'm new to XSL processing, how do I glue my XSL and enter to generate html?

+6
xslt
source share
2 answers

I. XSLT 1.0 :

No xs: integer and xs: decimal in the XPath 1.0 data model used by XSLT 1.0.

Here is a snippet of code that you can use :

  <xsl:choose> <xsl:when test="not(floor(SAVG) = SAVG)"> <xsl:value-of select="format-number(SAVG, '###,###,##0.00#')"/> </xsl:when> <xsl:otherwise> <!-- Integer value --> <xsl:value-of select="SAVG"/> </xsl:otherwise> </xsl:choose> 

Pay attention . To check if a numeric value is an integer, we use the following test:

  floor($someNum) = $someNum 

Here is one way to do this:

 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:sequence select= "for $num in (3, 3.14) return if($num instance of xs:integer) then ($num, ' is xs:integer', '&#xA;') else if($num instance of xs:decimal) then ($num, ' is xs:decimal', '&#xA;') else ($num, ' is something else', '&#xA;') "/> </xsl:template> </xsl:stylesheet> 

when this conversion is applied to any XML document (not used), the desired, correct result is obtained :

 3 is xs:integer 3.14 is xs:decimal 

Or using the format-number() function according to your example :

 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:sequence select= "for $num in (3, 3.14) return if($num instance of xs:integer) then (format-number($num, '###,###,##0.###'), '&#xA;') else if($num instance of xs:decimal) then (format-number($num, '###,###,##0.00#'), '&#xA;') else () "/> </xsl:template> </xsl:stylesheet> 

produces

 3 3.14 
+6
source share

This XSLT 1.0 style sheet:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="test"> <xsl:choose> <!-- Not number or number less than zero --> <xsl:when test="0 > SVGA or number(SVGA) != SVGA"> <xsl:text>N/A</xsl:text> </xsl:when> <!-- Number zero --> <xsl:when test="SVGA = 0"> <xsl:text>&lt;1</xsl:text> </xsl:when> <!-- Integer number --> <xsl:when test="floor(SVGA) = SVGA"> <xsl:value-of select="format-number(SVGA,'###,###,##0.###')"/> </xsl:when> <!-- Double number --> <xsl:otherwise> <xsl:value-of select="format-number(SVGA,'###,###,##0.00#')"/> </xsl:otherwise> </xsl:choose> <xsl:text>&#xA;</xsl:text> </xsl:template> </xsl:stylesheet> 

With this input:

 <root> <test> <SVGA>0</SVGA> </test> <test> <SVGA>12131</SVGA> </test> <test> <SVGA>123.5654</SVGA> </test> <test> <SVGA>-12.1</SVGA> </test> <test> <SVGA>-7528</SVGA> </test> <test> <SVGA>zero</SVGA> </test> </root> 

Output:

 &lt;1 12,131 123.565 N/A N/A N/A 

Edit 3 : better test order (plus Dimitre expression), better test case, closer input sample for polling.

0
source share

All Articles