How can I eliminate duplicate nodes based on values of multiple (more than 1) attributes? Also the attribute names are passed as parameters to the stylesheet. Now I am aware of the Muenchian method of grouping that uses a <xsl:key>
element. But I came to know that XSLT 1.0 does not allow paramters/variables in <xsl:key>
.
Is there another method(s) to achieve duplicate nodes removal? It is fine if it not as efficient as the Munechian method.
Update from previus question:
XML:
<data id = "root"> <record id="1" operator1='xxx' operator2='yyy' operator3='zzz'/> <record id="2" operator1='abc' operator2='yyy' operator3='zzz'/> <record id="3" operator1='abc' operator2='yyy' operator3='zzz'/> <record id="4" operator1='xxx' operator2='yyy' operator3='zzz'/> <record id="5" operator1='xxx' operator2='lkj' operator3='tyu'/> <record id="6" operator1='xxx' operator2='yyy' operator3='zzz'/> <record id="7" operator1='abc' operator2='yyy' operator3='zzz'/> <record id="8" operator1='abc' operator2='yyy' operator3='zzz'/> <record id="9" operator1='xxx' operator2='yyy' operator3='zzz'/> <record id="10" operator1='rrr' operator2='yyy' operator3='zzz'/> </data>
If you want to pass in the attribute names as a parameter then one approach could be a two step transformation where the first step takes any XML input and simply the attribute names and the element names as parameters to generate a second stylesheet that then eliminates the duplicates. Here is an example first stylesheet:
It takes four parameters:
parent-name
: the name of the element containing those elements of which you want to eliminate duplicateselement-name
: the name of those elements of which you want to eliminate duplicatesatt-names
: a comma separated list of attribute namessep
: a separator character that should not occur in attribute values in the input XMLThe stylesheet then generates a second stylesheet that applies Muenchian grouping to eliminate duplicates. For instance with the default parameters given in the stylesheet Saxon 6.5.5 generates the following stylesheet:
This can the be applied to an XML document like
and the output is
Use this transformation (simple and no need to generate a new stylesheet):
When applied to the XML document of your previous question:
The wanted, correct result is produced:
Other approach for a single transformation in two steps:
Output:
Edit: Also without named template for
@local-key
generationNote: If you are positive sure that attributes order is the same for all elements, then you could remove the sorting.