【发布时间】:2023-03-24 11:08:02
【问题描述】:
我在我的项目中使用lxml。我有一个输入 xml,需要解析它。问题是根元素有xmlns="some_url",例如
<root xmlns="some_url">
<child>some_text</child>
<child>some_text</child>
<child>
<sub_child>text</sub_child>
</child>
</root>
我的xml要复杂得多,所以当我需要找到一些元素时,我使用:
root.findall("nsp:child/nsp:sub_child", namespaces={"nsp":"some_url"})
所以我的代码充满了 find 方法和相同的 namespaces={"nsp":"some_url"}
我怎样才能将这个命名空间作为默认值传递一次?我想我应该从_SimpleElementPath 类继承。但是 xml 元素如何知道使用这个继承的类呢?
【问题讨论】: