Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

problem using XML Twig

by Anonymous Monk
on Sep 29, 2004 at 07:02 UTC ( [id://394874]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,
Using xml twig, I am trying to supply two elements in a line with the separator tab.
Ex: <c> <dl> ABC CDE add eerwe sdfsdfs erewrwe </dl> </c> Expected Result: <c> <dl> <first>ABC</first><second>CDE</second> <first>add</first><second>eerwe</second> <first>sdfsdfs</first><second>erewrwe</second> </dl> </c> Obtained Result: <c> <dl> <first>ABC</first><second>CDE</second> <first>add</first><second>eerwe</second> <first>sdfsdfs</first><second>erewrwe</second>rwe </dl> </c>

i.e. After </second> some characters are repeating, that character count is equivalent to line count. If i give five lines between
and
, then five characters are repeating.
Can any one tell where am i going wrong?
Code Used: my $t = new XML::Twig( twig_handlers => { "content"=>\&process_root }, pretty_print =>'nice', )->parsefile("$ARGV[0]"); my $say=$t->sprint; print FOUT "$say"; sub process_root { my ($a,$b,$c)=@_; my $bchild; for $bchild ($b->children) { if ($bchild->tag eq 'dl') { $bchild=&dl($a,$bchild); } } return $b; } sub dl { my ($a,$b,$c)=@_; my $bchild; if ($bchild->text) { $bchild->subs_text( qr{(.+?)\t(.+?)\n},'&elt(first=>{},$1)&elt(second=>{},$2)'); } return $b; }
Thanks in advance, -Anonymous user-

Replies are listed 'Best First'.
Re: problem using XML Twig
by Roger (Parson) on Sep 29, 2004 at 07:38 UTC
    You should look at how sub dl is invoked! Your sub dl is not implemented correctly. I have made a minor change to your code:

    .. sub dl { # my ($a, $b, $c) = @_; <-- what are you thinking? my ($a,$bchild)=@_; if ($bchild->text)

    Cheers.

      Hello Monks,
      Sorry i made small mistake, pls check my code now.
      Using xml twig, I am trying to supply two elements in a line with the separator tab.
      Ex:
      <c> <dl> ABC CDE add eerwe sdfsdfs erewrwe </dl> </c>
      Expected Result:
      <c> <dl> <first>ABC</first><second>CDE</second> <first>add</first><second>eerwe</second> <first>sdfsdfs</first><second>erewrwe</second> </dl> </c> Obtained Result: <c> <dl> <first>ABC</first><second>CDE</second> <first>add</first><second>eerwe</second> <first>sdfsdfs</first><second>erewrwe</second>rwe </dl> </c>

      i.e. After </second> some characters are repeating, that character count is equivalent to line count. If i give five lines between dl and /dl , then five characters are repeating. Can any one tell where am i going wrong? Code Used:
      my $t = new XML::Twig( twig_handlers => { "c"=>\&process_root }, pretty_print =>'nice', )->parsefile("$ARGV[0]"); my $say=$t->sprint; print FOUT "$say"; sub process_root { my ($a,$b,$c)=@_; my $bchild; for $bchild ($b->children) { if ($bchild->tag eq 'dl') { $bchild=&dl($a,$bchild); } } return $b; } sub dl { my ($a,$b,$c)=@_; my $bchild; for $bchild ($b->children) { if ($bchild->text) { $bchild->subs_text( qr{(.+?)\t(.+?)\n},'&elt(first=>{},$1)&elt(second=>{},$2)'); } return $b; } }
      Thanks in advance, -Anonymous user-
        Looks like you still haven't fixed your dl function yet???

        I amended the code as below and got the expected results:
        ... sub dl { my ($a,$bchild) = @_; if ($bchild->text) { $bchild->subs_text( qr{(.+?)\t(.+?)\n},'&elt(first=>{},$1)&elt(second=>{},$2)'); } return $b; }


Re: problem using XML Twig
by mirod (Canon) on Sep 29, 2004 at 13:24 UTC

    You could place your handler at dl level. Note that in this case the pretty_print do not work properly. The code looks a little simpler though:

    #!/usr/bin/perl -w use strict; use XML::Twig; XML::Twig->new( twig_roots => { dl => sub { $_->subs_text( qr/(.+?)\s+ +(.+?)\n/, '&elt(first + => $1)&elt( second => $2)' ) ->print; }, }, twig_print_outside_roots => 1, ) ->parse( \*DATA); __DATA__ <c> <dl> ABC CDE add eerwe sdfsdfs erewrwe </dl> </c>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-19 21:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found