First, we identify if the attribute exists in nodes-of-interest
library(xml2)
x <- read_xml("<root ATTR_A="word" ATTR_B="test" ATTR_C="number"> <child id ='a'/> <child id='b' d='b'/> </root>")
First, we identify whether the ATTR_Z="index" attribute exists on the "root" node. If there is no add the new attribute between ATTR_B="test" and ATTR_C="test" ELSE keep xml .
I Tried:
x <- xml_attr(x, ATTR_Z=,.after = ATTR_B ) <- "index"
It gives an error because the .after( ) does not exis. Any idea how to resolve this?
Expected Output:
<root ATTR_A="word" ATTR_B="test" ATTR_Z="index" ATTR_C="number"> <child id ='a'/> <child id='b' d='b'/> </root>