'XSLT remove duplicate element values

Here is my xml:

<?xml version="1.0" encoding="UTF-8"?>
<MD_LegalConstraints>
    <otherConstraints><CharacterString>Test 1</CharacterString></otherConstraints>
    <otherConstraints><CharacterString>Test 2</CharacterString></otherConstraints>
    <otherConstraints><CharacterString>Test 1</CharacterString></otherConstraints>
    <otherConstraints><CharacterString>Test 3</CharacterString></otherConstraints>
</MD_LegalConstraints>
    

I want to translate the xml above as below:

<?xml version="1.0" encoding="UTF-8"?>
<data>
<rights>
    <rightsStatement>Test 1</rightsStatement>
</rights>
<rights>
    <rightsStatement>Test 2</rightsStatement>
</rights>
<rights>
    <rightsStatement>Test 3</rightsStatement>
</rights>
</data>

I try to follow (XSL to remove duplicate records) to transform and remove the duplicate values. But it still contains the duplicate values (xslt version = 2.0)

<xsl:key name="legal-text" match="otherConstraints" use="."/>
<xsl:template match="MD_LegalConstraints">
<data>
<xsl:for-each select="otherConstraints[count(. | key('legal-text', .)[1]) = 1]">
<rights>
<rightsStatement>
<xsl:value-of select="."/>
</rightsStatement>
</rights>
</xsl:for-each>
</data>
</xsl:template>


Solution 1:[1]

Maybe this helps you (2.0), as Martin Honnen proposes in the comments:

<xsl:template match="MD_LegalConstraints">
    <xsl:for-each-group select="otherConstraints/CharacterString" group-by=".">
        <!-- do something -->
    </xsl:for-each-group>
</xsl:template>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 barbwire