How to get value of unique XML element by using XSLT

放肆的年华 提交于 2019-12-11 10:37:20

问题


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:

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>

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

Need the sample code in XSLT1. Thanks in advance.


回答1:


Using muenching grouping:

Create a key for each fund_by_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>


来源:https://stackoverflow.com/questions/22771031/how-to-get-value-of-unique-xml-element-by-using-xslt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!