http://www.perlmonks.org?node_id=1021679

slugger415 has asked for the wisdom of the Perl Monks concerning the following question:

Hello esteemed monks, I'm learning XML::Twig and have not been able to figure out how to insert an element in a particular location other that first_child or last_child. For example, here is my XML source:

<concept> <title>My Title</title> <prolog>some other info</prolog> </concept>

I want to insert a new element "short_desc" in between "title" and "prolog". How do I do that?

I can do this using first_child and last_child thusly:

my($twig,$con) = @_; $con->insert_new_elt('first_child','short_desc' =>{ AAA => 'hello'});

which results in

<short_desc AAA="hello"/> <title>Bat sonar</title> <prolog>some other info</prolog>

but what I want is:

<title>Bat sonar</title> <short_desc AAA="hello"/> <prolog>some other info</prolog>

Any pointers appreciated. I have been scanning the Twig doc but am not sure what to look for. I see mention of $opt_position but this is not defined anywhere. (The xml twig site seems to have been dead for a while...)

Thank you -- Scott

Replies are listed 'Best First'.
Re: XML::Twig inserting a new element, not first or last
by choroba (Cardinal) on Mar 04, 2013 at 17:22 UTC
    Scan for the "position options" and you will find before, after and within.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Wonderful, thank you!

Re: XML::Twig inserting a new element, not first or last
by mirod (Canon) on Mar 05, 2013 at 12:18 UTC
    The xml twig site seems to have been dead for a while...

    Are you sure? xmltwig.org should be working fine.