【问题标题】:Removing unclosed opening <p>tags from xhtml document从 xhtml 文档中删除未关闭的打开 <p> 标签
【发布时间】:2011-04-09 04:11:03
【问题描述】:

我有一个带有很多标签的大 xhtml 文档。我观察到一些未闭合的开头段落标签不必要地重复,我想删除它们或用空格替换它们。 我只想编写代码来识别未闭合的段落标签并删除它们。

这里有一个小例子来说明我的意思:

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

谁能给我控制台应用程序的代码,只是为了删除这些未闭合的段落标签。

【问题讨论】:

    标签: c# html xml regex xhtml


    【解决方案1】:

    这应该可行:

    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();
        }
    }
    

    我已经用你的示例对其进行了测试,它可以工作......虽然它对于算法来说是一个糟糕的公式,但它应该给你一个开始的基础!

    【讨论】:

      【解决方案2】:

      你必须弄清楚,创建了什么样的 DOM 树。可以理解为

      <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>
        </p>
      </p>
      

      <p><strong>Company Registration No.1</strong> </p>
      <p><strong>Company Registration No.2</strong></p>
      
      <p></p>      <!-- extra tag -->
      <p></p>      <!-- extra tag -->
      <hr/>     
      <p><strong> HALL WOOD (LEEDS) LIMITED</strong><br/></p>
      <p><strong>REPORT AND FINANCIAL STATEMENTS </strong></p>
      

      您可以尝试查找嵌套的 p-tag 并将内部内容移动到外部 p-tag 并删除保留为空的内部 p-tag。无论如何,我认为您需要先分析 DOM-tree。

      【讨论】:

      • 一个&lt;p&gt;(官方)不允许包含另一个&lt;p&gt;,所以第一个解释不太可能。当看到第二个单独的&lt;p&gt; 时,它意味着关闭第一个。 &lt;hr/&gt; 有点棘手。我敢打赌该部分的 DOM 看起来像 .../strong&gt;&lt;/p&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;&lt;hr/&gt;&lt;/p&gt; &lt;p&gt;&lt;strong...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-17
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多