问题
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