微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

xml – XSLT中的Divide和Multiply值选择

只想在div之后将选择值乘以1000000,这是非常新的;我相信这对某人来说是一个简单的问题.提前致谢.

<xsl:value-of select="AbsolutePos/@x div 80" />

想要乘以1000000,不要认为这是正确的,因此返回不正确的值

<xsl:value-of select="AbsolutePos/@x div 80 * 1000000" />

续:拥有以下XML

<AbsolutePos x="-1.73624e+006" y="-150800" z="40000"></AbsolutePos>

需要转变为

<PInsertion>-21703,-1885,500</PInsertion>

使用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>

虽然收到了

<PInsertion>NaN,500</PInsertion>

假设取X值并除以80然后乘以10000以返回-21703

解决方法

如果您的XSLT处理器无法识别科学记数法,则必须自己完成工作 – 例如:

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>

请注意,这是一个不会处理负指数的简化示例.

编辑

如果你的输入始终遵循(仅)@x的形式为#.#### e 006,那么你可以通过取substring-before(AbsolutePos / @ x,’e’的值来简化这个过程.并将其乘以12500(即10 ^ 6/80).

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。