1

Referenced from here

I am trying to find a way to format scientific numbers using xslt version 1.0. I tried to use the example from stockoverflow as reference but I can't get it to work. Any help would be awesome!

Thanks!

EDIT: I want all these numbers to have the same format.

    <?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet type="text/xsl" href="SNS.xslt"?>
    <Scientific_Numbers>
      <Section_1>
        <SN_1>1.0000234E-4</SN_1>
        <SN_1>1.0000353476E-4</SN_1>
      </Section_1>
      <Section_2>
        <SN_1>1.3400234E-4</SN_1>
        <SN_1>0.0000234E-2</SN_1>
      </Section_2>
      <Section_3>
        <SN_1>1.003453453400234E-2</SN_1>
        <SN_1>1.0234E+6</SN_1>
      </Section_3>
    </Scientific_Numbers>

    <?xml version="1.0" encoding="utf-8"?>

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <html>
      <body>
        <h2>ScientificNumbers</h2>
        <xsl:apply-templates/>
      </body>
    </html>
    </xsl:template>

  <xsl:template match="Scientific_Numbers">
    <xsl:apply-templates select="SN_1"/>
    <br/>
  </xsl:template>


    <?xml version="1.0" encoding="utf-8"?>

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="/">
        <html>
          <body>
           <h2>Testing</h2>
           <xsl:apply-templates/>
          </body>
        </html>
      </xsl:template>

      <xsl:template match="Scientific_Numbers">
        <xsl:apply-templates select="SN_1"/>
        <br/>
      </xsl:template>

      <xsl:template match="SN_1[substring-after(.,'E')]">
        <xsl:variable name="vExponent" select="substring-after(.,'E')"/>
        <xsl:variable name="vMantissa" select="substring-before(.,'E')"/>
        <xsl:variable name="vFactor"              
                  select="substring('100000000000000000000000000000000000000000000',
                  1, substring($vExponent,2) + 1)"/>
    <xsl:choose>
      <xsl:when test="starts-with($vExponent,'-')">
        <xsl:value-of select="$vMantissa div $vFactor"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$vMantissa * $vFactor"/>
      </xsl:otherwise>
    </xsl:choose>
      </xsl:template>
    </xsl:stylesheet>
Community
  • 1
  • 1
AD6
  • 57
  • 1
  • 7
  • http://stackoverflow.com/questions/4367737/formatting-scientific-number-representation-in-xsl – ABach Oct 08 '12 at 22:21
  • @ABach, Thanks but I linked that post in my question already. That post is what I am using as reference and I can't figure out how to implement it correclty. – AD6 Oct 09 '12 at 13:06

1 Answers1

0

There is no mechanism in XSLT 1.0 to output numbers in scientific notation (in fact, there are limited capabilities even in XSLT 2.0). You'll have to see what kind of extension mechanisms your XSLT processor supports.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164