2014-12-02 52 views
0

我使用PHP XPath和試圖讓與我的XML與SCP開頭的所有節點都是一樣的節點:custom_labelPHP的XPath找到

我的XML是:

<entry> 
    <scp:availability>in stock</scp:availability> 
    <scp:price unit='USD'>8.99</scp:price> 
    <scp:custom_label_0>seasonal</scp:custom_label_0> 
    <scp:custom_label_1>clearance</scp:custom_label_1> 
    <scp:custom_label_2>holiday</scp:custom_label_2> 
    <scp:custom_label_3>sale</scp:custom_label_3> 
    <scp:custom_label_4>best seller</scp:custom_label_4> 
</entry> 

我試着使用:

$numEntries = $entry->getXpath()->query("//*[contains(.,'scp:custom_label')]")->length; 

我沒有得到任何結果回來

我想看到$ numEntries等於5。

我很感激幫助。非常感謝您

+1

你的XML規範是壞的; ''應該是裏面有多個元素的標籤。 – 2014-12-03 14:40:42

回答

2

使用local-name()

$res = $xpath->query("//*[contains(local-name(),'custom_label')]"); 
+0

謝謝你的時間和你的回答。它讓我走上了正確的道路 – user583576 2014-12-03 14:29:36

0

這爲我工作

$xpath->query("//*[contains(local-name(),'custom_label')]"); 

謝謝tlenss

+0

如果你接受了另一個答案,那會更好,因爲大部分答案都直接來自它。 – 2014-12-03 14:41:54