Python 如何包装标签的所有内容?

Python 如何包装标签的所有内容?,python,xml,xslt,Python,Xml,Xslt,我有以下XML部分: <table> <tr> <td>Hello</td> <td>Hello</td> <td> <p>Hello already in P</p> </td> <td> This one has some naked text <span>and som

我有以下XML部分:

<table>
  <tr>
    <td>Hello</td>
    <td>Hello</td>
    <td>
      <p>Hello already in P</p>
    </td>
    <td>
      This one has some naked text
      <span>and some span wrapped text</span>
    </td>
  </tr>
</table>
我想在一个p标签中包装每个单元格的内容,这些内容还没有包装在一个p标签中。因此,输出为:

<table>
  <tr>
    <td><p>Hello</p></td>
    <td><p>Hello</p></td>
    <td>
      <p>Hello already in p tag</p>
    </td>
    <td>
      <p>
        This one has some text
        <span>and some span wrapped text</span>
      </p>
    </td>
  </tr>
</table>
我在项目中使用lxml etree,但该库似乎没有wrap方法或类似方法

现在我想这可能是XSLT转换的工作,但我希望避免在Python项目中添加另一层复杂性和其他依赖项


td的内容可以是任何深度的

我自己不使用lxml包,但请尝试以下方法:

def wrap(root):
    # find <td> elements that do not have a <p> element
    cells = etree.XPath("//td[not(p)]")(root)
    for cell in cells:
        # Create new <p> element
        e = Element("p")
        # Set the <p> element text from the parent
        e.text = cell.text
        # Clear the parent text because it is now in the <p> element
        cell.text = None
        # Move the parents children and make them the <p> element's children
        # (because the span on line 10 of the input file should be nested)
        for child in cell.getchildren():
           # This actually moves the child from the <td> element to the <p> element
           e.append(child)
        # Set the new <p> element as the cell's child
        cell.append(e)

嗨,谢谢你的回答。这个解决方案的问题是,它没有考虑到td的内容可以是任何深度的。你能给我一个新的示例输入文件和输出文件,上面的代码失败了,我会帮你修复它吗?事实上,我错了,你的答案对我有效。谢谢