Open Gootjes opened 4 years ago
I can reproduce the behavior you describe, but I don't really have time to look into if there is a obvious way to fix it. I would suggest not using .copy = FALSE
I came to this repo to report the same problem :sweat_smile:
xml <- xml2::read_xml(
"<document>
<a>blop</a><b>blip</b><b>paf</b>
</document>
"
)
xml
#> {xml_document}
#> <document>
#> [1] <a>blop</a>
#> [2] <b>blip</b>
#> [3] <b>paf</b>
a <- xml2::xml_find_first(xml, ".//a")
xml2::xml_add_parent(a, "parent")
parent <- xml2::xml_find_first(xml, ".//parent")
b <- xml2::xml_find_all(xml, ".//b")
xml2::xml_add_child(parent, b[[1]], .copy = FALSE)
# The node with 'blip' is in two places.
xml
#> {xml_document}
#> <document>
#> [1] <parent>\n <a>blop</a>\n <b>blip</b>\n <b>paf</b>\n</parent>
#> [2] <b>blip</b>
#> [3] <b>paf</b>
Created on 2022-05-19 by the reprex package (v2.0.1)
As the documentation of
xml_add_child
suggests, setting.copy = FALSE
moves the element to its new location. However, it does not remove the node from its original location, leaving a zombied node that cannot be removed.Case 1
Case 2
Whenever namespaces are involved,
xml_find_all
creates an error, I guess it produces an infinite loop somewhere.