Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

change attribute name using XML::Twig

by aakikce (Acolyte)
on Apr 26, 2007 at 12:58 UTC ( [id://612185]=perlquestion: print w/replies, xml ) Need Help??

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>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://612185]
Approved by jettero
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (8)
As of 2024-04-24 10:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found