XPath last appearance of each element

I have XML as

<root>
    <a>One</a>
    <a>Two</a>
    <b>Three</b>
    <c>Four</c>
    <a>Five</a>
    <b>
        <a>Six</a>
    </b>
</root>

and you need to select the last occurrence of any child name node in the root directory. In this case, the desired summary list would be:

<c>Four</c>
<a>Five</a>
<b>
    <a>Six</a>
</b>

Any help is appreciated!

+5
source share
4 answers

XSLT Solution:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="root/*">
        <xsl:variable name="n" select="name()"/>
        <xsl:copy-of
            select=".[not(following-sibling::node()[name()=$n])]"/>
    </xsl:template>
</xsl:stylesheet>

Output Produced:

<c>Four</c>
<a>Five</a>
<b>
   <a>Six</a>
</b>

The second solution (you can use it as a single XPath expression):

<xsl:template match="/root">
    <xsl:copy-of select="a[not(./following-sibling::a)]
        | b[not(./following-sibling::b)]
        | c[not(./following-sibling::c)]"/>
</xsl:template>
+3
source

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>
+6

XPath 2.0,

/root//*[not(name() = following-sibling::*/name())]
+4

XSLT 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:strip-space elements="*" />

    <xsl:template match="/root">
        <xsl:for-each-group select="*" group-by="name()">
            <!-- <xsl:sort select="index-of(/root/*, current-group()[last()])" order="ascending"/> -->
            <xsl:copy-of select="current-group()[last()]" />
        </xsl:for-each-group>
    </xsl:template>
</xsl:stylesheet>

:

<a>Five</a>
<b>
  <a>Six</a>
</b>
<c>Four</c>

, <xsl:sort>!

0

All Articles