2

嗨,我对 XSLT 很陌生,所以需要一些关于简单 XSL 代码的帮助。

我的输入 XML

<?xml version="1.0" encoding="ASCII"?>
<Node Name="Person"  Received="1"  Good="1" Bad="0"  Condition="byPerson:1111">
</Node>
<Node Name="Person"  Received="1"  Good="1" Bad="0"  Condition="byPerson:1111">
</Node>
<Node Name="Person"  Received="1"  Good="1" Bad="0"  Condition="byPerson:2222">
</Node>
<Node Name="Person"  Received="1"  Good="1" Bad="0"  Condition="byPerson:2222">
</Node>
<Node Name="Person"  Received="1"  Good="1" Bad="0"  Condition="byPerson:3333">
</Node>

我期望结果是所有 Received ,good 和 Bad 的总和,但每个独特条件只需要添加一次。

像这样的东西

<?xml version="1.0" encoding="ASCII"?>
<Received>3</Received >
<Good>3</Good>
<Bad>0</Bad>

我正在尝试下面的代码,但到目前为止没有成功,只是得到所有东西的总和,只想对每个“条件”求和一次。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


<xsl:template match="/">


<xsl:value-of select= "sum(Node@Received)"/>
<xsl:value-of select= "sum(Node/@Good)"/>
<xsl:value-of select= "sum(Node/@Bad)"/>  


</xsl:template> 

4

2 回答 2

1

下面的样式表使用来按 的值对元素xsl:key进行分组。使用Meunchien 方法和,选择每个唯一元素的第一个元素,然后生成所选元素的属性。<node>@Conditionkey()generate-id()node@Conditionsum()node

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output indent="yes"/>

    <xsl:key name="nodesByCondition" match="Node" use="@Condition"/>

    <xsl:template match="/">
        <results>

            <xsl:variable name="distinctNodes" 
                select="*/Node[generate-id() = 
                generate-id(key('nodesByCondition', @Condition)[1])]"/>

            <Received>
                <xsl:value-of select= "sum($distinctNodes/@Received)"/>
            </Received>
            <Good><xsl:value-of select= "sum($distinctNodes/@Good)"/></Good>
            <Bad><xsl:value-of select= "sum($distinctNodes/@Bad)"/></Bad>                
        </results>
    </xsl:template> 
</xsl:stylesheet>
于 2013-05-22T02:34:40.940 回答
0

在 XSLT 2.0 中,您可以使用distinct-values()

于 2013-09-16T22:27:27.440 回答