dcbecker has asked for the wisdom of the Perl Monks concerning the following question:
using XML::Twig, not sure that is the most appropriate module, but I need to eventually print a modified XML version of the input. 'myobj' has two instances, but with unique attribute of "uniqueName". Ultimately, I'm trying to match on a myObj instance with a specific uniqueName text value, and modify one of the attrib? text values for that instance. Cant get the parser to filter on a particular uniqueName. And the path and parent reported when it does filter on one of the attribA values seems to be missing the myObj and "myattributes" nodes.
demo.xml:<myconfig> <myobj> <uniqueName> objA </uniqueName> <myattributes> <attribA> fooA </attribA> <attribB> fooB </attribB> </myattributes> </myobj> <myobj> <uniqueName> objB </uniqueName> <myattributes> <attribA> fooA </attribA> <attribB> fooB </attribB> </myattributes> </myobj> </myconfig>
t.pl:
Danuse strict; use warnings; use English; use XML::Twig; my $xmlFile = "demo.xml"; my $t = XML::Twig->new( keep_atts_order => 1, pretty_print => 'indented_a', twig_roots => { # following gives "unrecognized expression in handler" err +or #q(myconfig/myobj[uniqueName="objA"]/myattributes/attribB) + => \&getfoo, q(myconfig/myobj/myattributes/attribB) => \&getfoo, } ); $t->parsefile( $xmlFile ); $t->print; exit 0; sub getfoo { my ($t, $e) = @_; print "text=", $e->text(), "' tag='", $e->tag(), "' path='", $e->path(), "' parentTag='", $e->parent->tag(), "'\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML::Twig correct parent, xpath filter
by choroba (Cardinal) on Oct 30, 2015 at 15:09 UTC | |
by dcbecker (Novice) on Oct 30, 2015 at 15:34 UTC | |
by choroba (Cardinal) on Oct 30, 2015 at 15:56 UTC | |
|
Re: XML::Twig correct parent, xpath filter
by Preceptor (Deacon) on Oct 30, 2015 at 16:16 UTC | |
by dcbecker (Novice) on Oct 30, 2015 at 19:54 UTC |