2

I have a XML file which I want to replace a value with a constant when it is empty, checking an attribute (annotation_ref). The XML tags are something like this:

 <ANNOTATION> <REF_ANNOTATION ANNOTATION_ID="id" ANNOTATION_REF="1234"> <ANNOTATION_VALUE></ANNOTATION_VALUE> </REF_ANNOTATION> </ANNOTATION> 

So, the result of the transformation would be:

 <ANNOTATION> <REF_ANNOTATION ANNOTATION_ID="id" ANNOTATION_REF="1234"> <ANNOTATION_VALUE>my_constant</ANNOTATION_VALUE> </REF_ANNOTATION> </ANNOTATION> 

But instead of that, I get this:

 <ANNOTATION> <REF_ANNOTATION ANNOTATION_ID="id" ANNOTATION_REF="1234"> <ANNOTATION_VALUE/> </REF_ANNOTATION> <ANNOTATION_VALUE>my_constant</ANNOTATION_VALUE></ANNOTATION> 

My code is the next:

$document = simplexml_load_file("my_document.eaf"); $aux = $annotation_document->ANNOTATION; foreach ($aux as $aux2) { if ($aux2->REF_ANNOTATION->attributes()->ANNOTATION_REF == $my_condition) { $aux2->ANNOTATION_VALUE = $my_constant; } } 

Thank you so much.

1 Answer 1

1

You're talking to the wrong node:

if ($aux2->REF_ANNOTATION->attributes()->ANNOTATION_REF == $my_condition) { ^^^^^^^^^^^^^^^^^^ $aux2->ANNOTATION_VALUE = $my_constant; ^^^^^^^^^^^^ 

You should have

$aux2->REF_ANNOTATION->ANNOTATION_VALUE = $my_constant; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.