0

これは私の元のXMLであり、SQLを実行して、レコードを1つだけ選択できるようにしたいのですが、正常に実行できたので、必要なレコードのみを取得したいと思います。

<catalog>
<product>
<programname>Teva Footwear</programname>
<programurl>http://www.teva.com?cid=aff_pr</programurl>
<catalogname>Teva Footwear Product Catalog</catalogname>
<lastupdated>03/25/2013</lastupdated>
<name>Teva Men Tanza Leather Sport Sandals in Brown</name>
<keywords>Teva Men Tanza Leather Sport Sandals in Brown</keywords>
<description>...</description>
<sku>1000183-BRN</sku>
<manufacturer>Teva</manufacturer>
<manufacturerid>1000183-BRN</manufacturerid>
<currency>USD</currency>
<saleprice>85.00</saleprice>
<price>85.00</price>
<retailprice>85.00</retailprice>
<fromprice>Yes</fromprice>
<buyurl>...</buyurl>
<impressionurl>http://www.awltovhc.com/image-6059025-10757671</impressionurl>
<imageurl>...</imageurl>
<advertisercategory>Men</advertisercategory>
<promotionaltext>Fast, Free Shipping and Free Returns!</promotionaltext>
<online>Yes</online>
<instock>YES</instock>
<condition>New</condition>
<warranty>...</warranty>
<standardshippingcost>0.0</standardshippingcost>
</product>
<product>...</product>
<product>
<programname>Teva Footwear</programname>
<programurl>http://www.teva.com?cid=aff_pr</programurl>
<catalogname>Teva Footwear Product Catalog</catalogname>
<lastupdated>03/25/2013</lastupdated>
<name>Teva Men Original Mush Flip Flops in Tread Black</name>
<keywords>Teva Men Original Mush Flip Flops in Tread Black</keywords>
<description>
See what all the hype is about and get yourself a pair of men's Teva Original Mush sandals today! Mush is the ultimate in flip flop foot-forming comfort. The Original Mush features a thicker sole and slightly wider forefoot and heel for a more generous fit.

これは私がこれまでに行ったことです(もちろん私はオンラインでコードを見つけました)

<?php 

// ----------------  The example uses the php 5 "DOM Functions"  to select specific node uding Xpath query ------------------  

$doc = new DOMDocument;                                        // Create a new dom document 
$doc->preserveWhiteSpace = false;                              // Set features 
$doc->formatOutput = true;                                     // Create indents on xml 

$doc->Load('Teva_Footwear-Teva_Footwear_Product_Catalog.xml');                                        // Load the file 

$xpath = new DOMXPath($doc); 
$query = '//catalog/product/sku[. = "1000183-WAL"]';         // The xpath (starts from root node) 
$names = $xpath->query($query);                                 // A list of matched elements 

$Output=""; 
foreach ($names as  $node) { 
   $Output.=$doc->saveXML($node->parentNode)."\n";             // We get the parent of  "<firstname>" element  (the entire "<person>" node and its children) (maybe get 

the parent node directly using xpath) 
                                                                // and use the saveXML() to convert it to string   
} 


echo $Output."<br>\n\n";                                      // The result  

echo "<hr><br><b>Below view the results  as HTML content. &nbsp; (See also the page's HTML code):</b>\n<pre>".htmlspecialchars($Output)."</pre>"; 

?>

これは、このコードの後に​​出力されます:(製造元IDによって選択されたレコード)

<product>
  <programname>Teva Footwear</programname>
  <programurl>http://www.teva.com?cid=aff_pr</programurl>
  <catalogname>Teva Footwear Product Catalog</catalogname>
  <lastupdated>03/25/2013</lastupdated>
  <name>Teva Men Tanza Leather Sport Sandals in Walnut</name>
  <keywords>Teva Men Tanza Leather Sport Sandals in Walnut</keywords>
  <description>Following in the footsteps of our other sports sandals (no pun intended)  the Tanza Leather was built to perform. Featuring our Universal Strapping System with 3 points of adjustment  as well as Shoc Pad&amp;trade; technology  the Tanza Leather gives you a luxurious feel without sacrificing functionality.</description>
  <sku>1000183-WAL</sku>
  <manufacturer>Teva</manufacturer>
  <manufacturerid>1000183-WAL</manufacturerid>
  <currency>USD</currency>
  <saleprice>85.00</saleprice>
  <price>85.00</price>
  <retailprice>85.00</retailprice>
  <fromprice>Yes</fromprice>
  <buyurl>http://www.dpbolvw.net/click-6059025-10757671?url=http%3A%2F%2Fwww.teva.com%2Fwomens-tanza-leather-feminine-sports-sandals%2F737872627089%252Cdefault%252Cpd.html</buyurl>
  <impressionurl>http://www.awltovhc.com/image-6059025-10757671</impressionurl>
  <imageurl>http://www.teva.com/on/demandware.static/Sites-TEVA-US-Site/Sites-masterCatalogTeva/default/v1364225639089/images/large/T0183-WAL_1.jpg</imageurl>
  <advertisercategory>Men</advertisercategory>
  <promotionaltext>Fast, Free Shipping and Free Returns!</promotionaltext>
  <online>Yes</online>
  <instock>YES</instock>
  <condition>New</condition>
  <warranty>Teva has a 1 year warranty against defective materials and/or workmanship</warranty>
  <standardshippingcost>0.0</standardshippingcost>
</product>

しかし、そのレコードのすべてが表示されているので、必要に応じてフォーマットできるようになります。メタ価格の説明と写真のキーワードは、次のようなURLを購入してリンクされています<a href="$buyurl"><img src="$image_url"></a>

4

1 に答える 1

0

simplexmlを使用したソリューション:

$xml = simplexml_load_file('file.xml');

list($product) = $xml->xpath("//product[sku = '1000183-WAL']");

echo "<a href=\"{$product->imageurl}\"><img src=\"{$product->imageurl}\" /></a>";
于 2013-03-27T23:21:29.020 に答える