First improve the markup to get rid of internal syntax, e.g.
<ply player="white" piece="P" from="d2" to="d4"/>
Write a recursive template which walks through all the moves. Pass a parameter which contains the current position of each of the 32 pieces. Update this parameter to reflect the new position of a piece after the move. Copy the move, adding a new attribute that identifies which of the 32 pieces is being moved. (You'll have to do something special for castling, promotion, etc).
Now you've got data that's tagged with the identity of the piece making the move, it's a simple matter of (/game/move/ply)[@piece=$x][last()].
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference