【问题标题】:Subtract in xslt 1.0在 xslt 1.0 中减去
【发布时间】:2017-06-19 21:25:57
【问题描述】:

我在这个示例文件中遇到了减法问题:

<EVENTS>
<ROW ID="204" ID_PLACE="1" EVENT_TYPE="B" EVENT_NAME="TEST1" EVENT_ID="201">
<PRICE>
<ROW EVENT_PRICE="165,00"/>
</PRICE>
</ROW>
<ROW ID="205" ID_PLACE="1" EVENT_TYPE="P" EVENT_NAME="TEST1" EVENT_ID="201">
<PRICE>
<ROW EVENT_PRICE="125,00"/>
</PRICE>
</ROW>
</EVENTS>

她是我的 XSLT 的相关部分:

<xsl:for-each select="EVENTS/ROW/PRICE/ROW">
  <xsl:variable name="event_b">
    <xsl:choose>
      <xsl:when test="EVENT_TYPE=B">
        <xsl:value-of select="EVENT_PRICE" />
      </xsl:when>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="event_p">
    <xsl:choose>
      <xsl:when test="EVENT_TYPE=P">
        <xsl:value-of select="EVENT_PRICE" />
      </xsl:when>
    </xsl:choose>
  </xsl:variable>
  <xsl:value-of select="number($event_b) - number($event_p)" />
</xsl:for-each>

我必须从对应的类型 B 中减去类型为 P 的 event_price。在此示例中,我想得到一个结果 40,并将其输出到结果树中,但它不起作用。怎么了?

【问题讨论】:

  • 发布您的代码,这样我们就不必猜测您的问题是什么。 -- 提示:您需要将逗号翻译成一个点才能将价格识别为数字。
  • 请不要在 cmets 中发布代码 - 改为编辑您的问题。还要确保您的代码完整 - 请参阅:minimal reproducible example.
  • 我需要这个 165,00-125,00=40,00 的结果。我对数字格式没有问题。我在一个名为 event_price_subtract 的节点中显示减法结果时遇到问题。
  • "我没有数字格式的问题。"我相信你会的。我们会在您发布代码时确定。
  • 我编辑了我的帖子并附上了我用于减法的 xslt。

标签: xml xslt


【解决方案1】:

您正在尝试将字符串转换为数字,但这些字符串的格式不适合 XSL/XPath/XSI 数字。具体来说,number() 构造函数仅将句点 ('.') 字符识别为小数分隔符,但您的输入字符串似乎在该角色中使用了逗号 (',')。

如果您无法更正数据以遵循表示十进制数字的流行约定,那么您需要考虑样式表中的变体约定。您可以使用translate 函数来做到这一点。

然而,在您到达那里之前,您的样式表结构中存在严重问题,其中包括:

  • 你的外部xsl:for-each 没有意义。您正在迭代单个(内部)&lt;ROW&gt;s,但您打算处理相应行的 pairs。一个方便的上下文将是最接近的共同祖先,尽管还有其他方法可以解决这个问题。

  • 您的 selecttest 表达式正在尝试引用您要引用属性的子元素。

  • 在某些地方,您的测试与(不存在的)子元素进行比较,而不是字符串值。

总体而言,您使事情变得比需要的困难得多。对于您提供的输入,您可以执行以下操作:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="EVENTS">
    <!-- Note @attribute_name syntax for referring to attributes -->
    <!-- Note quoted form for string literals -->
    <!-- Note that these expressions are evaluated relative to the EVENTS element
         matched by this template -->
    <!-- Note no iteration: each value wanted is identified via an
         appropriate XPath expression -->
    <!-- Note use of a select expression to specify the variables' values.
         That doesn't make much difference in this particular case, but it
         _is_ significant. -->
    <xsl:variable name="event_b"
        select="ROW[@EVENT_TYPE = 'B']/PRICE/ROW/@EVENT_PRICE"/>
    <xsl:variable name="event_p"
        select="ROW[@EVENT_TYPE = 'P']/PRICE/ROW/@EVENT_PRICE"/>

    <!-- Note use of translate() to translate commas to periods before converting
         to numbers -->
    <xsl:value-of select="number(translate($event_b, ',', '.')) - number(translate($event_p, ',', '.'))" />
  </xsl:template>

</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 2017-09-01
    • 2019-08-06
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    • 2023-02-01
    • 1970-01-01
    相关资源
    最近更新 更多