我目前有2个XML文件完全相同的结构,我想删除XMLfile中的entrys(或节点),如果它们存在于XMLfile2中。我正在使用的结构如下:
XMLFile1:
<ArrivingFlights>
<flight>
<to>Ciara</to>
<from>Vikki xx</from>
<imagepath>0003.jpg</imagepath>
<templateStyle>template1</templateStyle>
<time>11:00</time>
<date>19/12/15</date>
</flight>
<flight>
<to>Eadaoin</to>
<from>Dylan xx</from>
<imagepath>0005.jpg</imagepath>
<templateStyle>template1</templateStyle>
<time>18:00</time>
<date>22/12/15</date>
</flight>
<flight>
<to>Michelle</to>
<from>Brianna xx</from>
<imagepath>0001.jpg</imagepath>
<templateStyle>template1</templateStyle>
<time>17:00</time>
<date>18/12/15</date>
</flight>
<flight>
<to>Ger</to>
<from>Mammy xx</from>
<imagepath>0002.jpg</imagepath>
<templateStyle>template1</templateStyle>
<time>08:00</time>
<date>21/12/15</date>
</flight>
</ArrivingFlights>
XMLFile2:
<flight>
<to>Eadaoin</to>
<from>Dylan xx</from>
<imagepath>0005.jpg</imagepath>
<templateStyle>template1</templateStyle>
<time>18:00</time>
<date>22/12/15</date>
</flight>
在这个例子中,我想重新保存file1而不使用你在file2中看到的条目。
感谢您的时间。
答案 0 :(得分:1)
考虑使用XSLT,这是一种专门用于重构XML文件的专用语言。您甚至可以使用其document()函数引用外部XML文件。这里XSL是从外部加载的:
XSLT 脚本(另存为.xsl或.xslt文件)
<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="*"/>
<!-- Identity Transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Matches text between files and writes empty template (i.e. removes entire node) -->
<xsl:template match="flight[. = document('OtherFile.xml')/flight]"/>
</xsl:transform>
PHP 脚本
// Load the XML source and XSLT file
$doc = new DOMDocument();
$doc->load('Input.xml');
$xsl = new DOMDocument;
$xsl->load('XSLTScript.xsl');
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
// Transform XML source
$newXml = $proc->transformToXML($doc);
// Save output to file
$xmlfile = 'Output.xml';
file_put_contents($xmlfile, $newXml);
<强>输出强>
<?xml version="1.0" encoding="UTF-8"?>
<ArrivingFlights>
<flight>
<to>Ciara</to>
<from>Vikki xx</from>
<imagepath>0003.jpg</imagepath>
<templateStyle>template1</templateStyle>
<time>11:00</time>
<date>19/12/15</date>
</flight>
<flight>
<to>Michelle</to>
<from>Brianna xx</from>
<imagepath>0001.jpg</imagepath>
<templateStyle>template1</templateStyle>
<time>17:00</time>
<date>18/12/15</date>
</flight>
<flight>
<to>Ger</to>
<from>Mammy xx</from>
<imagepath>0002.jpg</imagepath>
<templateStyle>template1</templateStyle>
<time>08:00</time>
<date>21/12/15</date>
</flight>
</ArrivingFlights>
答案 1 :(得分:0)
您也可以使用Xpath查询
$message = null;
$oldXml = new DOMDocument;
$oldXml->load('xm11.xml');
$option = array();
foreach ($oldXml->getElementsByTagName('flight') as $product) {
$option[] = '( to = "' . $product->getElementsByTagName('to')->item(0)->nodeValue . '" and from ="' . $product->getElementsByTagName('from')->item(0)->nodeValue . '")';//ArrivingFlights/flight[( to = "Ciara" and from ="Vikki")] your query format
}
$option = implode('or', $option);
$newXml = new DOMDocument;
$newXml->load('xml2.xml');
$xp = new DOMXPath($newXml);
$query1 = '/ArrivingFlights/flight[' . $option . ']';
echo $query1;
foreach ($xp->query($query1) as $product) {
echo $newXml->saveXML($product);//delete it
}