2010-08-10 37 views
6

實施條件我有一個XML文件XPath中

<?xml version="1.0" encoding="UTF-8"?> 
<xml> 
    <events date="12/12/2010"> 
    <event> 
     <title>JqueryEvent</title> 
     <description> 
     easily 
     </description> 
    </event> 
    </events> 
    <events date="14/12/2011"> 
    <event> 
     <title>automatically onBlur</title> 
     <description> 
     when a date is selected. For an inline calendar, simply attach the datepicker to a div or span. 
     </description> 
    </event> 
    </events> 
</xml> 

,我使用這個XPath來選擇的節點

$xml = simplexml_load_file($file); 
$nodes = $xml->xpath('//xml/events'); 

它會選擇所有nodes.I要選擇基於節點在日期。

回答

7

指定XPath表達式的日期,

$nodes = $xml->xpath('//xml/events[@date="14/12/2011"]'); 

將在本例中只選擇最後一個活動節點

4

使用

$xml = simplexml_load_string($xml); 
$nodes = $xml->xpath('//events[@date="14/12/2011"]'); 
print_r($nodes); 

得到事件節點在具有指定日期的xml節點下面並且

$xml = simplexml_load_string($xml); 
$nodes = $xml->xpath('//xml/events[@date]'); 
print_r($nodes); 

獲取具有日期屬性的xml節點下的所有事件。同樣,使用

$xml = simplexml_load_string($xml); 
$nodes = $xml->xpath('//events[contains(@date, "2011")]'); 
print_r($nodes); 

可以在包含字符串「2011」的日期屬性中查找文檔中任何地方的所有事件節點。您可以使用simplexml_load_file直接加載XML文件。