比较值与十进制

时间:2019-04-16 16:52:08

标签: xslt

我需要比较xml中返回的值。如果值为0.00,则执行“此”,否则执行“此”。测试我的代码时出现错误。

错误: 无法使用提供的XML / XSL输入生成XML文档。在样式表编译期间报告了错误

网站用来测试: https://www.freeformatter.com/xsl-transformer.html

这是XML:

<cst>
  <cli>
    <cli_contact_person_name>a customer service representative</cli_contact_person_name>
    <cli_phone_number>312-422-3236</cli_phone_number>
    <cli_contact_phone_ext>312-422-3236</cli_contact_phone_ext>
    <cli_email_address>aha-orders@pbd.com</cli_email_address>
  </cli>
    <inv>
      <cli_contact_person_name>a customer service representative</cli_contact_person_name>
      <cli_phone_number>312-422-3236</cli_phone_number>
      <cli_contact_phone_ext>312-422-3236</cli_contact_phone_ext>
      <cli_email_address>aha-orders@pbd.com</cli_email_address>
      <AHABU>AHA</AHABU>
      <cst_key>D387A106-0E7C-4201-A9DD-1B0615191D18</cst_key>
      <inv_key>31F2FD83-0A06-49CC-A424-84054EF15D8F</inv_key>
      <inv_code_cp>131961</inv_code_cp>
      <inv_trx_date>01/05/2010</inv_trx_date>
      <inv_total>150.00</inv_total>
      <inv_pyd_date>01/05/2010</inv_pyd_date>
      <inv_paid>125.00</inv_paid>
      <inv_balance>25.00</inv_balance>
    </inv>
  </invs>
</cst>

这是XSL代码:

<!--Paid-->
    <div class="orderhistory-subcontainer2 orderhistory-hide-1">
        <xsl:choose>
            <xsl:when test="inv_balance=0.00">
                <xsl:value-of select="inv_pyd_date" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="" />
            </xsl:otherwise>
        </xsl:choose>
    <!--<xsl:value-of select="inv_pyd_date" />-->
    </div>

也尝试过:

<!--Paid-->
<div class="orderhistory-subcontainer2 orderhistory-hide-1">
    <xsl:choose>
        <xsl:when test = "format-number(inv_balance, '0.00') = number(0.00)">
            <xsl:value-of select="inv_pyd_date" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="" />
        </xsl:otherwise>
    </xsl:choose>
<!--<xsl:value-of select="inv_pyd_date" />-->

问题是我在这里想到的语法:

或者至少我认为那是原始问题。不确定现在是什么问题

更新: 也尝试过此操作以解决同一错误:

 <xsl:when test = "format-number(inv_balance, '#.00') = number(0.00)">

更新: 我发现了错误:

<xsl:value-of select="" />

1 个答案:

答案 0 :(得分:1)

简单的解决方案是:

 <xsl:when test="inv_balance=0">

这会将字符串“ 0.00”转换为数字并将其与数字0进行比较:

  

如果至少一个要比较的对象是一个数字,则将每个要比较的对象转换为一个数字,就像通过应用数字函数一样。
  https://www.w3.org/TR/1999/REC-xpath-19991116/#booleans