1

I need to retrieve the index of specific nodes from an XML document. The task is similiar to the one here (Find position of a node using xpath), but I have trouble applying the suggested solutions to the "more than one requested child node" scenario. Consider the following example XML document

<a>
<b>zyx</b>
<b>wvu</b>
<b>tsr</b>
<b>wvu</b>
</a> 

I want to retrieve the index for the b nodes with value "wvu" relative to a, i.e. I want to yield a vector [2,4]. Can this be done with XPath 1.0? Specifically I am using the XML package in R.

/Edit

Here is an Example document:

library(XML)
file1 <- htmlParse("<a><b>zyx</b><b>wvu</b><b>tsr</b><b>wvu</b></a> ")
xpathSApply(file1, "count(a/b[.='wvu']/preceding-sibling::*)+1")
[1] 1
Community
  • 1
  • 1
user3393472
  • 1,151
  • 1
  • 8
  • 12

1 Answers1

1

I don't know of a way to do this in a single XPATH, but you could break up the node finding and the index calculation into two separate logical steps:

sapply(getNodeSet(file1, "//a/b[.='wvu']"), 
    xpathSApply, "count(./preceding-sibling::*)+1")
# [1] 2 4
MrFlick
  • 195,160
  • 17
  • 277
  • 295