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
Created on 2020-10-16 by the reprex package (v0.3.0)