我发现了同样的话题,但我没有解决我的问题
我想使用产品代码在XML中搜索。
这是我的密码;
$xml = simplexml_load_file('http://xml.aksel.com.tr/Xml.aspx?SK=d021da08&CK=50288');
$product_code = $_GET['s'];
$products = $xml->xpath("//STOK_KODU[contains(text(), '".$product_code."')]/STOK_ADI/GRUPKODU");如果我删除"/STOKADI/GRUPKODU",它就能工作。但是产品名称、产品形象、产品类别却没有显示出来。问题出在哪里?
我的第二个问题,当我想展示所有的产品,我看到一些产品至少4-5次。
(注:我在WordPress工作)
发布于 2018-01-10 08:50:35
我不使用simple_xml,但是下面使用DOMDocument和DOMXPath的方法应该非常容易,如果需要的话,可以移植到simple_xml。起初,我以为您在寻找STOK_KODU的子节点,但是XML的结构表明并非如此。XPath查询将找到具有相关$product_code的节点,您可以从该节点轻松找到父节点,从而找到它的任何/所有子节点。
$file='http://xml.aksel.com.tr/Xml.aspx?SK=d021da08&CK=50288';
#$file='c:/temp/Xml.xml'; /* saved locally to test */
$output=array();
$product_code=13775;
$query='//XMLWEBDATA/STOK_KODU[ contains( text(), "'.$product_code.'" ) ]';
$dom=new DOMDocument;
$dom->load( $file );
$xp=new DOMXPath( $dom );
$col=$xp->query( $query );
if( $col && $col->length > 0 ){
foreach( $col as $node ){
/* get the parent */
$parent=$node->parentNode;
$data=array();
for( $i=0; $i < $parent->childNodes->length; $i++ ){
$tag=$parent->childNodes->item( $i )->tagName;
$value=$parent->childNodes->item( $i )->nodeValue;
if( !empty( $tag ) && !empty( $value ) ) $data[ $tag ]=$value;
}
$output[]=$data;
}
/* remove duplicates if there are any */
$output=array_unique( $output );
}
$xp = $dom = null;
/* process the results accordingly */
if( !empty( $output ) ){
foreach( $output as $obj ){
printf('<pre>%s</pre>', urldecode( http_build_query( $obj, null, PHP_EOL ) ) );
}
}其产出将是
STOK_KODU=13775
STOK_ADI=CHIP EPSON C3800 Black (S051127)
LISTEFIYAT=2,73
STOKBAKIYE1=16
GRUPKODU=DOLUM ÜRÜNLERİ GRUBU
KOD1=TONER DOLUM ÜRÜNLERİ
KOD2=ÇİPLER
PARABIRIMI=$
KULL5N=9500
RESIMURL=http://xml.aksel.com.tr/Photo.aspx?ID=22705&STOK=13775将每个字段作为变量访问(我理解您的注释是这样的)
foreach( $col as $node ){
/* get the parent */
$parent=$node->parentNode;
$data=array();
for( $i=0; $i < $parent->childNodes->length; $i++ ){
try{
/* test node type to avoid errors */
if( $parent->childNodes->item( $i )->nodeType==XML_ELEMENT_NODE ){
$tag=$parent->childNodes->item( $i )->tagName;
$value=$parent->childNodes->item( $i )->nodeValue;
if( !empty( $tag ) && !empty( $value ) ) $data[ $tag ]=$value;
}
}catch( Exception $e ){
echo $e->getMessage();
continue;
}
}
$output[]=$data;
}若要作为变量访问,请使用extract
if( !empty( $output ) ){
foreach( $output as $obj ){
extract( $obj );
printf("<pre>%s\n%s\n%s</pre>", $STOK_KODU, $STOK_ADI, $GRUPKODU );
}
}发布于 2018-01-10 07:59:32
如果您查看XML的结构,则GRUPKODU和STOKADI是同一级别的元素。因此,在您的XPath中同时访问这些内容是行不通的。如果您在后面查找XMLWEBDATA元素,则可以访问该元素中的这些元素。该XPath查找一个XMLWEBDATA,其内容包含产品代码“您的后”,然后使用foreach打印出每个匹配项,然后输出两个值.
$products = $xml->xpath("//XMLWEBDATA[STOK_KODU[contains(text(), '".$product_code."')]]");
foreach ( $products as $product ) {
echo (string)$product->STOK_ADI.PHP_EOL;
echo (string)$product->GRUPKODU.PHP_EOL;
}(我只将返回值转换为字符串,因为如果将返回值赋值给其他值,echo无论如何都会自动转换它,并且在赋值中使用时会造成混乱的结果)
如果我设置
$product_code = 14037;这输出
EPSON T2711XL (27XL) > WF3620,3640,7110,7610,7620 Black
TÜKETİM ÜRÜNLERİ GRUBU注:您可能需要使用..。
$products = $xml->xpath("//XMLWEBDATA[STOK_KODU='".$product_code."']");以确保您只找到完全匹配的代码,以防出现与其他代码部分匹配的情况。
https://stackoverflow.com/questions/48182353
复制相似问题