2016-07-11 12 views
0

nehme an, ich habe eine XML wie folgt aus:PHP: entfernen Knoten aus XML durch Attribut

<products> 
    <product id="1"> 
     <name>aaa</name> 
     <producturl>aaa</producturl> 
     <bigimage>aaa</bigimage> 
     <description>aaa</description> 
     <price>aaa</price> 
     <categoryid1>aaa</categoryid1> 
     <instock>aaa</instock> 
    </product> 
    <product id="2"> 
     <name>aaa</name> 
     <producturl>aaa</producturl> 
     <bigimage>aaa</bigimage> 
     <description>aaa</description> 
     <price>aaa</price> 
     <categoryid1>aaa</categoryid1> 
     <instock>aaa</instock> 
    </product> 
</products> 

und ich brauche in Abhängigkeit von der id-Attribut bestimmten Knoten zu löschen, wenn dieses Attribut in einem Array ist.

Ich habe verschiedene Möglichkeiten ausprobiert, aber das XML wird immer als das Original ausgegeben!

Mein Code so weit:

<?php header("Content-type: text/xml"); 
$url="http://www.aaa.it/aaa.xml"; 
$url=file_get_contents($url); 
$array=array("1","4","5"); 
$doc=new SimpleXMLElement($url); 
foreach($doc->product as $product){ 
    if(!in_array($product['id'],$array)){ 
     $dom=dom_import_simplexml($product); 
     $dom->parentNode->removeChild($dom); 
     // unset($doc->product->$product); 
    } 
} 
echo $doc->asXml(); ?> 

Vielen Dank alle.

Antwort

1

Betrachten Sie eine teilweise XPath und XSLT-Lösung, beide Geschwister in der Extensible Stylesheet Family. XPath wird zuerst verwendet, um alle aktuellen Produkt-IDs abzurufen, die dann mit dem aktuellen Array von IDs verglichen werden, um weiterhin array_diff zu verwenden. XSLT wird dann iterativ aufgebaut, um Knoten gemäß diesen nicht übereinstimmenden IDs zu entfernen. Das Entfernen von Knoten in XSLT erfordert einfach eine leere Vorlagenübereinstimmung.

// Load the XML source 
header("Content-type: text/xml"); 
$url="http://www.aaa.it/aaa.xml"; 
$url=file_get_contents($url); 
$doc=new SimpleXMLElement($url); 

// Retrieve all XML product ids with XPath 
$xpath = $doc->xpath("//product/@id"); 
$xmlids = []; 
foreach($xpath as $item => $value){ $xmlids[] = (string)$value; } 

// Compare difference with $array 
$array = array("1","4","5"); 
$removeids = array_diff($xmlids, $array); 

// Dynamically build XSLT string for each resulting id 
foreach($removeids as $id){   
    $xslstr='<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
      <xsl:output version="1.0" encoding="UTF-8" indent="yes" /> 
      <xsl:strip-space elements="*"/> 

       <xsl:template match="@*|node()"> 
       <xsl:copy> 
        <xsl:apply-templates select="@*|node()"/> 
       </xsl:copy> 
       </xsl:template> 

       <xsl:template match="product[@id=\''.$id.'\']"/> 

      </xsl:transform>';      
    $xsl = new SimpleXMLElement($xslstr); 

    // Configure the transformer and run 
    $proc = new XSLTProcessor; 
    $proc->importStyleSheet($xsl); 
    $newXML = $proc->transformToXML($doc); 

    // Adjust $doc object with each loop 
    $doc = new SimpleXMLElement($newXML); 
} 

// Echo Output  
echo $doc->asXML(); 
+0

Ich habe nicht, dass die Verlängerung (XSLTProcessor) – Zak

+1

Sie können die [Erweiterung] aktivieren (http://stackoverflow.com/questions/7930726/fatal-error-class-xsltprocessor-not-found) in Ihre INI-Datei. – Parfait

Verwandte Themen