问题描述
我有一个很大的 xml 文档,如下所示:
I have a big xml document that looks like this:
<Node name="foo">
<Node name="16764764625">
<Val name="type"><s>3</s></Val>
<Val name="owner"><s>1</s></Val>
<Val name="location"><s>4</s></Val>
<Val name="brb"><n/></Val>
<Val name="number"><f>24856</f></Val>
<Val name="number2"><f>97000.0</f></Val>
</Node>
<Node name="1764466544">
<Val name="type"><s>1</s></Val>
<Val name="owner"><s>2</s></Val>
<Val name="location"><s>6</s></Val>
<Val name="brb"><n/></Val>
<Val name="number"><f>265456</f></Val>
<Val name="number2"><f>99000.0</f></Val>
</Node>
...
</Node>
我的任务是通过搜索以查找节点 Val name="number" 的子元素是否包含 265456 来获取父节点的值:1764466544(第二个节点中的名称值)
My mission is to get the value of the parent node: 1764466544 (value of name in 2nd Node) by doing a search to find if the subelement of the node Val name="number" contains 265456
我对 XPath 和 ElementTree 进行了大量阅读,但我仍然不确定从哪里开始实际查询.正在寻找示例...结果我找不到任何引用父节点的内容.
I've been doing a heap of reading on XPath, and ElementTree, but I am still not sure where to start actually query this. Looking for examples... I can't find any that reference a parent node as a result.
仍然是 python 的新手.. 任何建议将不胜感激.
Still new to python.. any suggestions would be appreciated.
谢谢
推荐答案
此 XPath:
/Node/Node[Val[@name='number']/f='265456']/@name
输出:
1764466544
这篇关于python xml查询获取父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!