I am in a situation where I want to add nodes to an XML document with a default namespace (derived from a markdown document), but every time I do so, an unnamed namespace with the same URI is created, and I end up with several namespaces in my document. If I don't add the xmlns attribute, then the node has no namespace and can't be found using the default namespace prefix.
Is there a way to add nodes to a default namespace without creating aliases?
# d <- commonmark::markdown_xml("test")
d <- '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE document SYSTEM "CommonMark.dtd">
<document xmlns="http://commonmark.org/xml/1.0">
<paragraph>
<text xml:space="preserve">test</text>
</paragraph>
</document>'
dx <- xml2::read_xml(d)
xml2::xml_ns(dx)
#> d1 <-> http://commonmark.org/xml/1.0
xml2::xml_add_child(
dx, "code_block", "# test\n",
xmlns = xml2::xml_ns(dx)[[1]] # using the same namespace as the default
)
xml2::xml_ns(dx)
#> d1 <-> http://commonmark.org/xml/1.0
#> d2 <-> http://commonmark.org/xml/1.0
Thank you for the response. I had a feeling that it would come down to re-reading the document, but happily, it doesn't seem to have a significant increase in the amount of time needed to render the document and oddly reduces the memory footprint (example shown on the {dplyr} NEWS file). The only significant cost appears to be the fact that we now need to re-assign the variable.