获取唯一值并在XSLT中添加值
您好,我是XSLT新手,所以需要一些关于简单XSL代码的帮助 我的输入XML获取唯一值并在XSLT中添加值,xslt,unique,Xslt,Unique,您好,我是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=
<?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>
我期望结果是所有收到的结果的总和,好的和坏的,但是每个唯一的条件只需要添加一次
像这样的
<?xml version="1.0" encoding="ASCII"?>
<Received>3</Received >
<Good>3</Good>
<Bad>0</Bad>
3.
3.
0
我尝试了下面的代码,但到目前为止没有成功,只是得到了所有内容的总和,只想得到每个“条件”的总和一次
<?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>
下面的样式表使用一个属性来根据
@条件的值对
元素进行分组。使用with and,为每个唯一的@条件
选择第一个节点
元素,然后生成所选节点
元素的属性
<?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>
在XSLT2.0中,您可以使用distinct-values()您的输入不是格式良好的XML。那么,您想要三个唯一条件中每一个的属性的总和?在3个独特的条件下重复这三个要素?感谢Hansen的快速响应。。事实上,我期待更多的如下,如果1111进来,它应该将收到的计算为一,因此对于1个条件值,它应该是一,并为下一个唯一条件值增加。3好的,那么假设与给定的@条件
匹配的节点是一致的?不会有一些匹配的1111对@received]、
@good或
@bad>具有不同的值?是的,如果条件为1111,后续的值将是相同的,我所需要的是计算不同条件的首次出现次数,上面的答案给我所有的零,我是否需要为每个条件添加一个值,以便它可以相加?谢谢againHi Hansen,这方面的任何更新都会对我非常有帮助。谢谢。您输入的XML是什么样子的?您发布的内容格式不正确,无法解析。我添加了一个document元素来包装该内容,样式表希望节点
元素嵌套在该document元素下。您可能需要调整distinctNodes
select语句中的XPath以匹配您的XML结构。