根据父元素查找具有命名空间的 XPATH

发布于 2025-01-06 18:02:47 字数 673 浏览 3 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时浪漫 2025-01-13 18:02:47

让您的选择器更加具体,如下所示:

start_times = node.xpath("//t:chapter/start_time/text()", 
                   namespaces={'t':'http://example.com/namespace'})

Make your selector more specific like this:

start_times = node.xpath("//t:chapter/start_time/text()", 
                   namespaces={'t':'http://example.com/namespace'})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文