Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing unclosed opening <p>tags from xhtml document

I have a big xhtml document with lots of tags. I have observed that a few unclosed opening paragraph tags are repeating unnecessarily and I want to remove them or replace them with blank space. i just want to code to identify unclosed paragraph tags and delete them.

Here's a small sample to show what I mean:

<p><strong>Company Registration No.1</strong> </p>
<p><strong>Company Registration No.2</strong></p>

<p>      <!-- extra tag -->
<p>      <!-- extra tag -->

<hr/>     

<p><strong> HALL WOOD (LEEDS) LIMITED</strong><br/></p>
<p><strong>REPORT AND FINANCIAL STATEMENTS </strong></p>

Can some one please give me code for console application, just to remove these unclosed paragraph tags.

like image 907
Sangram Nandkhile Avatar asked Sep 07 '10 11:09

Sangram Nandkhile


1 Answers

this should work:

public static class XHTMLCleanerUpperThingy
{
    private const string p = "<p>";
    private const string closingp = "</p>";

    public static string CleanUpXHTML(string xhtml)
    {
        StringBuilder builder = new StringBuilder(xhtml);
        for (int idx = 0; idx < xhtml.Length; idx++)
        {
            int current;
            if ((current = xhtml.IndexOf(p, idx)) != -1)
            {
                int idxofnext = xhtml.IndexOf(p, current + p.Length);
                int idxofclose = xhtml.IndexOf(closingp, current);

                // if there is a next <p> tag
                if (idxofnext > 0)
                {
                    // if the next closing tag is farther than the next <p> tag
                    if (idxofnext < idxofclose)
                    {
                        for (int j = 0; j < p.Length; j++)
                        {
                            builder[current + j] = ' ';
                        }
                    }
                }
                // if there is not a final closing tag
                else if (idxofclose < 0)
                {
                    for (int j = 0; j < p.Length; j++)
                    {
                        builder[current + j] = ' ';
                    }
                }
            }
        }

        return builder.ToString();
    }
}

I have tested it with your sample example and it works...although it is a bad formula for an algorithm, it should give you a starting basis!

like image 83
Richard J. Ross III Avatar answered Sep 25 '22 04:09

Richard J. Ross III