Search code examples
pythonxmlxpathscrapyxpath-1.0

Use index on after filter on xpath


I have this following xml fragment.

<root>
  <site type="main">
    <link>http://stackexchange.com</link>
  </site>
  <site type="qa">
    <link>http://stackoverflow.com</link>
  </site>
  <site type="qa">
    <link>http://superuser.com</link>
  </site>
  <site type="">
    <link>http://data.stackexchange.com</link>
  </site>
</root>

I want to select the first site's link of type "qa". I think //site[@type="qa"][1] should do it. But id does not work.


Solution

  • Give a try to (//site[@type="qa"])[1]/link/text().

    Or, alternatively:

    //site[@type="qa"][position()=1]/link/text()
    

    Tested on http://www.xpathtester.com/, results into http://stackoverflow.com.