繁体   English   中英

使用名称空间并基于父元素查找XPATH

[英]Find XPATH with namespace and based on parent element

我有以下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

这样使您的选择器更具体:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM