Ben Paton
Ben Paton

Reputation: 1442

Match an id in existing xml and output matched parts of the xml as new xml

Hello I have an xml feed which has many products and each product can have mutiple skus. I'm trying to loop over that xml and then if I find a match for the EAN code then I'm trying to output the matched skus as a seperate xml file.

The code I have so far is below, please can you point out where I am going wrong as it's not quite working yet and it's outputting an empty response.

$ean = $_GET["ean"];
$trialXML = simplexml_load_file('trial.xml');

//function to lookup an EAN
function lookupEAN($ean,$xml) {

    //loop over products
    foreach($xml->products->product as $product) {
        //loop over individual skus
        foreach($product->sku as $sku) {
            if ($sku->ean == $ean) {  

                $skuData .= $sku;

            }
        }
    }
    return $skuData;
}

//lookup EAN
$response = lookupEAN($ean,$trialXML);

Header('Content-type: text/xml');
print_r($response);

Thi is an example product from the xml:

<product>
        <id>11077810</id>
        <name>Cooke &amp; Lewis Flow Soap Dispenser</name>
        <categoryId>9372170</categoryId>
        <features/>
        <url>http://www.diy.com/diy/jsp/bq/nav.jsp?action=detail&amp;fh_secondid=11077810&amp;fh_location=//catalog01/en_GB/categories&lt;{9372014}/categories&lt;{9372035}/categories&lt;{9372170}/specificationsProductType=bathroom_fittings/specificationsSpecificProductType=soap_dishes___dispensers</url>
        <productHierarchy>
            <productHierarchyElement>Rooms</productHierarchyElement>
            <productHierarchyElement>Bathrooms</productHierarchyElement>
            <productHierarchyElement>Bathroom Accessories</productHierarchyElement>
            <productHierarchyElement>Bathroom Fittings</productHierarchyElement>
            <productHierarchyElement>Soap Dishes &amp; Dispensers</productHierarchyElement>
        </productHierarchy>
        <averageRating>0.0</averageRating>
        <numberOfReviews>0</numberOfReviews>
        <sku>
            <id>11568516</id>
            <name>Cooke &amp; Lewis Flow Soap Dispenser</name>
            <description>From the tradtional to the downright modern, we have accessories to suit the style of your home.  Choose from the classic bronze effect through to the contemporary chrome to add a finishing touch to your bathroom.  Glass and wood enhance some of our bathroom ranges and some have matching toilet seats.</description>
            <ean>0000005286911</ean>
            <price>29.98</price>
            <wasPrice/>
            <deliveryBand>Next day</deliveryBand>
            <deliveryCost>5.0</deliveryCost>
            <deliveryTime>Collect at Store</deliveryTime>
            <deliveryLeadTime>0</deliveryLeadTime>
            <stockAvailability>1</stockAvailability>

            <channel>In Store Only</channel>
            <buyerCats>
                <catLevel0>BATHROOMS</catLevel0>
                <catLevel1>WALL MOUNTED ACCESSORIES</catLevel1>
                <catLevel2>DECO</catLevel2>
            </buyerCats>
            <affiliateCats>
                <affiliateCat0>Home &amp; Garden</affiliateCat0>
            </affiliateCats>
            <categoryHierarchy>Bathroom Fittings</categoryHierarchy>
            <imageUrl>http://s7g1.scene7.com/is/image/BandQ/0000005286911_001c_v001_zp</imageUrl>
            <thumbnailUrl>http://s7g1.scene7.com/is/image/BandQ/0000005286911_001c_v001_zp?$75x75_generic$=</thumbnailUrl>
            <facets>
                <facet name="styleStyle">Flow</facet>
                <facet name="ecoRecycle">false</facet>
                <facet name="specificationsSpecificProductType">Soap Dishes &amp; Dispensers</facet>
                <facet name="ecoGrowFoods">false</facet>
                <facet name="ecoSavesEnergy">false</facet>
                <facet name="ecoNurtureNature">false</facet>
                <facet name="ecoDLME">false</facet>
                <facet name="specificationsProductType">Bathroom Fittings</facet>
                <facet name="featuresBrand">Cooke &amp; Lewis</facet>
                <facet name="ecoHealthyHomes">false</facet>
                <facet name="ecoSavesWater">false</facet>
                <facet name="featuresColour">Chrome</facet>
            </facets>
            <relatedItems>
                                <relatedItem>0000005287512</relatedItem>
                                <relatedItem>0000005287673</relatedItem>
                                <relatedItem>0000005287673</relatedItem>
                            </relatedItems>
            <optionalTags>
                <optionalTag>ROOMS15</optionalTag>
            </optionalTags>
        </sku>
    </product>

Upvotes: 0

Views: 88

Answers (1)

michi
michi

Reputation: 6625

use simplexml' withxpathto select the`node you want - in 3 lines of code:

$xml = simplexml_load_string($x); // assume XML in $x
$sku = $xml->xpath("//sku[ean='$ean']")[0]; // select the 1st <sku> node with <ean> = $ean

echo htmlentities($sku->asXML()); // echo or save it as XML

see it working: http://codepad.viper-7.com/F1FR2f

PHP < 5.4, e.g.:

$sku = $xml->xpath("//sku[ean='$ean']");
$sku = $sku[0];

Upvotes: 1

Related Questions