DOMXPath / PHP - 如何从foreach中的parrent节点获取属性

时间:2016-08-03 17:04:43

标签: php xpath domdocument domxpath

这是我的代码:

let jumpOptions (sourceX, sourceY) = function
  Allocated (Red (_, (x, y)) as p)
| Allocated (Black (_, (x, y)) as p) when abs (sourceX - x) = 1
    -> y = sourceY - yDirection p
| _ -> false

let jumpsForSoldier = function
  Red (_, pos)
| Black (_, pos) -> List.filter (jumpOptions pos)

这是DOM HTML:

$url = "http://www.sportsdirect.com/flash-sale";
$statuss = 1;


$curl = curl_init();
    curl_setopt($curl, CURLOPT_COOKIE, "ChosenSite=www; SportsDirect_AnonymousUserCurrency=GBP; language=en-GB");
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSLVERSION, 3);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
    curl_setopt($curl, CURLOPT_VERBOSE, true);
    $str = curl_exec($curl);  
    curl_close($curl);  



    libxml_use_internal_errors(true); 
    $doc = new DOMDocument();
    $doc->loadHTML($str);

    $xpath = new DOMXpath($doc);

    $Sizes = $xpath->query('//ul[@class="bxslider"]//li//a/img');
    $AddSlides = $xpath->query('//ul[@class="bxslider2"]//li//a/img');
    $AddSlides2 = $xpath->query('//ul[@class="bxslider3"]//li//a/img');
    $N = 1;
    $i = 0;
    foreach ($Sizes as $item) {
        $N++;
        $i++;
        $ImageAlt = $item->getAttribute("alt");
        $Image = $item->getAttribute("src");
        $DataImage = $item->getAttribute("data-src");

        $Link = $xpath->query('//ul[@class="bxslider"]//li/a')->item(0)->getAttribute("href");

        if($DataImage == ""){
        echo "<img src='$Image'> LINK: $Link <br>";
        //$write->insert("responsivebannerslider_slide", array("group_names" => "1", "titles" => "$ImageAlt", "img_video" => "image", "img_hosting" => "1", "hosted_url" => "$Image", "url" => "$url", "url_target" => "new_window", "url_target" => "new_window", "date_enabled" => "0","sort_order" => "$N", "type" => "1", "statuss" => "$statuss"));
        }
        if($DataImage != ""){
            //$write->insert("responsivebannerslider_slide", array("group_names" => "1", "titles" => "$ImageAlt", "img_video" => "image", "img_hosting" => "1", "hosted_url" => "http://images.sportsdirect.com$DataImage", "url" => "$url", "url_target" => "new_window", "url_target" => "new_window", "date_enabled" => "0", "sort_order" => "$N",  "type" => "1", "statuss" => "$statuss"));
        echo "<img src='http://images.sportsdirect.com$DataImage'> LINK: $Link <br>";
        }

我能够获取所有图像,但我想获得parrent <ul class="bxslider"> <li><a href="/flash-sale-three"> <img class="img-responsive" src="//images.sportsdirect.com/images/marketing/flash-sale-slide-160803.jpg" alt="Flash Sale"> </a></li> <li><a href="/pricecrash/price-crash-3"> <img class="img-responsive" data-src="/images/marketing/Price-Crash-160803.jpg" alt="Price Crash"> </a> </li> <li><a href="/special-offer"> <img class="img-responsive" data-src="/images/marketing/weekly-offer-front-site-160721.jpg" alt="Offer Of The Week"> </a> </li> </ul> 所拥有的链接。

我如何获得parrent节点属性?

我使用此<a href="TARGET-HERE"></a>获取所有图片,但我不能在foreach循环中仅使用此ul[@class="bxslider"]//li//a/img并使用属性ul[@class="bxslider"]//li/a为什么?

我希望我已经解释得很好,如果没有,请问我你不理解的事情。

我的错误在哪里?

1 个答案:

答案 0 :(得分:0)

您可以将$ item的父节点的属性设为:

$Link = $item->parentNode->getAttribute("href");