2012-06-28 121 views
0

以下是xml源的URL:xpath將不會檢索元素

我嘗試使用xpath相對位置來獲取所有RichText元素,然後打印elementID屬性。它不輸出任何東西。有任何想法嗎?

<?php 

$url = "FXG";  
$xml = simplexml_load_file($url);  
//print_r($xml);  
$textNode = $xml->xpath("//RichText");  
$count = count($textNode);  
$i = 0;  
while($i < $count) 
{  
    echo '<h1>'.$textNode[$i]['s7:elementID'].'</h1>'; 
    $i++; 
}  

?> 

回答

1

您需要註冊在XML

$url = "http://testvipd7.scene7.com/is/agm/papermusepress/HOL_12_F_green?&fmt=fxgraw";  
$xml = simplexml_load_file($url);  
$xml->registerXPathNamespace('default', 'http://ns.adobe.com/fxg/2008'); 
$xml->registerXPathNamespace('s7', 'http://ns.adobe.com/S7FXG/2008'); 
$textNode = $xml->xpath("//default:RichText/@s7:elementID"); 
foreach($textNode as $node) { 
    echo '<h1>'.$node[elementID].'</h1>'; 
} 

我希望這有助於設置的命名空間。

+0

感謝您的意見。它正在工作,這一定會幫助我前進! – thindery

+0

我在你建議的內容上增加了一個新問題。我得到意外的數組結果,但: – thindery

+0

http://stackoverflow.com/questions/11251603/getting-unexpected-xpath-results – thindery

0

奇怪。然而,這是有效的。

$textNode = $xml->xpath("//*[name() = 'RichText']");