in reply to XML::Twig Text replacement
use strict; use warnings; use XML::Twig qw( ); binmode STDOUT; my $t = XML::Twig->new( twig_handlers => { '/Profile/Application' => sub { my $Id = $_->att('Id'); my $CsID = (split(/\//, $Id))[-1]; $_->set_att(Id => $CsID); }, }, ); $t->parsefile($ARGV[0]); $t->flush();
use strict; use warnings; use XML::LibXML qw( ); use XML::LibXML::XPathContext qw( ); my $doc = XML::LibXML->new()->parse_file($ARGV[0]); my $root = $doc->documentElement(); my $xpc = XML::LibXML::XPathContext->new(); $xpc->registerNs(x => 'xxxxxxxxx'); for ($xpc->findnodes('/x:Profile/x:Application', $root)) { my $Id = $_->getAttribute('Id'); my $CsID = (split(/\//, $Id))[-1]; $_->setAttribute(Id => $CsID); } binmode STDOUT; print $doc->toString();
XML::LibXML is a bit wordier than XML::Twig (the 2 xpc lines) in order to handle namespaces correctly. (XML::Twig doesn't.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: XML::Twig Text replacement
by mirod (Canon) on May 01, 2010 at 21:13 UTC | |
by ikegami (Patriarch) on May 02, 2010 at 01:18 UTC | |
by mirod (Canon) on May 03, 2010 at 09:12 UTC | |
by ikegami (Patriarch) on May 03, 2010 at 15:44 UTC | |
|
Re^2: XML::Twig Text replacement
by Gizmo (Novice) on May 01, 2010 at 12:16 UTC |