Re: spot/remove repeated elements in XML documet

"Jenda Krynicky" <[email protected]>
Newsgroups gmane.comp.lang.perl.xml
Message-ID <[email protected]>
From: Manuel Souto Pico <[email protected]>
> I have a long XML document in which several branches have repeated 
> contents, for example:
> 
> <document>
> ...
>     <entry ="10">
>        <Eng>house</Eng>
>        <Spa>casa</Spa>
>     </entry>
>     <entry ="11">
>        <Eng>shoe</Eng>
>        <Spa>zapato</Spa>
>     </entry>
>     <entry ="12">
>        <Eng>house</Eng>
>        <Spa>casa</Spa>
>     </entry>
> ...
> </document>
> 
> 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...

Something like this would work:

use XML::Rules;

my $parser = XML::Rules->new(
	style => 'filter',
	rules => {
		_default => 'raw extended',
		entry => sub {
			my ($tag, $attr, $context, $parents, $parser) = @_;
			my $content_string = "$attr->{':Eng'}{_content}$;$attr-
>{':Spa'}{_content}";
			if ($parser->{parameters}{seen}{$content_string}++) {
				return
			} else {
				return $tag => $attr
			}
		},
	}
);

$parser->filter( \*DATA);

__DATA__
<document>
    <entry id="10">
       <Eng>house</Eng>
       <Spa>casa</Spa>
    </entry>
    <entry id="11">
       <Eng>shoe</Eng>
       <Spa>zapato</Spa>
    </entry>
    <entry id="12">
       <Eng>house</Eng>
       <Spa>casa</Spa>
    </entry>
</document>



HTH, Jenda
===== [email protected] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
	-- Terry Pratchett in Sourcery

_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.