0

My sample input XML is:

<root>
    <a>
    <b>
    <c>item1</c>
    <d>item2</d>
    <e>item3</e> 
    </b>
    </a>
    <a>
    <b>
    <c>item4</c>
    <c>item5</c>
    <e>item6</e>   
    </b>        
    </a>
    </root>

I am suppose to select all the first occurrence of node c. The output should be item1 and item4. when I am using /root/a/b/c it returns item1,item4 and item5.

M Zippka
  • 93
  • 13
  • Try `/root/a/b/c[0]`. Otherwise show some more of what you're trying (e.g. code). – Thomas Mar 10 '17 at 09:19
  • 3
    Possible duplicate of [What is the XPath expression to find only the first occurrence?](http://stackoverflow.com/questions/14294997/what-is-the-xpath-expression-to-find-only-the-first-occurrence) – M Zippka Mar 10 '17 at 09:35
  • where tag `b` is closing? – Vitaliy Mar 10 '17 at 09:47
  • http://stackoverflow.com/questions/14294997/what-is-the-xpath-expression-to-find-only-the-first-occurrence here only the first element is selected.I want to select all the first occurrence of node c. – ranjit singh Mar 10 '17 at 10:43

2 Answers2

1

I used this for your example //root//c[1] and it's working as you expect. enter image description here

Incepter
  • 2,711
  • 15
  • 33
0
/root/a/b/c[1]

For more detailed explanation see: https://stackoverflow.com/a/5818966/36305

Community
  • 1
  • 1
M Zippka
  • 93
  • 13