【问题标题】:How to remove span inside specific tags using Html Agility Pack?如何使用 Html Agility Pack 删除特定标签内的跨度?
【发布时间】:2019-10-22 10:00:49
【问题描述】:

我有一个示例 html,我想在下面使用 html 敏捷包删除评论标签中的所有 span 标签

             <comment id="e096f3920ecbd8378f2b77b9608588434" type="start"></comment>
                <span style="color:hsl(0,0%,0%);">
                       <span style="color:hsl(0,0%,0%);">
                            Microsoft
                        </span>
                </span>
             <comment id="e096f3920ecbd8378f2b77b9608588434" type="end"></comment>

这个 html 是由 ckeditor 自动生成的,它的评论标签有类型(startend)。是否可以使用 html 敏捷包从开始到结束的评论标签中删除一些标签?

更新问题(2019 年 10 月 24 日)

完整的 HTML(由 ckeditor 提供)

<p>
   <span style="color:hsl(0,0%,0%);">
      <span style="color:hsl(0,0%,0%);">
         <span style="color:hsl(0,0%,0%);">
            <span style="color:hsl(0,0%,0%);">
               <span style="color:hsl(0,0%,0%);">
                  <span style="color:hsl(0,0%,0%);">
                     <span style="color:hsl(0,0%,0%);">
                        <comment id="ef7492a2e61d2666914b6a947aef5a6d6" type="start"></comment>
                        <span style="color:hsl(0,0%,0%);">This&nbsp;</span>
                     </span>
                  </span>
               </span>
            </span>
         </span>
      </span>
      <comment id="e042cfd52178aaa260f79e1accd66441c" type="start"></comment>
      Microsoft&nbsp;
      <comment id="e042cfd52178aaa260f79e1accd66441c" type="end"></comment>
      <span style="color:hsl(0,0%,0%);">
         <span style="color:hsl(0,0%,0%);">
            <span style="color:hsl(0,0%,0%);">
               <span style="color:hsl(0,0%,0%);">
                  <span style="color:hsl(0,0%,0%);">
                     <span style="color:hsl(0,0%,0%);">
                        <span style="color:hsl(0,0%,0%);">Enterprise</span>
                        <comment id="ef7492a2e61d2666914b6a947aef5a6d6" type="end"></comment>
                        <span style="color:hsl(0,0%,0%);"> Agreement is entered into between the entities identified on the signature form.</span>
                     </span>
                  </span>
               </span>
            </span>
         </span>
      </span>
   </span>
</p>

我想删除 comment start id (ef7492a2e61d2666914b6a947aef5a6d6) 和 comment end id (ef7492a2e61d2666914b6a947aef5a6d6)

内的所有跨度

【问题讨论】:

    标签: c# html html-parsing html-agility-pack ckeditor5


    【解决方案1】:

    您可以使用Xpath 查询来选择“start”cmets。之后,您需要遍历兄弟元素,直到到达“end”类型的注释。

    var nodesToRemove = new List<HtmlAgilityPack.HtmlNode>();
    var startNodes = doc.DocumentNode.SelectNodes("//comment[@type='start']");
    foreach (var startNode in startNodes)
    {
        var node = startNode.NextSibling;
    
        while (node != null)
        {
            var attribute = node.Attributes["type"];
    
            // found the "end" type of comment.
            if(attribute != null && attribute.Value == "end")
                break;
    
            if (node.Name == "span")
                nodesToRemove.Add(node);
    
            node = node.NextSibling;
        }
    }
    
    foreach (var node in nodesToRemove)
        node.Remove();
    

    【讨论】:

    • 我认为如果你在开始评论中有开始评论这将导致下一个兄弟为空,这将不起作用。
    • 函数是否可以作为参数获取ef7492a2e61d2666914b6a947aef5a6d6 id?
    • 它是ckeditor自动生成的,我想我们可以把它作为参数。
    • 这个 DOM 是一个层次模型。跨度节点不在评论模型内。您需要更好地定义要删除的节点类型。
    • 是的,这是一个挑战,DOM 是一个层次模型。评论标签内有一些结束跨度标签,而开放跨度标签在外面。我在想这个节点是否可以删除?
    猜你喜欢
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    相关资源
    最近更新 更多