简体   繁体   English

如何修复丢失的HTML <open> & <close> HTML字符串中间的标签

[英]How to fix missing HTML<open> & <close> tags in middle of HTML string

I have to validate HTML string in my project, 我必须在项目中验证HTML字符串,

I have an html with : 我有一个HTML:

<ul>
    <li>Test 1</li>
    <ol>
        <li>Test 2</li>        
    </ol>
    <li>Test 1</li>
</ul>

Required Output is : 所需的输出是:

<ul>
    <li>Test 1</li>
</ul>
<ol>
    <li>Test 2</li>
</ol>
<ul>
    <li>Test 1</li>
</ul>

In HTML code </ul> and <ul> tags are missing. 在HTML代码中,缺少</ul><ul>标记。 Now in this case how to Search where are the missing tag and how to insert appropriate ending tag and beginning tags in proper places. 现在,在这种情况下,如何搜索丢失的标记在哪里以及如何在适当的位置插入适当的结束标记和开始标记。

I tried HTML agility pack and Tidy library, but I couldn't find better solution for this issue. 我尝试了HTML敏捷包和Tidy库,但是找不到这个问题的更好解决方案。

Using any DOM manipulation library, you will need to create a ruleset describing which elements can be children of a <ul> element. 使用任何DOM操作库,您将需要创建一个规则集,描述哪些元素可以是<ul>元素的子元素。 Iterate over all of its children, and when you find a child ( <ol> ) that's not allowed, move all subsequent children, including the <ol> child, to the parent of the <ul> element, placing it at the index of the <ul> element + 1. 遍历其所有子项,当您找到不允许的子项( <ol> )时,请将所有后续子项(包括<ol>子项)移动到<ul>元素的父项,并将其放置在<ul>元素+1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM