Vebut Posted July 26, 2009 Share Posted July 26, 2009 Hello, I'm working on a selector class and would like to select elements and nodes based on attributes and values. I know how to retrieve the nodes and everything is working as it should, but I'm stuck at the point where I'd like a new DOMNodeList to work with: <?php /** * Select node/element by attribute and its value. * * @param string $attr * @param string $value * @return object */ public function elementsByAttr ($attr, $value) { $this->nodes = $this->getElementsByTagName('*'); foreach ($this->nodes as $id => $node) { if ($node->getAttribute($attr) == $value) { // New Dom node list. } } return $this; } ?> Is there a way to manually create a DOMNodeList or alter the existing one in $this->nodes (by removing the nodes that does not match the criteria)? Thanks, Daniel Quote Link to comment https://forums.phpfreaks.com/topic/167501-solved-domdocument-and-domnodelist/ Share on other sites More sharing options...
Vebut Posted July 26, 2009 Author Share Posted July 26, 2009 Ok, instead of limiting me to the domdocument functions I included the DOMXpath class, solution: <?php /** * Select node/element by attribute and its value. * * @param string $attr * @param string $value * @return bool|object */ public function elementsByAttr ($attr, $value) { $xpath = new DOMXPath($this); return $this->nodes($xpath->query("//*[@$attr='$value']")); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/167501-solved-domdocument-and-domnodelist/#findComment-883337 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.