我想在不同级别执行两个元素的乘积之和 [英] I want perform sum of product of two elements at differnt levels

查看:28
本文介绍了我想在不同级别执行两个元素的乘积之和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下代码,但它不起作用.我取了一个字符串并在其中写入 XSLT 并加载它 XslCompiledTransform 对象.

I tried for following code but its not working. I have taken one string and write XSLT in it and load it XslCompiledTransform object.

 <xsl:sequence select=
        "sum(//Item/(cost * related_id/Item/quantity))"/>

源 XML:

<AML>
  <Item>
    <cost>
      40
    </cost>
    <related_id>
      <Item>
        <quantity>2</quantity>
      </Item>
    </related_id>
  </Item>
  <Item>
    <cost>
      50
    </cost>
    <related_id>
      <Item>
        <quantity>10</quantity>
      </Item>
    </related_id>
  </Item>
</AML>

推荐答案

正如我在评论中所说,xsl:sequence 和您尝试使用的 XPath 语法在 XSLT 中不可用1.0(XslCompiledTransform 使用的),但是您可以通过使用递归模板来实现公式的总和:

As I said in the comments, xsl:sequence and that XPath syntax you're trying to use aren't available in XSLT 1.0 (which XslCompiledTransform uses), but you can achieve a sum of formulas by using a recursive template:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" indent="yes"/>

  <xsl:template match="/">
    <xsl:call-template name="SumSet">
      <xsl:with-param name="items" select="/*/Item" />
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="SumSet">
    <xsl:param name="items" />

    <xsl:if test="$items">
      <xsl:variable name="currentValue">
        <xsl:apply-templates select="$items[1]" />
      </xsl:variable>
      <xsl:variable name="remainderSum">
        <xsl:call-template name="SumSet">
          <xsl:with-param name="items" select="$items[position() > 1]" />
        </xsl:call-template>
      </xsl:variable>

      <xsl:value-of select="$currentValue + $remainderSum"/>
    </xsl:if>
    <xsl:if test="not($items)">0</xsl:if>
  </xsl:template>

  <xsl:template match="Item">
    <xsl:value-of select="cost * related_id/Item/quantity"/>
  </xsl:template>
</xsl:stylesheet>

当在您的输入 XML 上运行时,结果是:

When this is run on your input XML, the result is:

580

这将是通用方法,但既然您已经提到您正在使用 XslCompiledTransform,您可以使用 msxsl:node-set() 来稍微简化此任务:

That would be the generic approach, but since you've mentioned that you're using XslCompiledTransform, you can use msxsl:node-set() to simplify this task a bit:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt">
  <xsl:output method="text" indent="yes"/>

  <xsl:template match="/">
    <xsl:variable name="values">
      <xsl:apply-templates select="/*/Item" />
    </xsl:variable>

    <xsl:value-of select="sum(msxsl:node-set($values)/*)"/>
  </xsl:template>

  <xsl:template match="Item">
    <itemValue>
      <xsl:value-of select="cost * related_id/Item/quantity"/>
    </itemValue>
  </xsl:template>
</xsl:stylesheet>

在输入 XML 上运行时,这也会产生值 580.

This also produces the value 580 when run on your input XML.

这篇关于我想在不同级别执行两个元素的乘积之和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆