如何使用 XSLT 获取唯一 XML 元素的值 [英] How to get value of unique XML element by using XSLT

查看:27
本文介绍了如何使用 XSLT 获取唯一 XML 元素的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要来自 XML 文档的唯一元素列表.如果元素的出现次数超过 1,我希望在我的输出中出现最后一次:

I want the unique list of elements from XML document. If the occurrence of element is more than 1, i want to have the last occurrence in my output:

请参考以下 XML 获取唯一列表:

Please refer the below XML for getting unique list:

<Organization>
    <Fund>
      <id>001</id>
      <name>ABC Ltd</name>
    </Fund>
    <Fund>
      <id>002</id>
      <name>DEF Limited</name>
    </Fund>
    <Fund>
      <id>001</id>
      <name>ABC Ltd.</name>
    </Fund>
    <Fund>
      <id>002</id>
      <name>DEF Corporation</name>
    </Fund>
    <Fund>
      <id>003</id>
      <name>XYZ LLC.</name>
    </Fund>
 </Organization>

转换应该输出以下结果:

The transform should output the below result:

<Organization>
    <Fund>
      <id>001</id>
      <name>ABC Ltd.</name>
    </Fund>
    <Fund>
      <id>002</id>
      <name>DEF Corporation</name>
    </Fund>
    <Fund>
      <id>003</id>
      <name>XYZ LLC.</name>
    </Fund>
 </Organization>

*请注意id 001和002的基金名称标签的变化.

*Please note the change in name tag of Fund with id 001 and 002.

需要 XSLT1 中的示例代码.提前致谢.

Need the sample code in XSLT1. Thanks in advance.

推荐答案

使用 muenching 分组:

Using muenching grouping:

为每个 fund_by_id 创建一个键

Create a key for each fund_by_id

通过仅选择 ID 与密钥组中最后一个 ID 匹配的那些基金,从每个密钥复制最后一个基金.

copy the last fund from each key by selecting only those funds whose ID matches the last ID in a key group.

<xsl:key name="funds_by_id" match="Fund" use="id"/>
<xsl:template match="Organization">
  <Organization>
      <xsl:copy-of select="Fund[generate-id() = 
                generate-id(key('funds_by_id',id)[last()])]"/>
  </Organization>
</xsl:template>

这篇关于如何使用 XSLT 获取唯一 XML 元素的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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