XPATH select second item (not child)

Viewed 27

I was trying to get only the first element:

<xml>
  <title>xpath.playground.fontoxml.com</title>
  <summary>This is a learning tool for XML, XPath and XQuery.</summary>
  <tips>
    <tip id='1' name='hello'>You can edit everything on the left</tip>
    <tip id='examples'>You can access more examples from a menu in the top right</tip>
    <tip id='permalink'>Another button there lets you share your test using an URL</tip>
  </tips>
  <tips>
    <tip id='2' name='hello'>You can edit everything on the left</tip>
    <tip id='examples'>You can access more examples from a menu in the top right</tip>
    <tip id='permalink'>Another button there lets you share your test using an URL</tip>
  </tips>
</xml>

This is the XPATH, im trying to get one element from all with name='hello'

//*[@name='hello'][1]

but it returns both <tip id='1' name='hello'> and <tip id='2' name='hello'>

my expectation was //*[@name='hello'][1] returns the element with id=1 and //*[@name='hello'][2] returns the element with id=2

how do I get this result

1 Answers

Please try this:

(//*[@name='hello'])[2]
Related