【问题标题】:Divide and Multiply value-of-select in XSLTXSLT 中的除法和乘法选择值
【发布时间】:2014-05-27 04:23:29
【问题描述】:

只想在div之后将select的value-of乘以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,-1885,500</PInsertion>

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

【问题讨论】:

  • 您的代码似乎没问题。 @x 的值是多少?
  • "因此返回不正确的值" 一个具体的例子,包括输入和接收到的结果会很有用。
  • 对我最初的问题进行了修改,谢谢大家。
  • 并非所有 XSLT 处理器都能识别 -1.73624e+006 是一个数字。
  • 这就是它失败并返回的地方; NaN,-1885,500 ,我能做什么?

标签: xml xslt division multiplication


【解决方案1】:

如果您的 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)。

【讨论】:

    猜你喜欢
    • 2021-04-12
    • 2019-09-22
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    • 2014-01-07
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多