Re: [xsl] How to sort and compare with different element value in XSL

Subject: Re: [xsl] How to sort and compare with different element value in XSL
From: "Heiko Niemann kontakt@xxxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 9 Sep 2016 09:54:09 -0000
I don't know whether I got you right: your expected output assumes you
want all contacts with different Nids and if Nids are the same you want
the latest. If so this should work:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="2.0">
  <xsl:output method="xml" encoding="UTF-8"/>
  <xsl:template match="/objects">
    <xsl:copy>
      <xsl:for-each-group select="Contact" group-by="Nid">
        <xsl:sort select="Nid"/>
        <xsl:sort select="LastModifiedDate" order="descending"/>
        <xsl:copy-of select="."/>
      </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

This also sorts by Nid - just for the heck of it. :)

Heiko



> I have only one XML. and there <Nid> contains same data like 892828740. I
> want get the Id,LastModifiedDate,Nid for Contact based on updated
> LastModifiedDate if <Nid> has more then one same kind of data. Below is my
> Input data, XSL, expected output. But my code is not working fine
>
> Below is XML:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <objects>
>   <Contact>
>     <Id>003j000001DQMkcAAH</Id>
>     <LastModifiedDate>2016-09-08T10:31:24.000Z</LastModifiedDate>
>     <Nid>892828740</Nid>
>   </Contact>
>   <Contact>
>     <Id>003j000001DQMlXAAX</Id>
>     <LastModifiedDate>2016-09-08T10:22:47.000Z</LastModifiedDate>
>     <Nid>879284114</Nid>
>   </Contact>
>   <Contact>
>     <Id>003j000001DQMlYAAX</Id>
>     <LastModifiedDate>2016-09-08T10:22:47.000Z</LastModifiedDate>
>     <Nid>882692370</Nid>
>   </Contact>
>   <Contact>
>     <Id>003j000001DQMlZAAX</Id>
>     <LastModifiedDate>2016-09-08T10:22:47.000Z</LastModifiedDate>
>     <Nid>892828740</Nid>
>   </Contact>
>  </objects>
>
>
>
> Below in XSL:
>
> <xsl:stylesheet version="2.0" xmlns:xsl="
> http://www.w3.org/1999/XSL/Transform";>
>     <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
>     <xsl:template match="objects">
>         <xsl:if test="count(//Contact/Nid[(. =
> ../following-sibling::Contact/Nid)])">
>             <Nids>
>
>                 <Nid>
>                     <Id>
>                         <xsl:value-of
>                             select="//Contact/Nid[(. =
> ../following-sibling::Contact/Nid)]"/>
>                     </Id>
>                     <LastModifiedDate>
>                         <xsl:value-of select="//LastModifiedDate"/>
>                     </LastModifiedDate>
>                     <Nid/>
>                 </Nid>
>
>             </Nids>
>         </xsl:if>
>     </xsl:template>
> </xsl:stylesheet>
>
>
>
>
> Expected output:
>
> <objects>
>   <Contact>
>     <Id>003j000001DQMkcAAH</Id>
>     <LastModifiedDate>2016-09-08T10:31:24.000Z</LastModifiedDate>
>     <Nid>892828740</Nid>
>   </Contact>
>   <Contact>
>     <Id>003j000001DQMlXAAX</Id>
>     <LastModifiedDate>2016-09-08T10:22:47.000Z</LastModifiedDate>
>     <Nid>879284114</Nid>
>   </Contact>
>   <Contact>
>     <Id>003j000001DQMlYAAX</Id>
>     <LastModifiedDate>2016-09-08T10:22:47.000Z</LastModifiedDate>
>     <Nid>882692370</Nid>
>   </Contact>
> </objects>

Current Thread