1

how can a start a second and third XPatch query in an XPatch query?
For example

CodePad = http://codepad.viper-7.com/ZhMNGw

HTML CODE

<div class="entries">                               
    <h3 class="headline" style="position: relative; cursor: pointer;">
        <div>
            <a class="selink" href="/tste/?sd=28726585">  
                <span class="date"> 10:15 </span> 
                <span class="titel">THE TITLE<span class="subtitel">some subtitle</span>
                </span>
            </a>
        </div>                              
    </h3>
</div>  
<div class="entries">                               
    <h3 class="headline" style="position: relative; cursor: pointer;">
        <div>
            <a class="selink" href="/tste/?sd=287265995">  
                <span class="date"> 10:16 </span> 
                <span class="titel">THE TITLE 2<span class="subtitel">some subtitle</span>
                </span>
            </a>
        </div>                              
    </h3>
</div>  

PHP

libxml_use_internal_errors(true);
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$doc->strictErrorChecking = false;
$doc->recover = true;
$doc->loadHTMLFile('http://domain.com/startpage.php');
$xpath = new DOMXPath($doc);

$query = "//div[@class='entries']"; // <- QUERY ONE

$entries = $xpath->query($query);
$list = array();

$count = 0;
foreach ($entries as $key => $value)
{        
    $list[$count] = array();

    // get the link <- QUERY TWO
    $list[$count]['url'] =  $xpath->query("//a[@class='selink']");

    // get the title but NOT the subtitle <- QUERY THREE
    $list[$count]['title'] = $xpath->query("//span[@class='titel']");


    $count++;
}


print_r($list);

1 Answer 1

1

$xpath->query($expr) is executed on the whole document each call within the loop because you don't pass the document node the XPath query should be evaluated in relatively.

With the polymorphic method DOMNodeList query(string $expr, DOMNode $node) you can do a sub query relative to the given $node. This method produces the desired result only if you use a relative XPath $expr (without leading /). To retrieve the string from each DOMNode/TextNode finally use the queries as follows:

$list[$count]['url'] = $xpath->query("h3/div/a[@class='selink']/@href", $value)->item(0)->value;
$list[$count]['title'] = $xpath->query("h3/div/a/span[@class='titel']/text()", $value)->item(0)->wholeText;

I edited your CodePad code here.

regards, Max

Sign up to request clarification or add additional context in comments.

2 Comments

I dont get it. Can you please edit my codepad demo? codepad.viper-7.com/ZhMNGw
Sorry, I just realised each query starts with the document root. Instead you should use relative sub queries. I adjusted my answer respectively.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.