Search code examples
rxml2

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?


Solution

  • 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>