blinks Posted February 17, 2010 Share Posted February 17, 2010 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?? Quote Link to comment https://forums.phpfreaks.com/topic/192324-xpath-stuff/ Share on other sites More sharing options...
blinks Posted February 17, 2010 Author Share Posted February 17, 2010 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/192324-xpath-stuff/#findComment-1013491 Share on other sites More sharing options...
salathe Posted February 17, 2010 Share Posted February 17, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/192324-xpath-stuff/#findComment-1013592 Share on other sites More sharing options...
blinks Posted February 17, 2010 Author Share Posted February 17, 2010 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 :-) Quote Link to comment https://forums.phpfreaks.com/topic/192324-xpath-stuff/#findComment-1014022 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.