0

您好,我有一个 xml 提要,其中包含许多产品,每个产品都可以有多个 skus。我正在尝试遍历该 xml,然后如果找到 EAN 代码的匹配项,那么我将尝试将匹配的 skus 输出为单独的 xml 文件。

我到目前为止的代码如下,请你指出我哪里出错了,因为它还没有完全工作并且它输出一个空响应。

$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);

这是来自 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>
4

1 回答 1

1

使用您想要的simplexml' withxpath to select the`node - 在 3 行代码中:

$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

看到它工作:http ://codepad.viper-7.com/F1FR2f

PHP < 5.4,例如:

$sku = $xml->xpath("//sku[ean='$ean']");
$sku = $sku[0];
于 2013-06-08T00:07:05.450 回答