note 103084 added to domelement.getattribute
| From: | metron at underhive-planet dot com | Date: | Thu, 24 Mar 2011 16:45:55 +0000 |
| Subject: | note 103084 added to domelement.getattribute | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-178171@lists.php.net to get a copy of this message | ||
You can also use DOMXPath objects to interleave foreach() in this way is slightly shorter. If you
need a special element or attribute value. The variable $xquery needs xpath expression. This example
filters all group elements as described in xpath query ($xquery), then it filters again the results
by last if condition and stores all attribute names in an object variable of type array
($this->menu).
Benefit: Using class array variable $menu in other class methods. Whenever you need those fields.
XML:
<xml>
<category menu="products">
<groups>
<group menu="vegetables">...</group>
</groups>
</category>
</xml>
public class example {
public $menu;
function _construct(){}
public function getGroupValueByCategory($xpath_object,$category) {
$xquery =
'//category[normalize-space(@menu)="'.$category.'"]/groups/group';
if($elements = $xpobject->evaluate($xquery)){
$this->menu = array();
}
foreach ($elements as $element) {
if($element->hasAttributes()){
$attributes = $element->attributes;
if(!is_null($attributes)){
foreach ($attributes as $index=>$attr){
if($attr->name == 'menu'){
$this->menu[] = $attr->value;
}
}
}
}
}
}
}
I used this to implement simple menu structure.
----
Server IP: 69.147.83.197
Probable Submitter: 92.231.189.196
----
Manual Page -- http://www.php.net/manual/en/domelement.getattribute.php
Edit -- https://master.php.net/note/edit/103084
Del: integrated -- https://master.php.net/note/delete/103084/integrated
Del: useless -- https://master.php.net/note/delete/103084/useless
Del: bad code -- https://master.php.net/note/delete/103084/bad+code
Del: spam -- https://master.php.net/note/delete/103084/spam
Del: non-english -- https://master.php.net/note/delete/103084/non-english
Del: in docs -- https://master.php.net/note/delete/103084/in+docs
Del: other reasons-- https://master.php.net/note/delete/103084
Reject -- https://master.php.net/note/reject/103084
Search -- https://master.php.net/manage/user-notes.php