in reply to Re: XML tag replace
in thread XML tag replace

Apology for the message. I want to make the number of lines short considering server space. Let me explain it in detail.
I have the code like this

<author-group> <auth> </auth> <auth> </auth> <auth> </auth> <aff> </aff> <aff> </aff> <corres id="cor1"> </corres> </author-group>

I want to group all <auth> tags in a separate tag like

<author-group> <XXX> <auth> </auth> <auth> </auth> <auth> </auth> <XXX> <aff> </aff> <aff> </aff> <corres id="cor1"> </corres> </author-group>

I tried this using regular expression. I can’t get the “</auth>carriage return <aff>”. Please advice.

Could you please suggest any book to learn basics of perl.

Edit: g0n - code tags

Replies are listed 'Best First'.
Re^3: XML tag replace
by GrandFather (Saint) on Jul 14, 2007 at 00:22 UTC

    Learning Perl would be a good book to start with. You may find that the monastery's Tutorials section is good for a quick start.

    Here's a starting point for your XML manipulation using XML::TreeBuilder:

    use strict; use warnings; use XML::TreeBuilder; my $xml = <<XML; <author-group> <auth> </auth> <auth> </auth> <auth> </auth> <aff> </aff> <aff> </aff> <corres id="cor1"> </corres> </author-group> XML my $root = XML::TreeBuilder->new (); $root->parse ($xml); for my $group ($root->look_down ('_tag', 'author-group')) { my @auths = $group->look_down ('_tag', 'auth'); my $newRoot = HTML::Element->new ('xxx'); $_->detach () for @auths; $newRoot->push_content (@auths); $group->unshift_content ($newRoot); } print $root->as_XML ();

    Prints:

    <author-group><xxx><auth> </auth><auth> </auth><auth> </auth></xxx> <aff> </aff> <aff> </aff> <corres id="cor1"> </corres> </author-group>

    Still pretty trivial eh.;)


    DWIM is Perl's answer to Gödel
Re^3: XML tag replace
by toolic (Bishop) on Jul 13, 2007 at 17:31 UTC
    As far as learning Perl basics goes, "Learning Perl", published by O'Reilly & Associates, is excellent. I bought it years ago, and I still refer to it.