doujiao3016
2016-07-11 15:58
浏览 75

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

suppose I have an xml like this:

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

and I need to delete certain node depending on the id attribute, if this attribute is in an array.

I've tried different ways, but the xml is outputted always as the original one!

My code so far:

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

Thanks a lot everyone.

图片转代码服务由CSDN问答提供 功能建议

假设我有一个这样的xml:

 &lt; 产品&gt; 
&lt; product id =“1”&gt; 
&lt; name&gt; aaa&lt; / name&gt; 
&lt; producturl&gt; aaa&lt; / producturl&gt; 
&lt; bigimage&gt; aaa&lt; / bigimage&gt; 
&lt;  ; description&gt; aaa&lt; / description&gt; 
&lt; price&gt; aaa&lt; / price&gt; 
&lt; categoryid1&gt; aaa&lt; / categoryid1&gt; 
&lt; instock&gt; aaa&lt; / instock&gt; 
&lt; / product&gt; 
  &lt; product id =“2”&gt; 
&lt; name&gt; aaa&lt; / name&gt; 
&lt; producturl&gt; aaa&lt; / producturl&gt; 
&lt; bigimage&gt; aaa&lt; / bigimage&gt; 
&lt; description&gt; aaa&lt;  ; / description&gt; 
&lt; price&gt; aaa&lt; / price&gt; 
&lt; categoryid1&gt; aaa&lt; / categoryid1&gt; 
&lt; instock&gt; aaa&lt; / instock&gt; 
&lt; / product&gt; 
&lt; / products&gt;  
   
 
 

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

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

到目前为止我的代码: \ n

 &lt;?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-&gt; product as  $ product({
 if if(!in_array($ product ['id'],$ array)){
 $ dom = dom_import_simplexml($ product); 
 $ dom-&gt; parentNode-&gt; removeChild($ dom  ); 
 // unset($ doc-&gt; product-&gt; $ product); 
} 
} 
echo $ doc-&gt; asXml();  ?&gt; 
   
 
 

非常感谢大家。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • duandian2725 2016-07-11 16:58
    最佳回答

    Consider a partly XPath and XSLT solution, both siblings in the Extensible Stylesheet Family. XPath is first used to retrieve all current product ids which is then compared with current array of ids to keep using array_diff. XSLT is then iteratively built to remove nodes according to these unmatched ids. Removing nodes in XSLT requires simply an empty template match.

    // 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();   
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题