简体   繁体   English

使用XPath在php中搜索多个属性的XML

[英]Searching XML using XPath for multiple attributes in php

I found same topics but I didn't solve my problem :( 我找到了相同的主题,但我没有解决问题:(

I want to search in XML with product code 我想用产品代码搜索XML

this is my code; 这是我的代码;

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

if I remove "/STOKADI/GRUPKODU", its working. 如果我删除“ / STOKADI / GRUPKODU”,它的工作原理。 But Product title, product image, product category didn't show. 但是没有显示产品标题,产品图片,产品类别。 Where is the problem? 问题出在哪儿?

And my second problem, when I want to show all products; 还有第二个问题,当我想展示所有产品时; I see some products at least 4-5 times. 我看到一些产品至少4-5次。

(Note: I work in WordPress) (注意:我在WordPress中工作)

I don't use simple_xml but the approach below, using DOMDocument and DOMXPath should be easy enough to port to simple_xml if desired. 我不使用simple_xml但是下面的方法,使用DOMDocumentDOMXPath应该足够容易,如果需要,可以移植到simple_xml Initially I thought you were looking for child nodes of STOK_KODU but the structure of the XML suggests otherwise. 最初,我以为您正在寻找STOK_KODU子节点,但是XML的结构则STOK_KODU The XPath query will find the node with the relevant $product_code from which you can easily find the parent and thus any/all of it's children. 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 ) ) );
    }
}

The output of which will be 其输出将是

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

To access each field as a variable ( which is what I understand your comment to be ) 以变​​量形式访问每个字段(根据我的理解,这就是您的评论)

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;
}

and to access as variables, use extract 并将其用作变量,请使用extract

            if( !empty( $output ) ){
                foreach( $output as $obj ){

                    extract( $obj );
                    printf("<pre>%s\n%s\n%s</pre>", $STOK_KODU, $STOK_ADI, $GRUPKODU );

                }
            }

If you look at the structure of the XML, the GRUPKODU and STOKADI are elements at the same level. 如果查看XML的结构,则GRUPKODU和STOKADI是同一级别的元素。 So trying to access these in you XPath at the same time isn't going to work. 因此,尝试同时在您的XPath中访问它们是行不通的。 If instead you look for the XMLWEBDATA element your after, you can then access these elements inside this element. 相反,如果您以后寻找XMLWEBDATA元素,则可以在此元素内访问这些元素。 This XPath looks for an XMLWEBDATA with a STOK_KODU with content that contains the product code your after, then using foreach to print out each match, it outputs the two values your after... 该XPath会查找带有STOK_KODU的XMLWEBDATA,其内容包含您的after产品代码,然后使用foreach打印出每个匹配项,并输出after之后的两个值...

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

(I only cast the return values to strings as this is needed if assigning to other values, echo will automatically cast it anyway and it can cause confusing results when used in assignments) (我只将返回值转换为字符串,因为如果分配给其他值, echoecho仍会自动将其转换为字符串,并且在赋值中使用时可能导致混乱的结果)

If I set 如果我设置

$product_code = 14037;

This outputs 这个输出

EPSON T2711XL (27XL) > WF3620,3640,7110,7610,7620  Black
TÜKETİM ÜRÜNLERİ GRUBU

Note: You may want to use.... 注意:您可能要使用...。

$products = $xml->xpath("//XMLWEBDATA[STOK_KODU='".$product_code."']");

to ensure that you only find a code that fully matches in case there are cases where it will match parts of other codes. 以确保您只找到完全匹配的代码,以防某些情况下它将与其他代码的某些部分匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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