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

In reply to Re: Removing duplicate subtrees from XML by dakkar
in thread Removing duplicate subtrees from XML by matth

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.