How to correct this code for adding selected xml tags from one xml source into another using xpath and php class methods
<p>I want to read the following xml and find specific xml tags by their id values , Finally i want to copy these selected tags inside another xml using php user define class methods: sample given below:</p>

<pre><code>    &lt;items&gt;
       &lt;item&gt;
       &lt;id&gt;1&lt;/id&gt;
       &lt;name&gt;Item-1&lt;/name&gt;
       &lt;price&gt;10&lt;/price&gt;
       &lt;/item&gt;

       &lt;item&gt;
       &lt;id&gt;2&lt;/id&gt;
       &lt;name&gt;Item-2&lt;/name&gt;
       &lt;price&gt;20&lt;/price&gt;
       &lt;/item&gt;

       &lt;item&gt;
       &lt;id&gt;3&lt;/id&gt;
       &lt;name&gt;Item-3&lt;/name&gt;
       &lt;price&gt;30&lt;/price&gt;
       &lt;/item&gt;         
   &lt;/items&gt;

&lt;customer&gt;
   &lt;purchasedItems&gt;
      // add the xml here for the purchased Item chosen by id values 
   &lt;/purchasedItems&gt;
&lt;/customer&gt;
</code></pre>

<p>If customer purchased Item-1 and Item-3, then output will be:</p>

<pre><code>&lt;customer&gt;
       &lt;purchasedItems&gt;
          &lt;item&gt;
           &lt;id&gt;1&lt;/id&gt;
           &lt;name&gt;Item-1&lt;/name&gt;
           &lt;price&gt;10&lt;/price&gt;
           &lt;/item&gt;
           &lt;item&gt;
           &lt;id&gt;3&lt;/id&gt;
           &lt;name&gt;Item-3&lt;/name&gt;
           &lt;price&gt;30&lt;/price&gt;
           &lt;/item&gt;  
       &lt;/purchasedItems&gt;
&lt;/customer&gt;
</code></pre>

<p>i try with the following code which is not working :</p>

<pre><code>&lt;?php
header('Content-Type: text/xml');
class Items {

    public static function addItem($purchasedItemsXml, $Items, $parentTag, $idTag ,$id)
    {   
        $docSource = new DOMDocument();
        $docSource-&gt;loadXML($Items);
        $docDest = new DOMDocument();
        $docDest-&gt;loadXML($purchasedItemsXml);
        $xpath = new DOMXPath($docSource);      
        $result = $xpath-&gt;query("//{$idTag}[text()='{$id}']/parent::*");        
        $result = $docDest-&gt;importNode($result-&gt;item(0), true);     
        $items = $docDest-&gt;getElementsByTagName($parentTag)-&gt;item(0);
        $items-&gt;appendChild($result);               
        echo $docDest-&gt;saveXML();             
        return ;
    }
}
</code></pre>

<p><strong>Updates: After changing Following code,Now Its Working Thanks to all.</strong></p>

<pre><code>class purchasedItems extends Items{
     public static function  addPurchasedItems($purchasedItemsXml, $Items, $parentTag, $idTag, $ids){ 
        $count =0;
         foreach ($ids as $id) {         
             if($count==0){
                $xml = self::addItem($purchasedItemsXml, $Items, $parentTag, $idTag, $id);
             }
             else{
             $xml = self::addItem($xml, $Items, $parentTag, $idTag, $id);
             } 
             $count++;

        }       
         echo $xml;

        return ;
    } 
}



$Items =  "&lt;items&gt;
               &lt;item&gt;
               &lt;id&gt;1&lt;/id&gt;
               &lt;name&gt;Item-1&lt;/name&gt;
               &lt;price&gt;10&lt;/price&gt;
               &lt;/item&gt;         
               &lt;item&gt;
               &lt;id&gt;2&lt;/id&gt;
               &lt;name&gt;Item-2&lt;/name&gt;
               &lt;price&gt;20&lt;/price&gt;
               &lt;/item&gt;         
               &lt;item&gt;
               &lt;id&gt;3&lt;/id&gt;
               &lt;name&gt;Item-3&lt;/name&gt;
               &lt;price&gt;30&lt;/price&gt;
               &lt;/item&gt;         
           &lt;/items&gt;";

$purchasedItemsXml = "&lt;customer&gt;
                          &lt;purchasedItems&gt;

                          &lt;/purchasedItems&gt;
                      &lt;/customer&gt;";
$ids = array(1, 3);
$parentTag = 'purchasedItems';
$idTag = 'id';
purchasedItems::addPurchasedItems($purchasedItemsXml, $Items, $parentTag, $idTag, $ids);
?&gt;
</code></pre>

<p>as a beginner, i am sure i did some mistake, but i am now stuck at point.I want to do this using the existing given class hierarchy.please help me with this code.Thanks</p>