Yet another solution:

use XML::Rules; my $parser = XML::Rules->new( rules => [ item => sub { '<li>' . $_[1]->{_content} . '</li>'}, list => sub { for ($_[1]->{_content}) { s/^\s+//; s/\s+$//; s{</li>\s+<li>}{</li><li>}g; } "\nTX <ol>" . $_[1]->{_content} . "</ol>\n" }, para => sub { 'TX ' . $_[1]->{_content}}, head => sub { 'TX <h3>' . $_[1]->{_content} . "</h3>"}, 'doc.body' => 'content', ] ); use Data::Dumper; my $result = $parser->parse($XML)->{'doc.body'}; for ($result) { s/^\s+//; s/\s+$//; } print $result; __END__ C:\temp>c:\temp\reformatXML.pl TX <h3>Introduction</h3> TX para 1 TX para 2 TX <h3>Individual</h3> TX para 1 TX para 2: TX <ol><li>item 1</li><li>2</li></ol>

Getting rid of the excess whitespace complicates the code somewhat.


In reply to Re: xml file reading and displaying by Jenda
in thread xml file reading and displaying by valavanp

Title:
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, details, 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, summary, 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.