【问题标题】:How to product and sum the values of two different nodes of a different complex element using xslt?如何使用 xslt 对不同复杂元素的两个不同节点的值进行乘积和求和?
【发布时间】:2016-04-23 18:46:47
【问题描述】:

我的 XML 是

<root> <source> <item> <id>111</id> <qty>2</qty> </item> <item> <id>222</id> <qty>1</qty> </item> <item> <id>333</id> <qty>4</qty> </item> </source> <target> <record> <id>111</id> <price>1000</price> </record> <record> <id>333</id> <price>500</price> </record> </target> </root>

现在我需要匹配源/项目和目标/记录的 id 元素,如果它匹配我需要生产

来源/项目/数量 * 目标/记录/价格

一旦所有匹配案例的产品都完成了,我应该对所有产品价值求和,结果应该是

4000 即(所有匹配元素的总和(数量 * 价格)。)

如何实现这一点,请帮助我,在此先感谢

【问题讨论】:

    标签: xml xslt xslt-1.0


    【解决方案1】:

    我已经修改了来自 Dimitre Novatchev 的 answer 以满足您的要求。代码如下:

    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:strip-space elements="*"/>
        <xsl:output indent="yes" omit-xml-declaration="yes"/>
    
        <xsl:key name="target" match="record" use="id" />
    
        <xsl:template match="/">
            <xsl:call-template name="sumProducts">
                <!-- get only sources with respective equivalent targets -->
                <xsl:with-param name="pList" select="*/*/item[key('target', id)]"/>
                <!-- a list of target records -->
                <xsl:with-param name="pList2" select="*/*/record"/>
            </xsl:call-template>
        </xsl:template>
    
        <xsl:template name="sumProducts">
            <xsl:param name="pList"/>
            <xsl:param name="pList2"/>
            <xsl:param name="pAccum" select="0"/>
    
            <xsl:choose>
                <xsl:when test="$pList">
                    <xsl:variable name="vHead" select="$pList[1]"/>
                    <xsl:variable name="vHead2" select="$pList2"/>
    
                    <xsl:call-template name="sumProducts">
                        <xsl:with-param name="pList" select="$pList[position() > 1]"/>
                        <xsl:with-param name="pList2" select="$pList2"/>
                        <xsl:with-param name="pAccum"
                            select="$pAccum + $vHead/qty * $vHead2[id = $vHead/id]/price"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$pAccum"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

    • & Dimitre 非常感谢您的回答,这很有帮助
    【解决方案2】:

    在 XSLT 2.0 中,您可以:

    <xsl:key name="target" match="record" use="id" />
    
    <xsl:template match="/root">
        <result>
            <xsl:value-of select="sum(source/item[key('target', id)]/(qty * key('target', id)/price))"/>
        </result>
    </xsl:template>
    

    【讨论】:

    • 感谢您的回答,我们正在使用 xslt 1.0,因为我们的框架仅支持它。
    • 那你为什么把你的问题标记为 XSLT 2.0?
    • 我认为知道2.0的人也可以帮助我解决这个问题。
    • A tag is a word or phrase that describes the topic of the question. 这并不是为了描述您认为可以帮助您的人。谢谢你浪费我的时间。
    猜你喜欢
    • 2013-01-22
    • 2011-10-27
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    相关资源
    最近更新 更多