0

Hi,

I need to do some sorting of the XML file. First, group the group_1 first and then sort those group_1 block by SORT_ID and Salary. Second, group the group_2 and then sort those group_2 block by SORT_ID.

I just try the below and it works if the XML only have group_1..........
***********************************************************
<xsl:template match="allgroup">

<xsl:apply-templates>
<xsl:sort select="SORT_ID" data-type="number"/>
<xsl:sort select="salary" data-type="number"/>
</xsl:apply-templates>

</xsl:template>


Input XML
************************************************************
<allgroup>

<group_1 id="1">
<last>Hill</last>
<first>Phil</first>
<salary>100000</salary>
<SORT_ID>1</SORT_ID>
<hireDate>04/23/1999</hireDate>
</group_1>

<group_1 id="2">
<last>Herbert</last>
<first>Johnny</first>
<salary>95000</salary>
<SORT_ID>2</SORT_ID>
<hireDate>09/01/1998</hireDate>
</group_1>

<group_2 id="5">
<last>Hill</last>
<first>Phil</first>
<salary>100000</salary>
<SORT_ID>1</SORT_ID>
<hireDate>04/23/1999</hireDate>
<role>M</role>
</group_2>

<group_1 id="3">
<last>Tony</last>
<first>Graham</first>
<salary>89000</salary>
<SORT_ID>1</SORT_ID>
<hireDate>08/20/2000</hireDate>
</group_1>

<group_1 id="4">
<last>Jack</last>
<first>Johnny</first>
<salary>100000</salary>
<SORT_ID>2</SORT_ID>
<hireDate>09/01/1998</hireDate>
</group_1>

</allgroup>

Output XML
************************************************************
<?xml version="1.0" encoding="UTF-16"?>
<allgroup>
<group_1>
<last>Tony</last><first>Graham</first><salary>89000</salary><hireDate>08/20/2000</hireDate><SORT_ID>1</SORT_ID>
</group_1>
<group_1>
<last>Hill</last><first>Phil</first><salary>100000</salary><hireDate>04/23/1999</hireDate><SORT_ID>1</SORT_ID>
</group_1>
<group_1>
<last>Herbert</last><first>Johnny</first><salary>95000</salary><hireDate>09/01/1998</hireDate><SORT_ID>2</SORT_ID>
</group_1>
<group_1>
<last>Jack</last><first>Johnny</first><salary>100000</salary><hireDate>09/01/1998</hireDate><SORT_ID>2</SORT_ID>
</group_1>

<group_2>
<role>M</role>
</group_2>
<allgroup>

Any suggestion?
Thanks.

AOTONY

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by fpmurphy
0

You were nearly there. Try the following stylesheet on your sample document

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text"/>

<xsl:template match="text()" />

<xsl:template match="allgroup">
   <xsl:apply-templates>
       <xsl:sort select="name()" />
       <xsl:sort select="SORT_ID" data-type="number"/>
       <xsl:sort select="salary" data-type="number" order="descending"/>
   </xsl:apply-templates>
</xsl:template>

<xsl:template match="last" >
   GROUP: <xsl:value-of select="name(..)"/> SORT_ID: <xsl:value-of select="../SORT_ID"/> SAL: <xsl:value-of select="../s
alary" /> NAME: <xsl:value-of select="." />, <xsl:value-of select="../first" />
</xsl:template>

</xsl:stylesheet>

which will output

GROUP: group_1 SORT_ID: 1 SAL: 100000 NAME: Hill, Phil
  GROUP: group_1 SORT_ID: 1 SAL: 89000 NAME: Tony, Graham
  GROUP: group_1 SORT_ID: 2 SAL: 100000 NAME: Jack, Johnny
  GROUP: group_1 SORT_ID: 2 SAL: 95000 NAME: Herbert, Johnny
  GROUP: group_2 SORT_ID: 1 SAL: 100000 NAME: Hill, Phil
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.