Reputation: 23
I have an XML with hundreds of records in this format (only 2 shown here)
<assessment>
<subject>
<Name>470 470 015</Name>
<LAD>3.446887644423149</LAD>
<LAD>3.446887644423149</LAD>
<LM>4.049357373198344</LM>
<LM>4.049357373198344</LM>
<RCA>3.283339910532276</RCA>
<RCA>3.283339910532276</RCA>
</subject>
<subject>
<Name>230 230067</Name>
<LAD>2.392278459908628</LAD>
<LAD>2.392278459908628</LAD>
<LM>3.50258194988953</LM>
<LM>3.50258194988953</LM>
<RCA>3.274917502338067</RCA>
<RCA>3.274917502338067</RCA>
</subject>
</assessment>
I would like to remove the duplicate nodes such that it looks like this
<assessment>
<subject>
<Name>470 470 015</Name>
<LAD>3.446887644423149</LAD>
<LM>4.049357373198344</LM>
<RCA>3.283339910532276</RCA>
</subject>
<subject>
<Name>230 230067</Name>
<LAD>2.392278459908628</LAD>
<LM>3.50258194988953</LM>
<RCA>3.274917502338067</RCA>
</subject>
</assessment>
Based on code noted below from a previous answer to a similar question (https://stackoverflow.com/a/37078109/18941723), I was able to process the first record, but not all the records in the file.
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:for-each-group select="*" group-by="name()">
<xsl:apply-templates select="current-group()[1]"/>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Upvotes: 2
Views: 225
Reputation: 52888
Try changing:
match="/*"
to:
match="subject"
Martin commented:
The current answers and your attempt identity duplicates based on the element/node name. I wonder whether you can have several elements of the same name (e.g.
LAD
) inside of asubject
element which can have a different value and what you would need to do in that case.
One option is to use a composite key by specifying composite="yes"
and adding .
or normalize-space()
to group-by
. This will keep one instance of each element name/value combo.
Example...
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" expand-text="yes">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="subject">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:for-each-group select="*" group-by="name(),normalize-space()" composite="yes">
<xsl:apply-templates select="current-group()[1]"/>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1
Reputation: 2714
You had the right approach, here's how I would do it :
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="subject">
<xsl:copy>
<xsl:for-each-group select="*" group-by="name()">
<xsl:copy-of select="current-group()[1]"/>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
See it working here : https://xsltfiddle.liberty-development.net/6qjt5SC
Upvotes: 1