如何从xml文件中删除重复的元素?
时间:2020-03-06 14:33:58 来源:igfitidea点击:
我有一个XML文件,例如
<ns0:Employees xmlns:ns0="http://TestIndexMap.Employees"> <Employee FirstName="FirstName_0" LastName="LastName_1" dept="dept_2" empNumber="1"> <Schedules> <Schedule Date_join="2008-01-20" Date_end="2008-01-30" /> </Schedules> </Employee> <Employee FirstName="FirstName_0" LastName="LastName_1" dept="dept_2" empNumber="2"> <Schedules> <Schedule Date_join="2008-01-20" Date_end="2008-01-30" /> </Schedules> </Employee> <Employee FirstName="FirstName_2" LastName="LastName_1" dept="dept_2" empNumber="2"> <Schedules> <Schedule Date_join="2007-01-21" Date_end="2007-12-30" /> </Schedules> </Employee> <Employee FirstName="FirstName_2" LastName="LastName_1" dept="dept_2" empNumber="2"> <Schedules> <Schedule Date_join="2007-01-21" Date_end="2007-12-30" /> <Schedule Date_join="2008-06-20" Date_end="2008-01-30" /> </Schedules> </Employee> </ns0:Employees>
我想删除基于fistname,姓氏,date_join和data_end的重复项。
请,有人可以解释如何使用XSLT实现这一目标吗?
解决方案
以下是一些如何根据元素名称和id字段删除重复项的示例。将其扩展到任意字段应该不太困难。
Q: Expansion. A part of my xml looks like this:
<location> <state>xxxx</state> </location> <location> <state>yyyy</state> </location> <location> <state>xxxx</state> </location>
The desired output is:
xxxx yyyy
That is, duplicate values of state should not be printed. Can this be done?
<xsl:variable name="unique-list" select="//state[not(.=following::state)]" /> <xsl:for-each select="$unique-list"> <xsl:value-of select="." /> </xsl:for-each>