in reply to Swaping xml elements using XML::Twig
Well, while XML::Twig tries real hard to do what you want, it still doesn't understand that line:
$twig->get_xpath('//source')->move('before', $twig->get_xpath('//pagesource'));You have to pay attention to what the methods return: here get_xpath returns a list of XML::Twig::Elt. When you try to apply an XML::Twig method to that (unblessed) array... you get the error message you reported. You have to loop over the elements of that array yourself, and move them one by one. Bummer, I know ;--)
Here is my take on this:
#!/usr/bin/perl use strict; use warnings; use XML::Twig; my $twig = XML::Twig->new(pretty_print => 'nice'); $twig->parse(\*DATA); foreach my $elt ($twig->get_xpath('//source')) { $elt->move( before => $elt->prev_sibling( 'pagesource')); } $twig->print; # use print_to_file to print to a file __DATA__ <root> <pagesource> <para>Teacher's Guide Level A</para><para><graphic alt="title" links=" +Studio Logo R BW.tif"/></para> </pagesource> <source> <paragraph>ISBN-13: 978-1-4190-4181-5</paragraph><paragraph>ISBN-10: 1 +-4190-4181-9</paragraph> </source> <pagesource> <para>Teacher's Guide Level A</para><para><graphic alt="title" links=" +Studio Logo R BW.tif"/></para> </pagesource> <source> <paragraph>ISBN-13: 978-1-4190-4181-5</paragraph><paragraph>ISBN-10: 1 +-4190-4181-9</paragraph> </source> </root>
|
|---|