2017-02-23 20 views
0

我注意到了一些奇怪的东西。这是我的XMLXPath - Java XPath结果出人意料

<Items> 
    <Item> 
     <Name>A</Name> 
     <Amount>0.0012</Amount> 
     <Quantity>17</Quantity> 
     <TotalAmount>0.0204</TotalAmount> 
    </Item> 
    <Item> 
     <Name>B</Name> 
     <Amount>1</Amount> 
     <Quantity>2</Quantity> 
     <TotalAmount>2</TotalAmount> 
    </Item> 
    <Item> 
     <Name>C</Name> 
     <Amount>3</Amount> 
     <Quantity>2</Quantity> 
     <TotalAmount>6</TotalAmount> 
    </Item> 
</Items> 

这是我用

/项目/项目[((金额*数量)!=总金额)] /名称

这中的XPath XPath必须打印其TotalAmount!=产品(数量,数量)的项目的名称。

我得到的值A,但我不明白为什么会发生由于0.0012×17 = 0.0204

如果我删除项目“A”,然后我没有得到的结果。

也是如此的XPath的新版本以及

为$ X IN /项目/项目[((金额*数量)!=总金额)返回 $ X /名称

我在Java中使用Saxon 9。

有人可以解释为什么会发生这种情况。

回答

1

尝试使用xs:decimal/xs:integer更好的精度:/Items/Item[((xs:decimal(Amount) * xs:integer(Quantity)) != TotalAmount)]/Name

如果你看一下http://xsltransform.net/94AbWBV这确实

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs"> 

    <xsl:strip-space elements="*"/> 
    <xsl:output indent="yes"/> 

    <xsl:template match="/"> 
     <results> 
      <names-precise> 
      <xsl:value-of select="/Items/Item[((xs:decimal(Amount) * xs:integer(Quantity)) != TotalAmount)]/Name"/> 
      </names-precise> 
      <names-imprecise> 
       <xsl:value-of select="/Items/Item[((Amount * Quantity) != TotalAmount)]/Name"/> 
      </names-imprecise> 
      <xsl:apply-templates/> 
     </results> 

    </xsl:template> 

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="Item"> 
     <xsl:copy> 
      <xsl:apply-templates/> 
      <double-computation> 
       <xsl:value-of select="Amount * Quantity"/> 
      </double-computation> 
      <decimal-computation> 
       <xsl:value-of select="xs:decimal(Amount) * xs:integer(Quantity)"/> 
      </decimal-computation> 
     </xsl:copy> 
    </xsl:template> 
</xsl:transform> 

你可以看到,使用不足以默认的浮点运算来计算确切的结果:

<results> 
    <names-precise/> 
    <names-imprecise>A</names-imprecise> 
    <Items> 
     <Item> 
     <Name>A</Name> 
     <Amount>0.0012</Amount> 
     <Quantity>17</Quantity> 
     <TotalAmount>0.0204</TotalAmount> 
     <double-computation>0.020399999999999998</double-computation> 
     <decimal-computation>0.0204</decimal-computation> 
     </Item> 
     <Item> 
     <Name>B</Name> 
     <Amount>1</Amount> 
     <Quantity>2</Quantity> 
     <TotalAmount>2</TotalAmount> 
     <double-computation>2</double-computation> 
     <decimal-computation>2</decimal-computation> 
     </Item> 
     <Item> 
     <Name>C</Name> 
     <Amount>3</Amount> 
     <Quantity>2</Quantity> 
     <TotalAmount>6</TotalAmount> 
     <double-computation>6</double-computation> 
     <decimal-computation>6</decimal-computation> 
     </Item> 
    </Items> 
</results> 

同样不精确等存在使用IEEE双数字格式的Javascript等语言:

document.getElementById('result').textContent = 0.0012 * 17;
<p>Result of <code>0.0012 * 17</code> with Javascript is <code id="result"></code>.</p>

+0

嗨马丁。谢谢。像魅力一样工作。 :) – Balu