根据父元素查找具有命名空间的 XPATH
我有以下 xml:
...
<chapter>
<start_time>00:01:04</start_time>
</chapter>
<chapter>
<start_time>00:01:05</start_time>
</chapter>
...
目前,要获取我正在执行的 start_times:
start_times = node.xpath("//t:start_time/text()",
namespaces={'t':'http://example.com/namespace'})
但是,我还从非章节元素获取 start_times
:
[00:00:01, 00:00:02, 00:01:04, 00:01:05]
此外,由于某种原因起诉 //t: Chapter/start_time
返回空结果。
如何仅获取那些具有 chapter
父元素的 start_times
?
I have the following xml:
...
<chapter>
<start_time>00:01:04</start_time>
</chapter>
<chapter>
<start_time>00:01:05</start_time>
</chapter>
...
Currently, to get the start_times I am doing:
start_times = node.xpath("//t:start_time/text()",
namespaces={'t':'http://example.com/namespace'})
However, I am also getting start_times
from non-chapter elements:
[00:00:01, 00:00:02, 00:01:04, 00:01:05]
In addition, for some reason suing //t:chapter/start_time
returns an empty result.
How would I get only those start_times
that have a parent element of chapter
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让您的选择器更加具体,如下所示:
Make your selector more specific like this: