Logo

DOMXPath()

ops...
Thanks to: No one :(

Description

Creates an XPath queryable class, from an HTML files DOMDocument. XPath allows to to query data in HTML.

Usage: new DOMXPath($htmlDom);
 
// Creates a new XPath class from a DOMDocument
$xpath = new \DOMXPath($dom);
 
// The '//' prefix searching for a pattern at all levels of the document 
$xpath->evaluate('//div[@class="match-program__row"]')
 
// The '/' tells the search pattern to look at the beginning of the entire document 
$xpath->evaluate('/html/body/div[@class="match-program__row"]')
 
// The '.' tells it to search only within a given context 
$fights = $xpath->evaluate('//div[@class="match-program__row"]');
 
foreach ($fights as $fight) {
    $element = $xpath->evaluate('.//div[@class="match-program__row__team"]', $fight)->item(0)->textContent;
    echo $element;
}
 
// XPath will always return a list of elements. So even in a case where you know there will only be one item, you still have to specify that you want the first item
// Like in the below example:
$date = $xpath->evaluate('.//div[@class="match-program__row__date"]', $fight)->item(0)->textContent;
 
// The below example will look for divs with the EXACT class structure and will even take into account spaces
$date = $xpath->evaluate('//div[@class="match-program__row__date "]');
 
// So looking for class contains instead is a better idea. However, if the class has a trailing space, it will also count that as part of the class
$fights = $xpath->evaluate('//div[contains(@class, "match-program__row ")]');
 
// Note that if you get the the HTML from a website/file that creates its elements from JavaScript, those elements will not be included 
foreach ($fights as $fight) {
    $element = $xpath->evaluate('.//div[@class="match-program__row__team"]', $fight)->item(0)->textContent;
    echo $element;
}