Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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';

In reply to Increment the tag content using XML::Twig by Samy_rio

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 all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (6)
    As of 2017-12-16 04:39 GMT
    Find Nodes?
      Voting Booth?
      What programming language do you hate the most?

      Results (448 votes). Check out past polls.