[xsl] Find elements with same key and merge sub-elements

Subject: [xsl] Find elements with same key and merge sub-elements
From: "Larry Hayashi lhtrees@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 26 Sep 2023 19:34:11 -0000
Hi,

I have an xml file containing dictionary entries with their
meanings/senses. I'd like to find Entries that have 2 or more senses that
have the same gloss. Sometimes an entry will have more than one sense but
each sense will have its own unique gloss. For the entries that have senses
with identical glosses, I want to merge those senses and their contents as
shown below.

Input
<Root>
    <Entry form="voiture">
        <Sense num="1">
            <Gloss>car</Gloss>
            <Index>
                <IndexItem>automobile</IndexItem>
                <IndexItem>vehicle</IndexItem>
                <IndexItem>car</IndexItem>
            </Index>
        </Sense>
        <Sense num="2">
            <Gloss>car</Gloss>
            <Category>transportation</Category>
        </Sense>
    </Entry>
    <Entry>
        <!-- ...Etc.... -->
    </Entry>
</Root>

Desired Output:
<Root>
    <Entry form="voiture">
        <Sense num="1">
            <Gloss>car</Gloss>
            <Index>
                <IndexItem>automobile</IndexItem>
                <IndexItem>vehicle</IndexItem>
                <IndexItem>car</IndexItem>
            </Index>
            <Category>transportation</Category>
        </Sense>
    </Entry>
    <Entry>
        <!-- ...Etc.... -->
    </Entry>
</Root>

Thanks in advance for any pointers as to what might be the most efficient
approach.

Larry

Current Thread