Jump to content

xpath stuff


blinks

Recommended Posts

The following code is throwing up a "Cannot use object of type DOMElement as array" error on the $node[@ID >0] line

 

$fieldNodeList = $xpath->query("/name");
$fieldNodeList2 = $xpath->query("/name/affiliation");
$newString = "hello";
foreach($fieldNodeList as $node) {
if ($node[@ID > 0]) {
	$fieldNodeList2->nodeValue = $newString;
}	
}

 

What this is meant to do is change /name/affiliation if the attribute value of /name is greater than 0.

 

Any suggestions, please??

Link to comment
https://forums.phpfreaks.com/topic/192324-xpath-stuff/
Share on other sites

Got it figured out -

$fieldNodeList = $xpath->query("/name");
$fieldNodeList2 = $xpath->query("/name/affiliation");
$newString = "hello";

foreach($fieldNodeList as $node) {
                if ($node->getAttribute("ID") > "0") {
                                $fieldNodeList2->item(0)->nodeValue = $newString;
                }              
}

Link to comment
https://forums.phpfreaks.com/topic/192324-xpath-stuff/#findComment-1013491
Share on other sites

Are you sure that that really does what you want? With that code, only the first /name/affiliation node will have its value set to hello regardless of how many name elements there are.

 

Would it be right to assume that you wish to change the affiliation value for all of the name nodes having an @id greater then zero?

Link to comment
https://forums.phpfreaks.com/topic/192324-xpath-stuff/#findComment-1013592
Share on other sites

Hi salathe, you are right in supposing I want to change the affiliation for all name nodes having an @id greater than zero. And yes, this code does do what I want. There is a foreach loop before the code, which I didn't include.

 

There is only ever one affiliation per name, and as the code is for a one-off global update, it doesn't have to cater for future possibilities.

 

Thanks for your reply :-)

Link to comment
https://forums.phpfreaks.com/topic/192324-xpath-stuff/#findComment-1014022
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.