2013-05-01 71 views
0

我想知道如果我能得到一個相鄰節點的值simplexml的獲取節點的值,接下來將匹配節點

<item> 
    <title>Cell Phone Plans That Make You Go Hmmm</title> 
    <link>http://www.articlegeek.com/computers/telecommunication_articles/10574- cellphoneplanst.htm</link> 
    <description>Cell phone plans across the globe vary a great deal. Some say that cell phone plans in the US are more costly. However, there are some advantages to the cell phone plans in the US which may balance the difference.</description> 
</item> 

這是我的查詢來獲取描述的內容,但我想要的鏈接,以及匹配說明。

$userIN = "cell"; 
    $nod2 = $xml->xpath 
    ("//description 
       [text( 
       [contains 
       (translate(.,'ABCDEFGHJIKLMNOPQRSTUVWXYZ', 'abcdefghjiklmnopqrstuvwxyz'), 
        '".$userIN."')]]"); 
+0

當你開始成爲使用XPath親已經,你可能想了解下一個座標軸:[XPath軸 - 的XPath(Schlitt&Westhoff的)](http://schlitt.info/opensource/blog/ 0704_xpath.html#xpath-axes) – hakre 2013-05-01 23:41:59

+0

請參閱http://zvon.org/xxl/XPathTutorial/Output/example15.html – michi 2013-05-02 11:37:40

+0

好奇:這個xpath表達式在做什麼?尋找包含'cell'的''節點並將全部大寫替換爲小寫?爲什麼這樣做? – michi 2013-05-02 11:41:47

回答

0

我借其他主題的這一回答:

SimpleXML get next/prev node

希望這有助於。

$xmlData = new SimpleXMLElement(file_get_contents("data.xml")); 
$index = 0; 
foreach($xmlData->row as $item) { 
    if ($item->url == $_GET['id']) { 
     // show photo 
     $title = $item->title; 

     $prev = $xmlData->row[$index-1]; 
     $next = $xmlData->row[$index+1]; 
    } 
    $index++; 
}