Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want close the tags in hierarchical way.

This is my input.
HTML
<P_Normal><list-bull></P_Normal>
<P_list-bull(1)>Use Microsoft Windows accessibility  </P_list-bull(1)>
<P_list-simple(2)>   Magnify the display </P_list-simple(2)>
<P_list-simple(2)>   Change the size of text and icons </P_list-simple(2)>
<P_list-simple(2)>   Convert the text displayed  </P_list-simple(2)>
<P_list-simple(2)>   Change the contrast </P_list-simple(2)>
<P_list-simple(2)>   Display an onscreen keyboard </P_list-simple(2)>
<P_list-simple(2)>   Convert the mouse for left-handed use. </P_list-simple(2)>
<P_list-bull(1)>Deliver learning through  </P_list-bull(1)>
<P_list-bull(1)>Increase the character size on handouts for visually impaired learners. </P_list-bull(1)>
<P_list-bull(1)>Adjust background colours and fonts to assist dyslexic learners. </P_list-bull(1)>
<P_list-bull(1)><pg>13</pg>Provide feedback on written work using the comments facility and track changes rather than handwritten notes, so that the learners can change character size or use a text to speech application to hear feedback. </P_list-bull(1)>
</list-bull>

I want to close the list tags in hierarchical manner(below way)
HTML
<list list-type="bullet">
<list-item><p>Use Microsoft Windows accessibility options to:
<list list-type="simple">
<list-item><p> Magnify the display</p></list-item>
<list-item><p> Change the size of text and icons</p></list-item>
<list-item><p> Convert the text displayed into speech (i.e. narrator)</p></list-item>
<list-item><p> Change the contrast</p></list-item>
<list-item><p> Display an onscreen keyboard</p></list-item>
<list-item><p> Convert the mouse for left-handed use.</p></list-item>
</list></p></list-item>
<list-item><p>Deliver learning through </p></list-item>
<list-item><p>Increase the character </p></list-item>
<list-item><p> fonts to assist dyslexic learners.</p></list-item>
<list-item><p> Adjust background colours</p></list-item>
</list></p>


Please help me. Thank you.
Posted
Updated 6-Jan-12 20:16pm
v3

Classical application of XSLT: XML to XML.
 
Share this answer
 
v2
Since my Solution 1 is not really applicable (the tags are not XML conformant), enclosed please find an alternative version in Perl.

Cheers

Andi

PERL
#!perl

use strict;

# file:     { line NEWLINE } .
# line:     listitem | ANY_CHAR .
# listitem: begin itemtext end .
# begin:    '<' tag '>' .
# end:      '<' '/' tag '>' .
# tag:      'P_list-' kind '(' level ')' .
# itemtext: { ANY_CHAR_EXCEPT_NEWLINE } .

my $data = readfile(shift);

my $currentlevel = 0;
while($data =~ s%^[\s\S]*?<(P_list-(\w+)\((\d+)\))>(.*?)</\1>\s*%%g)
{
    my ($tag, $kind, $level, $text) = ($1, $2, $3, $4);
    if ($currentlevel < $level) {
        # open lists up to the given level
        while($currentlevel < $level) {
            printBeginList($kind);
            printBeginItem();
            $currentlevel++;
        }
    } elsif ($currentlevel > $level) {
        # close lists down to the given level
        while($currentlevel > $level) {
            $currentlevel--;
            printEndItem();
            printEndList();
        }
        printEndItem();
        printBeginItem();
    } else {
        # close former item
        printEndItem();
        printBeginItem();
    }
    printItem($text);
}
# close lists down to the top level
while($currentlevel > 0) {
    $currentlevel--;
    printEndItem();
    printEndList();
}
exit 0;

sub readfile {
    my ($file) = @_;

    my $sep = $/;
    $/ = undef;
    open(F, $file) or die("Failed to read '$file' ($!)");
    my $data = <F>;
    close(F);
    $/ = $sep;

    return $data;
}

sub getType {
    my ($kind) = @_;
    return $kind eq "bull" ? "bullet" : $kind;
}

sub printBeginList {
    my ($kind) = @_;
    print "\n<list list-type=\"", getType($kind), "\">\n";
}

sub printEndList {
    print "</list>\n";
}

sub printBeginItem {
    print "<list-item><p>";
}

sub printEndItem {
    print "</p></list-item>\n";
}

sub printItem {
    my ($text) = @_;
    print $text;
}
 
Share this answer
 
v2
Comments
ckulasekaran 6-Jan-12 21:58pm    
Its helpful for me, thank U.....
Andreas Gieriet 6-Jan-12 22:00pm    
You are welcome!
Any rating? Does it solve your problem?
Thanks
Andi
ckulasekaran 7-Jan-12 2:12am    
ya its working partially,but ur answer is correct for my question,now my problem is,there is a opening and closing <list-bull>tag around the list item,i want to take the content based on <list-bull|num|alpha>,plz...give some suggestion again....thank u
Andreas Gieriet 11-Jan-12 3:46am    
Can you please specify more precisely.
Hint: if you want to get help, make it easy to help you... ;-)
ckulasekaran 7-Jan-12 2:19am    
plz help how to write the list item into output file

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900