PHP:按属性从xml中删除节点

时间:2016-07-11 15:58:28

标签: php xml nodes unset

假设我有一个像这样的xml:

<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>

如果此属性在数组中,我需要删除某个节点,具体取决于id属性。

我尝试了不同的方法,但xml总是以原始方式输出!

到目前为止我的代码:

<?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(); ?>

非常感谢大家。

1 个答案:

答案 0 :(得分:1)

考虑部分XPath和XSLT解决方案,Extensible Stylesheet Family中的兄弟姐妹。 XPath首先用于检索所有当前的产品ID,然后将其与当前的id数组进行比较以继续使用array_diff。然后迭代地构建XSLT以根据这些不匹配的id移除节点。删除XSLT中的节点只需要一个空模板匹配。

// 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();