0

In a related post, How to select specified node within Xpath node sets by index with Selenium?, it is mentioned that there is "no index i in xpath". I am trying to use an index in an R loop within an XPath expression such as

getNodeSet(xmlfile, '//first[i]/second/third')

Clearly, according to the above post it works perfectly when replacing 'i' with '1', but not e.g. for i <- 1. However, the workaround in the above post (i.e. using ['+i+']) does not seem to work. Any ideas on how to make indices work in XPath expressions?

Community
  • 1
  • 1

1 Answers1

2

'//first[i]/second/third' is just a string. Therefore you can use the R string building function paste0() to make your own (R doesn't use + for string concatenation).

getNodeSet(xmlfile, paste0('//first[', i, ']/second/third'))
MrFlick
  • 195,160
  • 17
  • 277
  • 295