Solved

Tags again

Posted on 2002-05-29
1
198 Views
Last Modified: 2010-03-05
I need a script that will close specific tags in an xml script.


I had a script which is below that kind of works but it skips every second tag. I'm not sure why.


#! /usr/local/bin/perl -w

while (<>) {
   if((/<([^>]*)>/) && (!$inTag)) {
       $inTag = 1;
       $startTag = $1;
   }
   elsif(/<([^>]*)>/) {
       print "</$startTag>\n";
       $inTag = 0;
   }
   print;
}





The xml file looks like this:

<hockey>

The Toronto Maple leafs scored 3-0 in their final game to win the series.

How will Toronto do in the next round?

Any comments are welcome.

<baseball>
</baseball>





Should look like this:

<hockey>

The Toronto Maple leafs scored 3-0 in their final game to win the series.

How will Toronto do in the next round?

Any comments are welcome.
</hockey>

<baseball>
</baseball>  
0
Comment
Question by:2xdafun
1 Comment
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 7043617
while( <> ){
    if( /<\/\Q$startTag\E>/i ){
        $startTag = '';
    }elsif( /<([^\/>][^>]*)>/ ){
        print "<\/$startTag>\n\n" if $startTag;
        $startTag = $1;
    }
    print;
}
print "<\/$startTag>\n\n" if $startTag;

0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
perl script help 12 106
PERL get the value for query 4 157
work on ods spreadsheet with perl in ubuntu 4 81
git fat pull happens nothing in centos 6.3 python 2.6.6 33 225
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question