Sorting multiple field values from Input XML to top in the Output XML

Viewed 39

Sorting the same field values from xml to top and rest field values to bottom.

this is my input to xslt:

<?xml version='1.0' encoding='UTF-8'?>
<feed>
    <entry>
        <name>David</name>
        <updated>AA123</updated>
        <title>BB123</title>
    </entry>
    <entry>
        <name>John</name>
        <updated>AA123</updated>
        <title>AA123</title>
    </entry>
    <entry>
        <name>Jenny</name>
        <updated>CC789</updated>
        <title>TT789</title>
    </entry>
    <entry>
        <name>Dan</name>
        <updated>CC456</updated>
        <title>HH456</title>
    </entry>
    <entry>
        <name>Steve</name>
        <updated>CC456</updated>
        <title>CC456</title>
    </entry>
    <entry>
        <name>Jenny</name>
        <updated>AB456</updated>
        <title>DD789</title>
    </entry>
</feed>

Expected Output:

<?xml version='1.0' encoding='UTF-8'?>
        <feed>
        <entry>
            <name>John</name>
            <updated>AA123</updated>
            <title>AA123</title>
        </entry>
        <entry>
            <name>Steve</name>
            <updated>CC456</updated>
            <title>CC456</title>
        </entry>
        <entry>
            <name>David</name>
            <updated>AA123</updated>
            <title>BB123</title>
        </entry>
        <entry>
            <name>Dan</name>
            <updated>CC456</updated>
            <title>HH456</title>
        </entry>
        <entry>
            <name>Jenny</name>
            <updated>AB456</updated>
            <title>DD789</title>
        </entry>
        <entry>
            <name>Jenny</name>
            <updated>CC789</updated>
            <title>TT789</title>
        </entry>
    </feed>

Below XSLT is not SORTING in correct way:

    <xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/feed">
  <Customer>
    <xsl:for-each-group select="entry" group-by="updated">
      <xsl:for-each-group select="current-group()" group-by="title">
      <xsl:copy-of select="current-group()"/>
      <xsl:apply-templates>
                <xsl:sort select="updated"/>
                <xsl:sort select="title"/>
      </xsl:apply-templates>
      </xsl:for-each-group>
    </xsl:for-each-group>
  </Customer>
</xsl:template>
</xsl:stylesheet>

I have a requirement to SORT same FIELD values of field name "updated" and "title". If both the field values are same then it should come first in the OUTPUT XML and then later the different field value.

1 Answers

If I understand correctly, you want to do:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/feed">
    <feed>
        <xsl:perform-sort select="entry">
            <xsl:sort select="number(updated = title)" data-type="number" order="descending"/>
        </xsl:perform-sort> 
    </feed>
</xsl:template>

</xsl:stylesheet>
Related