简体   繁体   中英

Return blank if the node does not exist in XML using R

I need support in solving this issue:

I want to write a code in R in order to return "It is Sunday today" if the corresponding

node exists otherwise I want to return "".

I am using following code

Man<-unlist(xpathApply(doc,"//div[@class='description']//p[not(@*)]//tt[@class='notranslate']", xmlValue))

I am getting: [1] "It is sunday today"
but I am expecting: [1] "It is sunday today"
                    [2] ""    

Please help me how I can write the R code to get the expected output

Below is my XML:

doc <- (
'<div class="description">
   <p>
    <strong>Advice to Senior Management</strong>
    –
    <tt class="notranslate">It is sunday today</tt>
    </p>
    <p class="nep">
     <strong>No, I would not recommend </strong>
     – I'm not optimistic 
    </p>
  </div>
  <div class="description">
    <p class="nep">
     <strong>No, I would not recommend </strong>
     – I'm not optimistic 
    </p>
  </div>')

You need to get the divs with class description first. You can do this using getNodeSet or shorthand

doc["//div[@class='description']"]

Once you have these nodes you can check the sub nodes for the appropriate xpath

lapply(doc["//div[@class='description']"], function(x){
  xpathSApply(x, ".//p[not(@*)]//tt[@class='notranslate']", xmlValue)
}
)
> lapply(doc["//div[@class='description']"], function(x){xpathSApply(x, ".//p[not(@*)]//tt[@class='notranslate']", xmlValue)})
[[1]]
[1] "It is sunday today"

[[2]]
NULL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM