[xsl] removing duplicate elements based on two or more childs

Subject: [xsl] removing duplicate elements based on two or more childs
From: Manuel Souto Pico <manuel.souto@xxxxxxxxxxxxxx>
Date: Tue, 25 Aug 2009 16:45:45 +0200
Hi,

I had tried to get this done already some long ago, but due to lack of time and the difficulty of the task, I dropped it and did it by other less elegant means. Now I neede it again and I think it deserves a chance, with a bit of help I'm sure I'll manage to get it done and it will be extremely useful both for me and I guess for a lot of people.

A simplified explanation. In an XML file I have records, which contain languages, which contain terms. The path down to any term would be /doc/body/text/record/lang/term. For example:

<record id="1">
   <lang id="fra">
       <term>banque</term>
   </lang>
   <lang id="eng">
       <term>bank</term>
   </lang>
</record>
<record id="2">
   <lang id="fra">
       <term>banque</term>
   </lang>
   <lang id="eng">
       <term>bench</term>
   </lang>
</record>
<record id="3">
   <lang id="fra">
       <term>banque</term>
   </lang>
   <lang id="eng">
       <term>bank</term>
   </lang>
</record

As you can see, the French term is the same in the three records. If we applied a duplicate removing function based on the French term, we would end up with only one record. However, what I need is to remove duplicates taking into account the terms in all languages, here only two (French and English), but it should be extensible to n languages.

The expected outcome would contain only records 1 and 3 (that is, two distinct concepts, the bench and the bank). Notice that it is the whole parent element record which must not be generated if the terms that it contains are duplicates, not just the children, so this is not the ideal outcome:

<record id="1">
   <lang id="fra">
       <term>banque</term>
   </lang>
   <lang id="eng">
       <term>bank</term>
   </lang>
</record>
<record id="2">
   <lang id="fra">
       <term>banque</term>
   </lang>
   <lang id="eng">
       <term>bench</term>
   </lang>
</record>
<record id="3">
   <lang id="fra"/>
   <lang id="eng"/>
</record

I've tried using <xsl:apply-templates select="child::seg[not (. = preceding-sibling::seg)]" /> but, say, /record[@id=1]/lang[@id="fra"]/term and /record[@id=3]/lang[@id="fra"]/term are not siblings because they don't have the same parent. Perhaps this wasn't the best way to go anyway.

Any suggestion would be more than welcome. Thanks a lot.

Have nice evening,

--
Manuel Souto Pico

Current Thread