我有一个类似这样的现有XML:
<para>
<ul type="unordered">
<li>
<content>
<para>First thing on the list</para>
</content>
</li>
<li>
<content>
<para>Some introductory text for the inner list:</para>
<ul type="unordered">
<li>
<content>
<para>this is a sublist1</para>
</content>
</li>
<li>
<content>
<para>this is a sublist2</para>
</content>
</li>
</ul>
</content>
</li>
</ul>
</para>
但是我想要的是将内部列表元素包装在<para>
中。
<para>
<ul type="unordered">
<li>
<content>
<para>First thing on the list</para>
</content>
</li>
<li>
<content>
<para>
Some introductory text for the inner list:
<ul type="unordered">
<li>
<content>
<para>this is a sublist1</para>
</content>
</li>
<li>
<content>
<para>this is a sublist2</para>
</content>
</li>
</ul>
</para>
</content>
</li>
</ul>
</para>
我有代码检查列表中是否有列表并添加一个元素。
由于将<para>
添加到每个子列表中,因此我不确定自己在做什么错。然后将它们添加到父级(if eachl.append(para) to element.append(para)
),将整个子列表移到父级:
if element.tag == "ul":
element.tag = "list"
element.set("type", "unordered")
list_nlist = element.findall("li/ul")
if len(list_nlist) != 0:
for eachl in list_nlist:
para = copy.deepcopy(eachl)
para.tag = "para"
eachl.clear()
eachl.insert(0, para)
eachl.append(para)