Marko Ivanović
Marko Ivanović

Reputation: 23

R & XML2: Remove XML attributes

I have an XML document in variable doc. xml_attr(doc, "attr") for example prints out the value 200 from its attribute attr="200".

xml_set_attr(doc, "attr", "") does remove the value, but I want to remove the attr attribute from the tag so the document looks like:

<tag></tag> instead of

<tag attr></tag> or

<tag attr=""></tag>


Does xml2 have a function for this?

Upvotes: 2

Views: 998

Answers (1)

lukeA
lukeA

Reputation: 54237

Try assigning NULL to remove the attribute:

library(xml2)
(doc <- read_xml("<tag value='200'></tag>"))
# {xml_document}
# <tag value="200">
xml_set_attr(doc, "value", NULL)
doc
# {xml_document}
# <tag>

Upvotes: 1

Related Questions