0

I want to display the list elements of the ul but not the last one. I have used DOM nut it takes a long time.. Can Someone please give me the Xpath expression to solve this. Please Provide the whole solution code.

 $doc = new DOMDocument();  
    @$doc->loadHTMLFile($sel_image['snapdeal_content']);  
    $divs = $doc->getElementsByTagName('ul');  
    foreach($divs as $div) {  
    if ($div->getAttribute('class') == 'key-features') {  
        $li = $div->getElementsByTagName('li');  
        for($j=0;$j<$li->length-1;$j++){  
         echo "->".$li->item($j)->nodeValue;  
         echo "<br />";  
            }  

        }  

    }
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
Manoj Majumdar
  • 505
  • 1
  • 4
  • 23

1 Answers1

1

Try this excerpt to replace the for-loops in your solution. The $li array should contain all n-1 <li> elements of all <ul> enumerations in the document.

$xpath = new DOMXPath($doc);
$query = '//ul[@class = "key-features"]/li[position() < last()]';
$li = $xpath->query($query);

Also see http://www.php.net/manual/en/domxpath.query.php and XSL for-each: how to detect last node? .

Community
  • 1
  • 1
Marcus Rickert
  • 4,138
  • 3
  • 24
  • 29