xslt - what is correct way to test for xs:decimal in XSL? -


i'm trying display different information depending on incoming data. if it's integer, want display number, if it's decimal, want use 0.00# pattern. ya, know, bit mixed up, that's development spec. :>

i have following xsl specific section can't see past xsl:when error message of

"expected end of expression, found 'castable'. number(savg) -->castable <-- xs:decimal"

<xsl:choose>     <xsl:when test="number(savg) > 0">         <xsl:choose>             <xsl:when test="number(savg) castable 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 looking/poking around answers , have tried "instance of", i've tried using xsl:if, etc can't seem work. appreciated.

thanks.

from comments:

yes, using 1.0. i'm sorry i'm new xsl processing, how glue xsl , input generate html?

i. xslt 1.0:

there no xs:integer , xs:decimal in xpath 1.0 data model used xslt 1.0.

here code snippet may 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>  

do note: test if numeric value integer, use following test:

 floor($somenum) = $somenum 

here 1 way 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)          ($num, ' xs:integer', '&#xa;')          else if($num instance of xs:decimal)            ($num, ' xs:decimal', '&#xa;')            else ($num, ' else', '&#xa;')    "/>  </xsl:template> </xsl:stylesheet> 

when transformation applied on xml document (not used), wanted, correct result produced:

3  xs:integer  3.14  xs:decimal  

or, using format-number() function per 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)          (format-number($num, '###,###,##0.###'), '&#xa;')          else if($num instance of xs:decimal)            (format-number($num, '###,###,##0.00#'), '&#xa;')            else ()    "/>  </xsl:template> </xsl:stylesheet> 

produces:

3  3.14  

Comments

Popular posts from this blog

unicode - Are email addresses allowed to contain non-alphanumeric characters? -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

c++ - Convert big endian to little endian when reading from a binary file -