Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Removing duplicate subtrees from XML

by dakkar (Hermit)
on Dec 03, 2002 at 11:51 UTC ( [id://217182]=note: print w/replies, xml ) Need Help??


in reply to Removing duplicate subtrees from XML

I think it would be better if you used some of the XML modules out there. Parsing XML by hand is (nearly) always the wrong choice.

For example, I wrote a simple XSLT solution. It's not Perl, and it's not pretty, but it seems to work.

<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Tr +ansform"> <xsl:key name="spec_id" match="species" use="@id"/> <xsl:key name="block_id" match="block" use="concat(../@id,@id)"/> <xsl:template match="/root"> <!-- get the first species for each id --> <xsl:for-each select="/root/species[count(.|key('spec_id',@id)[1])=1] +"> <xsl:variable name="s_id" select="@id"/> <species> <xsl:attribute name="id"><xsl:value-of select="$s_id"/></xsl:attrib +ute> <!-- get the first block for each id --> <xsl:for-each select="/root/species[@id=$s_id]/block[count(.|key('b +lock_id',concat($s_id,@id))[1])=1]"> <xsl:variable name="b_id" select="@id"/> <block> <xsl:attribute name="id"><xsl:value-of select="$b_id"/></xsl:attr +ibute> <!-- for each block, get contents --> <xsl:for-each select="/root/species[@id=$s_id]/block[@id=$b_id]/n +ode()"> <xsl:copy/> </xsl:for-each> </block> </xsl:for-each> </species> </xsl:for-each> </xsl:template> <xsl:template match="block"> </xsl:template> </xsl:stylesheet>

This XSLT program transforms this:

<?xml version="1.0" ?> <root> <species id="1"> <block id="1"> stuff a </block> </species> <species id="1"> <block id="1"> stuff b </block> </species> <species id="1"> <block id="2"> stuff c </block> </species> <species id="1"> <block id="2"> stuff d </block> </species> <species id="2"> <block id="1"> stuff e </block> </species> <species id="2"> <block id="1"> stuff f </block> </species> <species id="2"> <block id="2"> stuff g </block> </species> <species id="2"> <block id="2"> stuff h </block> </species> </root>

into this:

<?xml version="1.0"?> <species id="1"><block id="1"> stuff a stuff b </block><block id="2"> stuff c stuff d </block></species><species id="2"><block id="1"> stuff e stuff f </block><block id="2"> stuff g stuff h </block></species>

To extend it, you have to:

  1. Add more keys
  2. Nest other for-each blocks
-- 
        dakkar - Mobilis in mobile

Replies are listed 'Best First'.
Re: Re: Removing duplicate subtrees from XML
by matth (Monk) on Dec 03, 2002 at 12:49 UTC
    XSLT look like a logical way of representing the data structure. However, it also looks like I would have to spend some time learning about this system. I may come back to it in the future. Is there first class perl module support for XSLT?

      XSLT is a language based on very different principles than Perl. It is very popular for XML transformation (although I could never quite figure out why ;--). See Recommend XSL module for a list of Perl modules that support it (usually by interfacing with external XSLT engines).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://217182]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-26 02:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found