[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Number 'normalisation' in XSL

Posted on 2001-08-09
5
Medium Priority
?
448 Views
Last Modified: 2008-03-10
Hello!
I have to normalise the number that i have in XML, i.e. i have to calculate its mantissa and exponent.
So if i have 123456 it should be normalised as 1,23456e+5.
Unfortunatelly, i have to use only pure xsl, no extension functions or scripts are allowed.
Please give me working example asap.
Thanks.
0
Comment
Question by:pavlin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 27

Expert Comment

by:BigRat
ID: 6369460
You should look at the xsl:decimal-format element and the associated function format-number. I have studied both of these and I can't see anyway of doing it :(
0
 
LVL 4

Expert Comment

by:sdussinger
ID: 6371185
I've looked at the number formatting stuff as well, and it doesn't appear to work the way we'd like. However, in an extreme masochistic XSL session, I've managed to build a transform that will normalize a number.

Here it is in all its grotesque glory:

-----------------------------------------------------------

<!--
   Generic template to normalize a number

   Pass a param called "num" containing the number you wish
   to normalize
-->
<xsl:template name="normalize-number">
  <xsl:param name="num" select="0"/>

  <xsl:call-template name="do-normalize">
    <xsl:with-param name="mantissa" select="$num"/>
    <xsl:with-param name="exp" select="0"/>
  </xsl:call-template>

</xsl:template>

<!--
   Simple template for formatting the exponent. This
   is needed to ensure that the '+' sign shows up for
   positive exponents.
-->
<xsl:template name="format-exp">
  <xsl:param name="exp" select="0"/>

  <xsl:if test="$exp &gt;= 0">
    <xsl:text>+</xsl:text>
  </xsl:if>
  <xsl:value-of select="$exp"/>

</xsl:template>

<!--

   This is the body of the normalizer. It recursively
   processes the number until its in the correct form
-->
<xsl:template name="do-normalize">

  <xsl:param name="mantissa" select="0"/>
  <xsl:param name="exp" select="0"/>

  <xsl:choose>

    <!-- If the mantissa is 0, we're done. Just print it out -->
    <xsl:when test="$mantissa = 0.0 or $mantissa = -0.0">
      <xsl:value-of select="format-number(0.0, '#,##0.00000')"/>
      <xsl:text>e</xsl:text>
      <xsl:call-template name="format-exp">
        <xsl:with-param name="exp" select="$exp"/>
      </xsl:call-template>
    </xsl:when>

    <!-- If we've gotten the number normalized, then print it out.
         This is for a positive number -->
    <xsl:when test="$mantissa &gt;= 1.0 and $mantissa &lt; 10.0">
      <xsl:value-of select="format-number (number($mantissa), '#,##0.00000')"/>
      <xsl:text>e</xsl:text>
      <xsl:call-template name="format-exp">
        <xsl:with-param name="exp" select="$exp"/>
      </xsl:call-template>
    </xsl:when>

    <!-- We've gotten a negative number normalized, print it -->
    <xsl:when test="$mantissa &gt; -10.0 and $mantissa &lt;= -1.0">
      <xsl:value-of select="format-number (number($mantissa), '#,##0.00000')"/>
      <xsl:text>e</xsl:text>
      <xsl:call-template name="format-exp">
        <xsl:with-param name="exp" select="$exp"/>
      </xsl:call-template>
    </xsl:when>

    <!-- In this case we've got a number greater than abs(10.0). We
         add one to the exponent, divide the mantissa by 10, and recurse -->
    <xsl:when test="$mantissa &gt;= 10.0 or $mantissa &lt;= -10.0">
      <xsl:call-template name="do-normalize">
        <xsl:with-param name="mantissa" select="$mantissa div 10"/>
        <xsl:with-param name="exp" select="$exp + 1"/>
      </xsl:call-template>
    </xsl:when>

    <!-- In this case we have a number whose abs() < 1.0. We subtract 1
         from the exponent, multiply the mantissa by 10 and recurse. -->
    <xsl:when test="$mantissa &gt; -1.0 and $mantissa &lt; 1.0">
      <xsl:call-template name="do-normalize">
        <xsl:with-param name="mantissa" select="$mantissa * 10"/>
        <xsl:with-param name="exp" select="$exp - 1"/>
      </xsl:call-template>
    </xsl:when>
  </xsl:choose>

</xsl:template>


-----------------------------------------------------------

It's probably one of the most wretched things one would ever want to do with XSL, but it works. I've tested it using Stylus and it handled every test case I threw at it.

--Steve
0
 
LVL 2

Accepted Solution

by:
djokov earned 800 total points
ID: 6371664
Hi!
I think you want to calculate mantissa and exponent, not just to "print" the normalized value. In such case you will need to return two values from your "Normalization" template. So, my suggestion is:

Call template in this way:
<xsl:variable name="nn">
   <xsl:call-template name="Normalize">
       <xsl:with-param name="man" select="$NumberToBeNormalized"/>
   </xsl:call-template>
</xsl:variable>
<xsl:variable name="Normalized" select="$nn"/>
...
Then you could get Mantissa and Exponnet in this way:
<xsl:variable name="Mantissa" select="$Normalized/Mantissa"/>
<xsl:variable name="Exponent" select="$Normalized/Exponent"/>
... and then you could do what you want with $Mantissa and $Exponent.

Finally, the "Normalize" template:
<xsl:template name="Normalize">
   <xsl:with-param name="man"/>
   <xsl:with-param name="exp" select="0"/>

   <xsl:choose>
       <xsl:when test="($man > 10) or ($man &lt; -10)">
           <xsl:call-template name="Normalize">
               <xsl:with-param name="man" select="$man div 10"/>
               <xsl:with-param name="exp" select="$exp + 1"/>
           </xsl:call-template>
       </xsl:when>
       <xsl:when test="(($man &lt; 1) and ($man > 0)) or (($man &lt; 0) and ($man > -1))">
           <xsl:call-template name="Normalize">
               <xsl:with-param name="man" select="$man * 10"/>
               <xsl:with-param name="exp" select="$exp - 1"/>
           </xsl:call-template>
       </xsl:when>
       <xsl:otherwise>
           <Mantissa><xsl:value-of select="$man"/></Mantissa>
           <Exponent><xsl:value-of select="$exp"/></Exponent>
       </xsl:otherwise>
   </xsl:choose>
</xsl:template>

Cheers.
0
 
LVL 2

Expert Comment

by:djokov
ID: 6371672
<xsl:when test="($man > 10) or ($man &lt; -10)">
should be:
<xsl:when test="($man >= 10) or ($man &lt;= -10)">

sorry.
0
 
LVL 4

Expert Comment

by:sdussinger
ID: 6372253
It seems  as if my choice of the comments was misleading. When I said "print" in the comments I was really referring to formatting the normalized number.  One could easily use the code to assign a value to a variable as in the following:

<xsl:variable name="normalized">
  <xsl:call-template name="normalize-number">
    <xsl:with-param name="num" value="$value-to-normalize"/>
  </xsl:call-template>
</xsl:variable>

Sorry for the confusion...

--Steve
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question