note 49511 deleted from function.dom-domxpath-query by didou
| From: | didou@php.net | Date: | Sat, 02 Apr 2005 22:50:10 +0000 |
| Subject: | note 49511 deleted from function.dom-domxpath-query by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-87379@lists.php.net to get a copy of this message | ||
Note Submitter: Sébastien
----
Simple example about DOMXPath::query( )...
countries.xml
======================================
<?xml version="1.0"?>
<countries>
<country id="1">
<name>Italy</name>
<continent>Europe</continent>
</country>
<country id="2">
<name>Australy</name>
<continent>Oceania</continent>
</country>
<country id="3">
<name>France</name>
<continent>Europe</continent>
</country>
<country id="4">
<name>Japan</name>
<continent>Asia</continent>
</country>
</countries>
======================================
index.php
======================================
<?php
$countries = DOMDocument::load('countries.xml') ;
$xpath = new DOMXPath($countries) ;
$list = $xpath->query('//country[continent="Europe"]') ;
/*
XPath query get european countries
$list is a DOMNodeList object
You can use foreach( ) with DOMNodeList objects
*/
echo "<p>Countries found :
<strong>$list->length</strong></p>" ;
echo '<ul>' ;
foreach ( $list as $country ) {
printf('<li>%s (%d)</li>',
$country->getElementsByTagName('name')->item(0)->nodeValue,
$country->getAttribute('id')) ;
}
echo '</ul>' ;
?>
======================================
Will output :
======================================
Countries found : 2
* Italy (1)
* France (3)
======================================