2012-02-19 68 views
0

我有以下XML:發現與命名空間XPath和基於父元素

... 
    <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

回答

1

讓您選擇更具體的是這樣的:

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

出於某種原因,使用'// T:start_time'工作,但使用'// T:章/ start_time'不起作用。我不知道爲什麼,但這可能與名稱空間有關。 – David542 2012-02-19 22:00:26

+1

嘗試向start_time添加名稱空間以及:// t:chapter/t:start_time – 2012-02-19 22:02:42

+0

哦,我明白了。謝謝。 – David542 2012-02-19 22:03:30