[prev in list] [next in list] [prev in thread] [next in thread]
List: xsl-list
Subject: Re: [xsl] Find elements with same key and merge sub-elements
From: "Martin Honnen martin.honnen () gmx ! de" <xsl-list-service () lists ! mulberrytech ! com>
Date: 2023-09-26 19:42:08
Message-ID: 20230926154137.b328c747 () lists ! mulberrytech ! com
[Download RAW message or body]
On 26.09.2023 21:34, Larry Hayashi lhtrees@gmail.com wrote:
> 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.
>
It looks like a grouping problem so in XSLT 3 you can do something like
the following (it is not quite clear whether common elements of a group
of Entry elements with the same Gloss need to simply copied or also
somehow merged)
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Entry">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:for-each-group select="Sense" group-by="Gloss">
<xsl:copy>
<xsl:apply-templates select="@*, Gloss, current-group()/(*
except Gloss)"/>
</xsl:copy>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/651070
or by email: xsl-list-unsub@lists.mulberrytech.com
--~--
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic