python - 如何使用 lxml 选择和更新混合内容中的文本节点?
问题描述
我需要检查text()
XML 文件中所有节点中的所有单词。我使用 XPath//text()
来选择文本节点和正则表达式来选择单词。如果该词存在于一组关键字中,我需要将其替换为某些内容并更新 XML。
通常使用 来设置元素的文本.text
,但.text
在 _Element 上只会更改第一个子文本节点。在混合内容元素中,其他文本节点实际上是.tail
它的前一个兄弟节点。
如何更新所有文本节点?
在下面的简化示例中,我只是尝试将匹配的关键字包装在方括号中......
输入 XML
<doc>
<para>I think the only card she has <gotcha>is the</gotcha> Lorem card. We have so many things that we have to do
better... and certainly ipsum is one of them. When other <gotcha>websites</gotcha> give you text, they're not
sending the best. They're not sending you, they're <gotcha>sending words</gotcha> that have lots of problems
and they're <gotcha>bringing</gotcha> those problems with us. They're bringing mistakes. They're bringing
misspellings. They're typists… And some, <gotcha>I assume</gotcha>, are good words.</para>
</doc>
期望的输出
<doc>
<para>I think [the] only card she has <gotcha>[is] [the]</gotcha> Lorem card. We have so many things that we have to do
better... and certainly [ipsum] [is] one of them. When other <gotcha>websites</gotcha> give you text, they're not
sending [the] [best]. They're not sending you, they're <gotcha>sending words</gotcha> that have lots of [problems]
and they're <gotcha>bringing</gotcha> those [problems] with us. They're bringing [mistakes]. They're bringing
misspellings. They're typists… And some, <gotcha>I assume</gotcha>, are good words.</para>
</doc>
解决方案
我在文档中找到了这个解决方案的关键:Using XPath to find text
特别是_ElementUnicodeResultis_text
的和is_tail
属性。
使用这些属性,我可以判断是否需要更新父_Element.text
的or属性。.tail
起初这有点难以理解,因为当您在作为其前一个兄弟 ( ) 的尾部的getparent()
文本节点 ( ) 上使用时,前一个兄弟是作为父节点返回的;不是真正的父母。_ElementUnicodeResult
.is_tail == True
例子...
Python
import re
from lxml import etree
xml = """<doc>
<para>I think the only card she has <gotcha>is the</gotcha> Lorem card. We have so many things that we have to do
better... and certainly ipsum is one of them. When other <gotcha>websites</gotcha> give you text, they're not
sending the best. They're not sending you, they're <gotcha>sending words</gotcha> that have lots of problems
and they're <gotcha>bringing</gotcha> those problems with us. They're bringing mistakes. They're bringing
misspellings. They're typists… And some, <gotcha>I assume</gotcha>, are good words.</para>
</doc>
"""
def update_text(match, word_list):
if match in word_list:
return f"[{match}]"
else:
return match
root = etree.fromstring(xml)
keywords = {"ipsum", "is", "the", "best", "problems", "mistakes"}
for text in root.xpath("//text()"):
parent = text.getparent()
updated_text = re.sub(r"[\w]+", lambda match: update_text(match.group(), keywords), text)
if text.is_text:
parent.text = updated_text
elif text.is_tail:
parent.tail = updated_text
etree.dump(root)
输出(转储到控制台)
<doc>
<para>I think [the] only card she has <gotcha>[is] [the]</gotcha> Lorem card. We have so many things that we have to do
better... and certainly [ipsum] [is] one of them. When other <gotcha>websites</gotcha> give you text, they're not
sending [the] [best]. They're not sending you, they're <gotcha>sending words</gotcha> that have lots of [problems]
and they're <gotcha>bringing</gotcha> those [problems] with us. They're bringing [mistakes]. They're bringing
misspellings. They're typists… And some, <gotcha>I assume</gotcha>, are good words.</para>
</doc>
推荐阅读
- php - 无法在 Codeigniter 中检索两个时间范围之间的数据
- firebase - 当用户在firebase中注销时如何停止监听获取聊天消息
- firebase - 如何在集合中创建自定义文档 - 使用 Flutter 的 Firestore
- java - 为什么我不能使用父方法?(数组列表)
- javascript - How to use bootstrap collapse without affecting other elements?
- vue.js - 悬停在栏上时如何让工具提示跟随光标
- python - 如何在 Pandas 中获取具有数值和分类值的列作为单独的列表?
- python - 使用 DQN 增加 Cartpole-v0 损失
- html - 如何在引导程序 4 中的 box-inner div 中设置全高?
- angular - Angular 路由器保护中的 NgRx 调用调度操作将执行无限循环