Reputation: 53
Just want to multiply the value-of select by 1000000 after div, very new to this; I'm sure it's a easy question for someone. Thanks in advance.
<xsl:value-of select="AbsolutePos/@x div 80" />
Wanting to multiply by 1000000, don't think this is right, hence is returning incorrect value
<xsl:value-of select="AbsolutePos/@x div 80 * 1000000" />
Continued: Have the following XML
<AbsolutePos x="-1.73624e+006" y="-150800" z="40000"></AbsolutePos>
Needing to change to
<PInsertion>-21703,-1885,500</PInsertion>
Using XSL
<PInsertion><xsl:value-of select="AbsolutePos/@x div 80 * 1000000" />,<xsl:value-of select="AbsolutePos/@y div 80" />,<xsl:value-of select="AbsolutePos/@z div 80" /></PInsertion>
Though receiving
<PInsertion>NaN,-1885,500</PInsertion>
Suppose to take the X value and divide it by 80 then multiply by 10000 to return -21703
Upvotes: 4
Views: 20310
Reputation: 117073
If your XSLT processor does not recognize scientific notation, you will have to do the work yourself - for example:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="AbsolutePos">
<PInsertion>
<xsl:apply-templates select="@*"/>
</PInsertion>
</xsl:template>
<xsl:template match="AbsolutePos/@*">
<xsl:variable name="num">
<xsl:choose>
<xsl:when test="contains(., 'e+')">
<xsl:variable name="factor">
<xsl:call-template name="power-of-10">
<xsl:with-param name="exponent" select="substring-after(., 'e+')"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="substring-before(., 'e+') * $factor" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="." />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="$num div 80" />
<xsl:if test="position()!=last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:template>
<xsl:template name="power-of-10">
<xsl:param name="exponent"/>
<xsl:param name="result" select="1"/>
<xsl:choose>
<xsl:when test="$exponent">
<xsl:call-template name="power-of-10">
<xsl:with-param name="exponent" select="$exponent - 1"/>
<xsl:with-param name="result" select="$result * 10"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$result"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Note that this is a simplified example that will not handle negative exponents.
If your input always follows the pattern of (only) @x being in the form of #.####e+006
then you can make this much simpler by taking the value of substring-before(AbsolutePos/@x, 'e+')
and multiplying it by 12500 (i.e. 10^6 / 80).
Upvotes: 5