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

Hello Monks,

I have a tag like <img src="path of image"/> in a xml document. I want to change the name of the attribute using XML::Twig.

I tried like the following:-

for $item ($twig->get_xpath("//img")) { $item->change_att_name('src', 'links') }

But I didn't get result. pls help me

thanks

Aakikce

Replies are listed 'Best First'.
Re: change attribute name using XML::Twig
by Fletch (Bishop) on Apr 26, 2007 at 13:07 UTC

    You're doing something else wrong then. This works just fine.

    #!/usr/bin/env perl use strict; use warnings; use XML::Twig (); my $twig = XML::Twig->new; $twig->parse( qq{<img src="/foo/bar.jpg" />\n} ); for my $img ( $twig->get_xpath( "//img" ) ) { $img->change_att_name( 'src', 'links' ); } $twig->print; exit 0; __END__ ## Outputs: <img links="/foo/bar.jpg"/>

      Hello Monks,

      Sorry. It is working. please ignore this.

        Tell us what you were doing wrong. It's nice having a reference.

        Igor 'izut' Sutton
        your code, your rules.

Re: change attribute name using XML::Twig
by mirod (Canon) on Apr 26, 2007 at 13:12 UTC

    It worked when I tried it (see below). Does $twig->get_xpath("//img") return any result?

    What I tried:

    #!/usr/bin/perl use strict; use warnings; use XML::Twig; my $twig= XML::Twig->parse( \*DATA); for my $item ($twig->get_xpath("//img")) { $item->change_att_name('src', 'links') } $twig->print; __DATA__ <html> <body> <p><img src="foo"/></p> <p><img src="bar"/></p> </body> </html>