18 May 2010

Sorting data using xsl:sort

In SOA 11g, there is a new XSL-Construct for sorting the input nodes. This is a basic XSLT construct (element) which was missing earlier.
As the w3 standard defines, this construct cannot be used independently, but can be used along with xsl:for-each(or xsl:apply-templates)
It is used to sort the input data based upon some id or text(called as sort-key).
This can be very useful functionality in scenarios where one needs to process some data in a specific sequence.

To use this construct inside a transform activity, add a for-each construct first. Do the required mapping.
Then right-click the for-each and select sort




A dialogue box will open. Add the details like sort-order, data-type for sorting. Click OK.




The construct still shows a big red balloon. To complete the things, just map the appropriate source element to this construct.
Note that one sort construct adds only one parameter. To have sort on basis of more than one parameter, additional sort constructs need to be added.
The source code will show entry of the sort similar to :

<xsl:sort select="tns:age" order="descending" data-type="number"/>

Test and verify.  In the example below, I have added two sort statements first for age (descending) and then for id (ascending) :





Note that xsl:sort supports 5 attributes which are :
1. select : Specifies the string which can be used as sort-key
2. lang : Specifies the language for the sort key
3. data-key : Specifies the data type to be used while sorting. Possible values are text, number or a qualified name (either a prefixed name or unprefixed name)
4. order : Specifies the order of sorting. Possible values are ascending, descending
5. case-order : Used in case of data-key="text". Possible values are lower-first or upper-first

1 comment:

Anonymous said...

Useful info. thanks. saved me from my bad a** DBA. now i can sort as i need it :)