Reading the XML manipulation vignette from xml2, and a side by side comparison to the XML package, it seems to me xml2 doesn't have a function to copy and XML document or nodeset. I think XML got the xmlClone function, though I haven't used it. It means, xml2 requires a workaround. So far I found out this:
library(xml2)
x <- read_xml("<foo>
<bar>text <baz id = 'a' /></bar>
<bar>2</bar>
<baz id = 'b' />
</foo>")
x_copy <- xml_unserialize(xml_serialize(x, NULL))
I am wondering if really this is the most straightforward solution, or I am missing something? Also, isn't this too inefficient for large documents?