note 49511 added to function.dom-domxpath-query
| From: | S�bastien at osu1 dot php dot net | Date: | Sun, 30 Jan 2005 19:26:41 +0000 |
| Subject: | note 49511 added to function.dom-domxpath-query | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-84247@lists.php.net to get a copy of this message | ||
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)
======================================
----
Manual Page -- http://www.php.net/manual/en/function.dom-domxpath-query.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+49511
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49511&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49511&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49511&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49511&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49511&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+49511&report=yes
Search -- http://master.php.net/manage/user-notes.php