简体   繁体   中英

PHP: How can I easily combine or merge simpleXML elements using a node value?

I'm kinda new here in stackoverflow so forgive me in advance. :)

I'm trying to merge objects with the same BranchCode and basically, just making the Branch as a child of the main Product node. Please see my sample XML below. Thanks.

I have this XML ( simpleXMLElement->asXML() ):

    <?xml version="1.0" encoding="utf-8"?>
<ArrayOfProduct xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>14</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>150</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>226</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>227</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>26</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>34</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>35</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>400A</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>405A</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>460A</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>57</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>83</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>C3</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-02-2166</ProductCode>
    <BranchCode>Global</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>14</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>150</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>226</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>227</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>26</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>34</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>35</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>400A</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>405A</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>460A</BranchCode>
    <Available>5.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>57</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>83</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>C3</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>0.00</AvailableGlobally>
  </Product>
  <Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-051-2030</ProductCode>
    <BranchCode>Global</BranchCode>
    <Available>0.00</Available>
    <AvailableCSL>0.00</AvailableCSL>
    <AvailableGlobally>5.00</AvailableGlobally>
  </Product>
</ArrayOfProduct>

And I want an output that is similar to this:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfProduct xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Product>
    <Customer>238500</Customer>
    <ProductCode>AAA-50-3535</ProductCode>
    <Branch>
       <BranchCode>C3</BranchCode>
       <Available>10.00</Available>
       <AvailableCSL>0.00</AvailableCSL>
       <AvailableGlobally>100.00</AvailableGlobally>
    </Branch>
    <Branch>
       <BranchCode>A5</BranchCode>
       <Available>20.00</Available>
       <AvailableCSL>0.00</AvailableCSL>
       <AvailableGlobally>100.00</AvailableGlobally>
    </Branch>
    ....
    ....
  </Product>
  ....
  ....
</ArrayOfProduct>

There isn't any built-in function that would merge documents, so you have to do that "by hand." One way to do it would be in PHP, using DOM . Select the <Product> nodes you want to process via XPath, create a <Branch> node to which you move all the child nodes, then append the <Branch> nodes to the correct <Product> .

$dom = new DOMDocument;
// Those two options are purely for cosmetic reasons, you can remove them
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;
$dom->load('old.xml');

$ProductNodes = array();

$DOMXPath = new DOMXPath($dom);

foreach ($DOMXPath->query('/ArrayOfProduct/Product') as $Product)
{
    // Create a new <Branch/>
    $Branch = $dom->createElement('Branch');

    // Move the nodes to the <Branch/>, except for <Customer/> and <ProductCode/>
    $childNodes = $DOMXPath->query('./*[name() != "Customer"][name() != "ProductCode"]', $Product);
    foreach ($childNodes as $child)
    {
        $Branch->appendChild($Product->removeChild($child));
    }

    $key = $Product->getElementsByTagName('Customer')->item(0)->textContent
         . ':'
         . $Product->getElementsByTagName('ProductCode')->item(0)->textContent;

    // If it's not the first product with that combination of Customer:ProductCode, we remove the
    // node, otherwise we keep it and we'll append other branches to it
    if (isset($ProductNodes[$key]))
    {
        $Product->parentNode->removeChild($Product);
    }
    else
    {
        $ProductNodes[$key] = $Product;
    }

    $ProductNodes[$key]->appendChild($Branch);
}

echo $dom->saveXML();

Alternatively, you can change the structure of a document with an Identity Transform . Here's one such example: (this one doesn't handle PI and comment nodes)

copy.xsl

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" encoding="utf-8" indent="yes" />

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

    <xsl:template match="/ArrayOfProduct/Product">
        <!-- Check if it's the first Product node with that combination of Customer and ProductCode -->
        <xsl:if test="not(preceding-sibling::Product[Customer = current()/Customer and ProductCode = current()/ProductCode])">
            <xsl:copy>
                <!-- Copy the Customer and ProductCode nodes first -->
                <xsl:copy-of select="Customer | ProductCode" />

                <!-- Create a Branch for every Product with that combination of Customer and ProductCode -->
                <xsl:for-each select="/ArrayOfProduct/Product[Customer = current()/Customer and ProductCode = current()/ProductCode]">
                    <Branch>
                        <!-- Copy their children, except for Customer and ProductCode -->
                        <xsl:copy-of select="*[name() != 'Customer'][name() != 'ProductCode']"/>
                    </Branch>
                </xsl:for-each>
            </xsl:copy>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

You can run it in PHP:

$xml = new DOMDocument;
$xml->load('old.xml');

$xsl = new DOMDocument;
$xsl->load('copy.xsl');

$xslt = new XSLTProcessor;
$xslt->importStylesheet($xsl);

echo $xslt->transformToXml($xml);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM