How to remove duplicate elements from an xml file?
Question
I have an XML file like
<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>
I would want to remove the duplicates based on the fistname, last name and date_join and data_end .
Please, can someone explain how to achive this with XSLT?
Solution
Here are some samples of how to remove duplicates based on element name and id field. It should be not too hard to extend this to arbitrary fields.
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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow