Implementing condition in XPath [duplicate]

Solution 1:

Specify the date in the xpath expression,

i.e.

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

would select only the last events-node in the example

Solution 2:

Use

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

to get the event node below the xml node with the specified date and

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

to get all event below the xml node nodes having a date attribute. Likewise, use

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

to find all event nodes anywhere in the document with a date attribute containing the string "2011".

On a sidenote, you can use simplexml_load_file to load an XML file directly.