note 105531 rejected from domxpath.query by salathe
| From: | salathe@php.net | Date: | Thu, 25 Aug 2011 08:45:39 +0000 |
| Subject: | note 105531 rejected from domxpath.query by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-182716@lists.php.net to get a copy of this message | ||
Note Submitter: fclddcn at gmail dot com
----
I'v write this: find all elements that:
1, Is descendant of context element
2, Have 'auto' attribute
3, Is highest level (have no ancestor between self and the context element)
I'v implement it ,But I found the query is slowly, will someone make it faster?
<?php
$xml='
<root>
<a auto="1" tempId="current">
<b>
<c auto="1">
<d auto="1"></d>
</c>
</b>
<e auto="1">
<f>
<g auto="1"></g>
</f>
</e>
</a>
</root> ';
$doc=new DomDocument();
$tempId='XDFAY69LA';
$doc->loadXml($xml);
$domxpath=new DOMXPath($doc);
$a=$domxpath->query('a')->item(0);
$path=".//*[@auto and not(ancestor::*[@auto and
ancestor::*[@tempId='$tempId']])]";
$start=microtime(true);
for($n=0;$n<1000;$n++){ //run 1000 times
$a->setAttribute('tempId',$tempId);
$ar=$domxpath->query($path,$a);
$a->removeAttribute('tempId');
for($i=0;$i<$ar->length;$i++){
$node=$ar->item($i);
//echo $node->tagName . "\n";
}
}
$cost=round(1000 * (microtime(true)-$start));
echo "time cost: $cost";
?>