I am new to XSLT and am having a problem with removing duplicates from a simple XML file. Spent a lot of time trying to get it but it’s never quite right. Here is the source file:
<?xml version="1.0" encoding="UTF-16"?>
<language>
<lang name="welcome">welcom</lang>
<lang name="open">Open</lang>
<lang name="close">Close</lang>
<lang name="welcome">Welcome</lang>
<lang name="copy">Copy</lang>
</language>
Desired output is this:
<?xml version="1.0" encoding="UTF-16"?>
<language>
<lang name="open">Open</lang>
<lang name="close">Close</lang>
<lang name="welcome">Welcome</lang>
<lang name="copy">Copy</lang>
</language>
The actual files are much larger than this and “lang” and “name” may change later in the file, and I only want to keep the last duplicate. Basically, if the tag and attributes are duplicated, only keep the last entry. I hope this is possible with XSLT 1.0. If not, I can always use multiple scripts in case lang does change to something else. Thank you in advance!
The following XSLT should answer your question :
This way, you filter every
langelement that have a following siblinglangelement with the same value for thenameattribute.