簡體   English   中英

XPath-Java XPath結果意外

[英]XPath - Java XPath result unexpected

我注意到了一些奇怪的事情。 這是我的XML

<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

/ Items / Item [(((Amount * Quantity)!= TotalAmount)] /名稱

此XPath必須打印其TotalAmount!= Product( Amount,Quantity )的項目的名稱。

我得到值A。但是我不明白為什么會這樣,因為0.0012 * 17 = 0.0204

而且,如果我刪除項目“ A”,那么我不會得到結果。

新版本的XPath也是如此

/ Items / Item [(((Amount * Quantity)!= TotalAmount)]中的$ x返回$ x / Name

我在Java中使用Saxon 9。

有人可以解釋為什么會這樣。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM