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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Two approaches using XML::Twig, too bad the xpath support is dated
#!/usr/bin/perl -- use XML::Twig; BEGIN { *XML::Twig::Elt::x = *XML::Twig::Elt::get_xpath; } use strict; use warnings; my $str = <<'EOF'; <fudge> <factory> <drop> <bindname>qqqqq</bindname> <bindpass>welcome</bindpass> </drop> <drop> <bindname>abcde</bindname> <bindpass>welcome</bindpass> </drop> </factory> </fudge> EOF { my $t = XML::Twig->new( pretty_print => 'indented', twig_handlers => { '//factory/drop' => sub { if ( not $_->x('bindname[string()=~/abcde/]') ) { if ( my ($pass) = $_->x('bindpass[string()="welcom +e"]') ) { $pass->set_text( uc $pass->text ); } } }, }, ); $t->parse($str); $t->print(); } { my $bozo = 0; my $t = XML::Twig->new( pretty_print => 'indented', twig_handlers => { '//factory/drop/bindname' => sub { $bozo = 1 if $_->text =~ /abcde/; }, '//factory/drop/bindpass' => sub { if ( not $bozo and $_->text =~ /welcome/ ) { $_->set_text( uc $_->text ); } $bozo = 0; }, }, ); $t->parse($str); $t->print(); } __END__
And a similar one using XML::XSH2, too bad it doesn't have diagnostics/splain
#!/usr/bin/perl -- use strict; use warnings; use XML::XSH2; xsh(<<'__XSH__'); open "fudge.xml"; ls --indent /; pwd ; cd /fudge/factory ; pwd ; foreach //drop { if not( bindname/text()[contains(.,'abcde')] ){ set bindpass xsh:subst( bindpass/text(), "welcome", "W-e-l-c-o-m-e", "i" ); }; }; ls --indent /; save --indent :b /; # overwrite original file, :b means backup __XSH__ __END__ $ perl parsing fudge.xml done. <?xml version="1.0"?> <fudge> <factory> <drop> <bindname>qqqqq</bindname> <bindpass>welcome</bindpass> </drop> <drop> <bindname>abcde</bindname> <bindpass>welcome</bindpass> </drop> </factory> </fudge> Found 1 node(s). / /fudge/factory <?xml version="1.0"?> <fudge> <factory> <drop> <bindname>qqqqq</bindname> <bindpass>W-e-l-c-o-m-e</bindpass> </drop> <drop> <bindname>abcde</bindname> <bindpass>welcome</bindpass> </drop> </factory> </fudge> Found 1 node(s). Document saved into file 'fudge.xml'. $ ls -loanh fudge* -rw-rw-rw- 1 0 252 2011-08-26 06:05 fudge.xml -rw-rw-rw- 1 0 234 2011-08-26 06:05 fudge.xml~

In reply to Re: Search and replace query by Anonymous Monk
in thread Search and replace query by heeru

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (4)
    As of 2020-01-27 08:51 GMT
    Find Nodes?
      Voting Booth?