Jump to content

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

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.