Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Increment the tag content using XML::Twig

by Samy_rio (Vicar)
on Apr 04, 2007 at 06:25 UTC ( #608211=perlquestion: print w/replies, xml ) Need Help??
Samy_rio has asked for the wisdom of the Perl Monks concerning the following question:

G'day Monks, I am struggling in XML::Twig for increment the tag content.

use strict; use warnings; use XML::Twig; use XML::XPath; use XML::Twig::XPath; my $twigrp = new XML::Twig::XPath( pretty_print => 'indented_c', + # print format + keep_encoding => 1, ); $twigrp->parse(\*DATA); my %insbeforetag; $insbeforetag{1} = [('//chbody//exercises/orderedlist/item/para', 'ins +t', '\t{{Num}\.\t')]; for my $row (keys %insbeforetag){ for ($twigrp->findnodes($insbeforetag{$row}->[0])) { my $min_elt= XML::Twig::Elt->new( $insbeforetag{$row}->[1] => +$insbeforetag{$row}->[2]); $min_elt->paste( before => $_); } } $twigrp->print; __DATA__ <chapter> <chbody> <exercises att="problems"> <title>Content</title> <orderedlist numeration="number"> <item><para>Text1</para></item> <item><para>Text2</para></item> <item><para>Text3</para></item> </orderedlist> <orderedlist numeration="number"> <item><para>Text1</para></item> <item><para>Text2</para></item> <item><para>Text3</para></item> <item><para>Text4</para></item> </orderedlist> </exercises> </chbody> </chapter>

Output as:

<chapter> <chbody> <exercises att="problems"> <title>Content</title> <orderedlist numeration="number"> <item> <inst>\t{{Num}\.\t</inst> <para>Text1</para></item> <item> <inst>\t{{Num}\.\t</inst> <para>Text2</para></item> <item> <inst>\t{{Num}\.\t</inst> <para>Text3</para></item></orderedlist> <orderedlist numeration="number"> <item> <inst>\t{{Num}\.\t</inst> <para>Text1</para></item> <item> <inst>\t{{Num}\.\t</inst> <para>Text2</para></item> <item> <inst>\t{{Num}\.\t</inst> <para>Text3</para></item> <item> <inst>\t{{Num}\.\t</inst> <para>Text4</para></item></orderedlist></exercises></chbody> +</chapter>

But expected Output as:

<chapter> <chbody> <exercises att="problems"> <title>Content</title> <orderedlist numeration="number"> <inst> 1. </inst><item><para>Text1</para></item> <inst> 2. </inst><item><para>Text2</para></item> <inst> 3. </inst><item><para>Text3</para></item> </orderedlist> <orderedlist numeration="number"> <inst> 1. </inst><item><para>Text1</para></item> <inst> 2. </inst><item><para>Text2</para></item> <inst> 3. </inst><item><para>Text3</para></item> <inst> 4. </inst><item><para>Text4</para></item> </orderedlist> </exercises> </chbody> </chapter>

Tag content as "\t{{Num}}\.\t" will be " 1. " with orderedlist sequence. Each orderedlist {{Num}} will be reset. How to achive this?

Thanks in advance.

Velusamy R.

eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Replies are listed 'Best First'.
Re: Increment the tag content using XML::Twig
by Jenda (Abbot) on Apr 04, 2007 at 09:35 UTC

    Sorry, not a XML::Twig solution:

    use strict; use warnings; use XML::Rules; my $parser = XML::Rules->new( rules => [ _default => 'raw', item => 'as array', orderedlist => sub { delete $_[1]->{_content}; # remove the whitespace for (my $inst = 1; $inst <= @{$_[1]->{item}}; $inst++) { push @{$_[1]->{_content}}, "\n", [inst => "\t$inst.\t" +], [item => $_[1]->{item}[$inst-1]]; } push @{$_[1]->{_content}}, "\n"; delete $_[1]->{item}; return $_[0] => $_[1]; } ], style => 'filter', ); $parser->filter(\*DATA); __DATA__ <chapter> <chbody> <exercises att="problems"> <title>Content</title> <orderedlist numeration="number"> <item><para>Text1</para></item> <item><para>Text2</para></item> <item><para>Text3</para></item> </orderedlist> <orderedlist numeration="number"> <item><para>Text1</para></item> <item><para>Text2</para></item> <item><para>Text3</para></item> <item><para>Text4</para></item> </orderedlist> </exercises> </chbody> </chapter>

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://608211]
Approved by prasadbabu
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2017-09-25 08:50 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (278 votes). Check out past polls.