spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

PHP - digging farther down in a Xpath query


Try this :

$url =
'http://www.craigslist.org/about/sites';
$output = file_get_contents($url);
$doc = new DOMDocument();

libxml_use_internal_errors(true); //Supress
Warnings for HTML5 conversion issue
$doc->loadHTML($output);
libxml_use_internal_errors(false); //Start Showing
Errors

$xpath = new DOMXpath($doc);

foreach ($xpath->query('//h1') as $e) {
    $country = trim($e->textContent);

    foreach
($xpath->query('following-sibling::div[1]//h4',
$e) as $h4) {
        $state = trim($h4->textContent);

        foreach
($xpath->query('following-sibling::ul[1]//li/a',
$h4) as $a) {
                $town = $a->textContent;
                $attributeNodeMap =
$a->attributes;
                $nodeAttribute =
$attributeNodeMap->getNamedItem("href");
                $href =
trim($nodeAttribute->nodeValue);

                echo "$country - $state - $town -
$href<br>";
        }
    }
}

EDIT

So that's how I did it.
First of all I'm using firefox with firebug and firepath (i guess you can find similar tools for other web browser).
This tools let me try some Xpath without writing PHP code.

With firebug you can see the DOM tree which is really useful to know what you can reach, ... and then try Xpath with firepath

To start i selected all H1 nodes //h1 in the document and then you need to get all H4 for each H1 to get state but unfortunately H4 node is not a child of H1 node, so you need to find another way to reach it if you want to start from H1 node.

If you look at DOM tree you will see that a div (which contains H4 node) is one of the next sibling of H1 node, so let's select it following-sibling::div[1] (this is the div <div class="colmask"> for the current h1 node only).
We want all H4 nodes //h4 then we've got following-sibling::div[1]//h4

Now we do the same thing for the <a href...> for each H4, so we select all A nodes in all LI nodes which are in the next sibling UL of H4 following-sibling::ul[1]//li/a

I hope this is understandable (and useful of course) and sorry for the mistakes, English is not my language.


Categories : PHP

Related to : PHP - digging farther down in a Xpath query
Is there a way to ignore case when performing an xpath query
One option is to use XPath translate to convert uppercase letters to lowercase. In that case your expression would look like: var xpath = '//*[translate(name(), "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz") = "filter"]'; There are some nicer XPath 2.0 methods like matches and similar, but no browser currently supports it: http://en.wikipedia.org/wiki/Comparison_of_layout_engines

Categories : Javascript
how to split xml using xpath in perl?
The reason you're having difficult is because what you're doing is extracting a subset from an XML doc, but then trying to also include some of the stuff from the 'parent'. Pulling your 'delts' out would be fairly straightforward I would be wanting to use XML::Twig with this - this is a perfect place to use a twig handler. I'd be thinking something along the lines of (and apologies, this do

Categories : Xml
Get sibling variable value from XPath - PHP
Yes, using the getAttribute() method: $currency = $xpath->query('//*[@itemprop="priceCurrency"]')->item(0); $content = $currency->getAttribute('content'); echo $content; // prints "GBP"

Categories : PHP
Separating values from XML using php with xpath
You could just point to b first, then inside the loop, each b, point it to c and d: $result = $xml->xpath('//b'); foreach ($result as $val) { echo $val->c . $val->d . "<br>"; }

Categories : PHP
Extract value from xpath array
Use the ->item() method on the result to access the DOM nodes that your XPath query returns. Also you should rather use the query() method when you want to run an XPath query. I've never really understood the purpose of using evaluate(). $xpath = new DOMXPath($dom); $results = $xpath->query('//*[@itemprop="price"]'); echo $results->item(0)->nodeValue; DOMXPath->query() returns an

Categories : PHP
Recently Add
Cartesian product of associative array showing key and value - PHP
Datamapper orm save and update not working
find methods in COM object
PHP Keyword search not working with multiple words
Set a total based on items ordered, two types, and sometimes one type will not be ordered.
FB request to join a group, going around their official SDK
Using Google charts with php data?
OAuth2 integration with ExactOnline
libvirt-php receives the error: unable to connect to server Permission denied
How to echo results by sets of 2?
Why does PHP's sprintf not round 5s reliably?
php replace affects on replaced string
PHP Find and replace multiple similar entries
Replace PHP date output with pre-made images
How to install Laravel 4 packages on Windows
Using updateOrCreate for multiple data insert in Laravel
Storing products in a MYSQL database and then search and filter them?
Create order with PrestaShop's API
how to get latest messages from all users
Yii CMultiFileUpload Restrict Image width
Combine array by key factor
Semicolon after if condition in PHP - code still works
Get date using day of the week
soap web service with symfony
Seasonal Reservation system Day/month
is it possible to join a single table it self?
Attempting to compare two arrays PHP
Sticky Select Option
Eventbrite duplicate event entry
Pagination Not Working on custom post type in Wordpress
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.