Both the XPath 2.0 solution and the currently accepted answer are very inefficient (O (N ^ 2)).
This solution has sublinear complexity:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kElemsByName" match="/*/*"
use="name()"/>
<xsl:template match="/">
<xsl:copy-of select=
"/*/*[generate-id()
=
generate-id(key('kElemsByName', name())[last()])
]"/>
</xsl:template>
</xsl:stylesheet>
when applied to the provided XML document :
<root>
<a>One</a>
<a>Two</a>
<b>Three</b>
<c>Four</c>
<a>Five</a>
<b>
<a>Six</a>
</b>
</root>
required, the correct result is obtained :
<c>Four</c>
<a>Five</a>
<b>
<a>Six</a>
</b>
. Muenchian grouping - , . node .
II XPath 2.0 :
:
/*/*[index-of(/*/*/name(), name())[last()]]
XSLT 2.0 XPath 2.0:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:sequence select=
"/*/*[index-of(/*/*/name(), name())[last()]]"/>
</xsl:template>
</xsl:stylesheet>
XML ( ), :
<c>Four</c>
<a>Five</a>
<b>
<a>Six</a>
</b>