note 85724 modified in class.domelement by danbrown

From: Date: Sun, 14 Sep 2008 16:17:02 +0000
Subject: note 85724 modified in class.domelement by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-144411@lists.php.net to get a copy of this message
I wanted to find similar Elements - thats why I built an Xpath-String like this - maybe somebody needs it... its not very pretty - but neither is domdocument :) <?php $dom->load($xmlFile)) $xpathQuery = '//*'; $xmlNodes = $xpath->query($xpathQuery); $pathlist = array(); $attrlist = array(); foreach ($xmlNodes as $node) { $depth = $this->_getDomDepth($node); //get Path-Depth (for array key) $pathlist[$depth] = $node->tagName; // tagname $attrs = $node->attributes; $attr=''; $a=0; foreach ($attrs as $attrName => $attrNode) // attributes { if ($attrName !='reg') { if ($a++!=0) $attr .= ' and '; $attr .= '@'.$attrName.'='."'".$attrNode->value."'"; } } $attrlist[$depth] = $attr?'['.$attr.']':''; $path = ''; for ($i=0;$i<=$depth;$i++) $path .= '/'.$pathlist[$i].$attrlist[$i]; // the xpath of the actual Element // ... now you can go on and user $path to find similar elements } } } private function _getDomDepth(DomNode $node) { $r = -2; while ($node) { $r++; $node = $node->parentNode; } return $r; } ?> --was-- I wanted to find similar Elements - thats why I built an Xpath-String like this - maybe somebody needs it... its not very pretty - but neither ist domdocument :) $dom->load($xmlFile)) $xpathQuery = '//*'; $xmlNodes = $xpath->query($xpathQuery); $pathlist = array(); $attrlist = array(); foreach ($xmlNodes as $node) { $depth = $this->_getDomDepth($node); //get Path-Depth (for array key) $pathlist[$depth] = $node->tagName; // tagname $attrs = $node->attributes; $attr=''; $a=0; foreach ($attrs as $attrName => $attrNode) // attributes { if ($attrName !='reg') { if ($a++!=0) $attr .= ' and '; $attr .= '@'.$attrName.'='."'".$attrNode->value."'"; } } $attrlist[$depth] = $attr?'['.$attr.']':''; $path = ''; for ($i=0;$i<=$depth;$i++) $path .= '/'.$pathlist[$i].$attrlist[$i]; // the xpath of the actual Element // ... now you can go on and user $path to find similar elements } } } private function _getDomDepth(DomNode $node) { $r = -2; while ($node) { $r++; $node = $node->parentNode; } return $r; } http://php.net/manual/en/class.domelement.php

« previous php.notes (#144411) next »