Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/167501-solved-domdocument-and-domnodelist/
Share on other sites

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']"));
}
?>

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.