使用 lxml 插入包装子元素

Inserting wrapping child element with lxml

我正在尝试在一个元素中插入一个子元素,该元素与它们的前父元素具有相同的文本,因此是一个包装子元素。基本示例,即我的输入:

<p>
    <s>text</s>
    <s>text</s>
    <s>text</s>
</p>

我想要这样的东西(anno 的内容将来自 NLP 工具,不是很重要):

<p>
    <s>
        <orig>text</orig>
        <anno>token1</anno>
    </s>
    <s>
        <orig>text</orig>
        <anno>token2</anno>
    </s>
    <s>
        <orig>text</orig>
        <anno>token3</anno>
    </s>
</p>

anno 元素会更简单,我没有得到的是在其中创建一个子元素,其中包含曾经有的文本。

使用不同的元素函数(addprevious、addnext、append、insert)我可以做如下事情:

    <s>text<orig/></s>
    <s>text<orig>text</orig></s>
    <orig><s>text</s></orig>

但是none这就是我想要的。我必须使用替换吗?在这种情况下,如何?谢谢!

试试这个:

import sys

from lxml import etree

tree = etree.parse(open("/tmp/so.xml"))

# Make sure you have enough annos to match the existing <s> elements.
annos = ["token1", "token2", "token3"]

for i, s in enumerate(tree.xpath("//s")):
    # remember the text of the <s>
    text = s.text

    # remove the <s> from its parent
    parent = s.getparent()
    parent.remove(s)

    # create a new <s>
    new_s = etree.SubElement(parent, "s")

    # create the <orig>, set the remembered text on it
    orig = etree.SubElement(new_s, "orig")
    orig.text = text

    # create the <anon>, set the token on it
    annon = etree.SubElement(new_s, "anon")
    annon.text = annos[i]

with open("/tmp/out.xml", "wb") as f:
    tree.write(f, pretty_print=True)

输出:

<p>
  <s>
    <orig>text</orig>
    <anon>token1</anon>
  </s>
  <s>
    <orig>text</orig>
    <anon>token2</anon>
  </s>
  <s>
    <orig>text</orig>
    <anon>token3</anon>
  </s>
</p>