note 92304 added to domelement.getattribute

From: Date: Sat, 18 Jul 2009 05:25:18 +0000
Subject: note 92304 added to domelement.getattribute
Groups: php.notes 
Request: Send a blank email to php-notes+get-158452@lists.php.net to get a copy of this message
I want to retrieve value of name in my current language, or the default, or anything set if none of the above are true. There is a sample XML file: <product> <name lang="en">Candy</name> <name lang="fr">Bonbon</name> <name>Bonbon (no lang)</name> </product> There is my function, hope this can help. And if anybody want's to improve it, your welcom. The input elements are DOMNodeList returned from a previous DomXPath Query. <?php private function getValueByLang($elements) { $a = array(); if (!is_null($elements)) { foreach ($elements as $element) { if( !is_null($element->attributes)) { $is_lang = false; foreach ($element->attributes as $attrName => $attrNode) { if( 'lang' == $attrName ) { $a[$attrNode->value] = $element->nodeValue; $is_lang = true; } } if( !$is_lang ) { $a[] = $element->nodeValue; } } } // Returns current language if set. if( array_key_exists($this->lang, $a)) { $result = $a[$this->lang]; // Returns default language if set. } elseif( array_key_exists($this->default_lang, $a)) { $result = $a[$this->default_lang]; // Returns first result with no language attribute. } elseif( isset($a[0])) { $result = $a[0]; // Return anything set. } elseif( $result = reset($a) ) { // Return nothing, Doh! } else { $result = false; } return($result); } else { throw new Exception(printf("No elements defined.")); } } ?> ---- Server IP: 72.55.140.6 Probable Submitter: 70.25.152.223 ---- Manual Page -- http://www.php.net/manual/en/domelement.getattribute.php Edit -- https://master.php.net/note/edit/92304 Del: integrated -- https://master.php.net/note/delete/92304/integrated Del: useless -- https://master.php.net/note/delete/92304/useless Del: bad code -- https://master.php.net/note/delete/92304/bad+code Del: spam -- https://master.php.net/note/delete/92304/spam Del: non-english -- https://master.php.net/note/delete/92304/non-english Del: in docs -- https://master.php.net/note/delete/92304/in+docs Del: other reasons-- https://master.php.net/note/delete/92304 Reject -- https://master.php.net/note/reject/92304 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#158452) next »