在XSLT中求和3个嵌套的for-for的总数

托马斯·鲁伊斯

我有以下XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.3ds.com/xsd/XPDMXML">
    <xsl:output method="text" doctype-public="XSLT-compat" encoding="ISO-8859-1"/>
    <xsl:template match="/">
        <xsl:for-each select="/foo/bar/AAA[Owned/text()='1']">
            <xsl:variable name="vOP">
                <xsl:value-of select="./Instancing"/>
            </xsl:variable>
            <xsl:for-each select="/foo/bar/BBB[Owned[text()=$vOP]]">
                <xsl:variable name="vTO">
                    <xsl:value-of select="./Instancing"/>
                </xsl:variable>
                <xsl:for-each select="/foo/bar/CCC[Owned[text()=$vTO]]">
                    <xsl:variable name="vIE">
                        <xsl:value-of select="./Instancing"/>
                    </xsl:variable>
                    <xsl:text>"COUNT": </xsl:text><xsl:value-of select="count(/foo/buzz/DDD[Owned[text()=$vIE]])"/><xsl:text>,</xsl:text>
                </xsl:for-each>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

这是一个示例输入

<?xml version='1.0' encoding='utf-8'?>
<foo>
    <bar>
        <AAA>
            <Owned>1</Owned>
            <Instancing>2</Instancing>
        </AAA>
        <BBB>
            <Owned>2</Owned>
            <Instancing>3</Instancing>
        </BBB>
        <CCC>
            <Owned>3</Owned>
            <Instancing>4</Instancing>
        </CCC>
        <CCC>
            <Owned>3</Owned>
            <Instancing>5</Instancing>
        </CCC>
        <CCC><Owned>4</Owned></CCC>
    </bar>
    <buzz>
        <DDD><Owned>4</Owned></DDD>
        <DDD><Owned>4</Owned></DDD>
        <DDD><Owned>5</Owned></DDD>
        <DDD><Owned>3</Owned></DDD>
        <CCC><Owned>4</Owned></CCC>
    </buzz>
</foo>

有没有一种方法可以获取最近value-of通话的总价值(SUM)并可能删除所有foreach?

的输出应为3(2 + 1)。

马丁·洪恩

您可以将第一次计算的结果存储在变量中,然后对变量的值求和(并输出它们:

<xsl:template match="/">
    <xsl:variable name="counts" as="element(count)*">
        <xsl:for-each select="/foo/bar/AAA[Owned = 1]">
            <xsl:variable name="vOP" select="Instancing"/>
            <xsl:for-each select="/foo/bar/BBB[Owned = $vOP]">
                <xsl:variable name="vTO" select="Instancing"/>
                <xsl:for-each select="/foo/bar/CCC[Owned = $vTO]">
                    <xsl:variable name="vIE" select="Instancing"/>
                    <count>
                        <xsl:value-of select="count(/foo/buzz/DDD[Owned = $vIE])"/>
                    </count>
                </xsl:for-each>
            </xsl:for-each>
        </xsl:for-each>            
    </xsl:variable>
    <xsl:value-of select="$counts/concat('COUNT:', .), concat('SUM:', sum($counts))" separator=","/>
</xsl:template>

至于使用更紧凑的代码来做到这一点,您可以使用键来遵循交叉引用:

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

    <xsl:key name="ref" match="bar/*" use="Owned"/>

    <xsl:key name="buzz" match="buzz/DDD" use="Owned"/>

    <xsl:template match="/">
        <xsl:variable name="counts" as="element(count)*">
            <xsl:for-each select="key('ref', key('ref', key('ref', '1')/Instancing)/Instancing)">
                <count>
                    <xsl:value-of select="sum(count(key('buzz', Instancing)))"/>
                </count>
            </xsl:for-each>
        </xsl:variable>
        <xsl:value-of select="$counts/concat('COUNT:', .), concat('SUM:', sum($counts))" separator=","/>
    </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/nc4NzRs/4

我不知道您输入数据的可变性,也许您将需要不同的键来用于的不同子元素bar,对于您的示例数据而言,单个键就足够了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章