-1

I'm trying to retrieve the last and second to last entries for the date element in the below example:

<c:classroom>Classroom 1</c:classroom>
    <a:date>2018-03-01</a:date>
    <a:teacher>Ms Smith</a:teacher>
    <a:assistant>Sara</a:assistant>
    <a:student>Rose</a:student>
    <a:student>Kris</a:student>
    <a:student>Jane</a:student>
<c:classroom>Classroom 2</c:classroom>
    <a:date>2018-03-02</a:date>
    <a:teacher>Mr Jones</a:teacher>
    <a:assistant>Bob</a:assistant>
    <a:student>Mike</a:student>
    <a:student>Pat</a:student>
    <a:student>Rick</a:student>
<c:classroom>Classroom 1</c:classroom>
    <a:date>2018-03-05</a:date>
    <a:teacher>Ms Smith</a:teacher>
    <a:assistant>Sara</a:assistant>
    <a:student>Rose</a:student>
    <a:student>Kris</a:student>
    <a:student>Jane</a:student>
<c:classroom>Classroom 2</c:classroom>
    <a:date>2018-03-06</a:date>
    <a:teacher>Mr Jones</a:teacher>
    <a:assistant>Bob</a:assistant>
    <a:student>Mike</a:student>
    <a:student>Pat</a:student>
    <a:student>Rick</a:student>

Using //*[local-name() = 'date'][last()] returns almost everything and using //*[local-name() = 'date'][last()-1] fails.

Any suggestions?

UPDATE: This XPath query is for use in Google Sheets.

Catalyx
  • 435
  • 2
  • 7
  • 17
  • This question should not be a duplicate as the linked references (which are very helpful) had been reviewed and Google Sheets doesn't accept all XQueries. This was originally tagged with [tag:google-spreadsheet] and edited to explicitly state so. Also, it seems neither reference uses a combination of `last()` and `position()` operators as @zx485 used. Happy though to delete if SE still concludes it's a duplicate or doesn't contribute enough to stand alone. – Catalyx Mar 06 '18 at 21:46

1 Answers1

2

Try this XPath

(//*[local-name() = 'date'])[last() - position() &lt; 2]

This should get the last two dates in the document.

zx485
  • 28,498
  • 28
  • 50
  • 59
  • Thanks. The link was an interesting read. – zx485 Mar 05 '18 at 20:22
  • Thanks for this. This question is actually not a duplicate as the references (which are very helpful) had been reviewed and Google Sheets doesn't accept all XQueries; e.g. Google Sheets doesn't seem compatible with `<` though `<` works. Could you point out how to get a line break for between the dates? – Catalyx Mar 06 '18 at 14:37
  • The general approach is to `concat()` a ` ` at the end of each node value. But I don't know what the google way of doing this is. – zx485 Mar 06 '18 at 18:12