Re: spot/remove repeated elements in XML documet
Michael Ludwig <[email protected]>
| Newsgroups | gmane.comp.lang.perl.xml |
|---|---|
| Message-ID | <[email protected]> |
Manuel Souto Pico schrieb:
> <entry ="10">
> <Eng>house</Eng>
> <Spa>casa</Spa>
> </entry>
The <entry> element, of course, is not valid.
> <entry ="12">
> <Eng>house</Eng>
> <Spa>casa</Spa>
> </entry>
> where as you can see, entries 10 and 12 have identical contents.
>
> My question is: Is there any easy way to spot and/or remove repeated
> elements? I suppose it could be done with XSL or a perl module for
> XML...
It is not complicated. First, define identity. In your case, that may be
the concatenation of <Eng> and <Spa>. (It might also be something else.)
Then, in XSLT 2.0 it would be really easy. But with Perl, you only have
XSLT 1.0 at your disposal.
Here's an XSLT 1.0 solution. The algorithm is simple: Find duplicates
and eliminate all but the first one. The implementation may not be
obvious at first sight, but it is not difficult. Do some googling for
"Muenchian Grouping".
Michael Ludwig
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="unlikely" select="'||--||'"/>
<xsl:key name="dupl" match="entry" use="concat( Eng, $unlikely, Spa)"/>
<xsl:template match="document">
<xsl:apply-templates select="entry"/>
</xsl:template>
<xsl:template match="entry">
<xsl:variable name="lookup-key" select="concat(Eng, $unlikely, Spa)"/>
<xsl:if test="
generate-id() =
generate-id( key( 'dupl', $lookup-key)[ 1 ] )">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs