ConFoo Montreal 2026: Call for Papers

Voting

: max(one, zero)?
(Example: nine)

The Note You're Voting On

Severin
16 years ago
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;
}
?>

<< Back to user notes page

To Top