Search code examples
rxml2

generate function calls to modify xml with R


I would like to dynamically modify an XML document using R and xml2: my approach would be to have parameters ready into a dataframe, and to just pass these parameters to a function that would modify the xml document.

library(xml2)

doc <- xml2::read_xml('<CATALOG>
<PLANT myid="1">
  <COMMON>Bloodroot</COMMON>
  <BOTANICAL>Sanguinaria canadensis</BOTANICAL>
  <ZONE>4</ZONE>
  <LIGHT>Mostly Shady</LIGHT>
  <PRICE>$2.44</PRICE>
  <AVAILABILITY>031599</AVAILABILITY>
  </PLANT>
  <PLANT myid="2">
  <COMMON>Columbine</COMMON>
  <BOTANICAL>Aquilegia canadensis</BOTANICAL>
  <ZONE>3</ZONE>
  <LIGHT>Mostly Shady</LIGHT>
  <PRICE>$9.37</PRICE>
  <AVAILABILITY>030699</AVAILABILITY>
  </PLANT>
  <PLANT myid="3">
  <COMMON>Marsh Marigold</COMMON>
  <BOTANICAL>Caltha palustris</BOTANICAL>
  <ZONE>4</ZONE>
  <LIGHT>Mostly Sunny</LIGHT>
  <PRICE>$6.81</PRICE>
  <AVAILABILITY>051799</AVAILABILITY>
  </PLANT>
  <PLANT myid="4">
  <COMMON>Cowslip</COMMON>
  <BOTANICAL>Caltha palustris</BOTANICAL>
  <ZONE>4</ZONE>
  <LIGHT>Mostly Shady</LIGHT>
  <PRICE>$9.90</PRICE>
  <AVAILABILITY>030699</AVAILABILITY>
  </PLANT>
  <PLANT myid="5">
  <COMMON>Dutchman\'s-Breeches</COMMON>
  <BOTANICAL>Dicentra cucullaria</BOTANICAL>
  <ZONE>3</ZONE>
  <LIGHT>Mostly Shady</LIGHT>
  <PRICE>$6.44</PRICE>
  <AVAILABILITY>012099</AVAILABILITY>
  </PLANT>
  <PLANT myid="6">
  <COMMON>Ginger, Wild</COMMON>
  <BOTANICAL>Asarum canadense</BOTANICAL>
  <ZONE>3</ZONE>
  <LIGHT>Mostly Shady</LIGHT>
  <PRICE>$9.03</PRICE>
  <AVAILABILITY>041899</AVAILABILITY>
  </PLANT>
  <PLANT myid="7">
  <COMMON>Hepatica</COMMON>
  <BOTANICAL>Hepatica americana</BOTANICAL>
  <ZONE>4</ZONE>
  <LIGHT>Mostly Shady</LIGHT>
  <PRICE>$4.45</PRICE>
  <AVAILABILITY>012699</AVAILABILITY>
  </PLANT>
  </CATALOG>'

)


xpath <-  "//PLANT[@myid='2']"
xml_find_all(doc,xpath)

newxml <- function(xmldoc, xpath, newattr) {

  #
  VarName <- xml2::xml_find_all(xmldoc, xpath)
  xml_attr(VarName, "Name") <- newattr

}

Now if I want to modify this XML document made of thousands of plants, base on the id (I can store xpath and new value in a dataframe or vector), how should I proceed ?

I tried the following:

do.call(newxml,list(xmldoc = doc, xpath= dftest$xpath, 
                       newattr = dftest$newname))

But it is not working, it's telling me :

 Error in xpath_search(x$node, x$doc, xpath = xpath, nsMap = ns, num_results = Inf) : 
  Expecting a single string value: [type=character; extent=5]. 

Obvously there is something big I miss about R, but I can't figure what, please also feel free to propose a better approach to this problem.


Solution

  • It looks like the issue is passing your dataframe columns in as the function arguments.

    If I pass in singular arguments the function seems to provide the result:

    xpath <-  "//PLANT[@myid='2']"
    xml_find_all(doc,xpath)
    newxml <- function(xmldoc, xpath, newattr) {
    
      #
      VarName <- xml2::xml_find_all(xmldoc, xpath)
      xml_attr(VarName, "Name") <- newattr
    
    }
    do.call(newxml,list(xmldoc = doc, xpath= xpath, 
                        newattr = "TESTING"))
    

    Produces below:

     xml_find_all(doc,xpath)
    {xml_nodeset (1)}
    [1] <PLANT myid="2" Name="TESTING">\n  <COMMON>Columbine</COMMON>\n
    

    I think also possibly your dtypes in the data frame are factors not strings

    I used the below:

    newxml <- function(xpath, newattr,xmldoc=doc) {
      VarName <- xml2::xml_find_all(xmldoc, as.character(xpath))
      xml_attr(VarName, "Name") <- as.character(newattr)
    }
    
    df <- data.frame(a= c(xpath,xpath),b = c("TEST1","TEST2"))
    mapply(newxml,df$a, df$b)
    

    use multiple columns as variables with sapply