I am trying to find the last element in my xml, which looks like:
You need to put the last() indexing on the nodelist result, rather than as part of the selection criteria. Try:
last()
(//element[@name='D'])[last()]