Friday, January 22, 2010

Conditionally query specific elements within an XML document

Given:

     the name
     the desc
     misc garbage
     1


Suppose you had 100 of these and wanted to obtain just the name and description for a specific id

<?php
$doc = simplexml_load_string();

$results = $doc->xpath("//tree[contains(id,'1')]/child::*[self::name or self::desc]"); 

?>

That will get the job done