Interstage Shunsaku Data Manager Application Development Guide - Microsoft(R) Windows(R) 2000/ Microsoft(R) Windows Server(TM) 2003 - - UNIX - |
Contents
Index
![]() ![]() |
Part 1 The Basic for Developing Application | > Chapter 3 Data Search Methods | > 3.5 Sorting Search Results |
It is possible to sort search results using a character string in any element nodes of an XML document or the character string in the attribute nodes of those element nodes. The size relationship between character strings refers to the size relationship between the character code values of those strings. To use a character string to sort search results, a key specification in the sort expression must be defined using either a text expression, attribute expression or the rlen single-line function.
Refer to B.5.1 Sort Expression Format for more information on key specifications.
The explanation that follows assumes that the following documents exist:
Document A
<doc> : <basic date="2006/01/22"> : </basic> <detail> : <destination area="Clayton, Richmond, Melbourne">Melbourne Office</destination> : <train>$135.00</train> : </detail> : </doc> |
Document B
<doc> : <basic date="2006/02/03"> : </basic> <detail> : <destination area="Manly, Balmain, Darlinghurst, Sydney">Sydney branch</destination> : <train>$38.00</train> : </detail> : </doc> |
Document C
<doc> : <basic date="2006/01/13"> : </basic> <detail> : <destination area="Ashgrove, Ascot, Brisbane">Brisbane branch</destination> : <train>$75.00</train> : </detail> : </doc> |
Document D
<doc> : <basic date=""> : </basic> <detail> : <destination area="Griffith, Kingston, Deakin, Canberra">Canberra branch</destination> : <train>$21.00</train> : </detail> : </doc> |
Example 1
The following example shows how to sort documents from the cheapest (ascending order) transportation fee (train).
/doc/detail/train/text()Result: Documents are returned in the order A, D, B, C.
Example 2
The following example shows how to sort documents in ascending order according to the trip date (date):
/doc/basic/@dateResult: Documents are returned in the order C, A, B, D.
To sort character codes in descending order, specify 'DESC' after the key specification.
Example 3
The following example shows how to sort documents from the most expensive (descending order) transportation fee (train).
/doc/detail/train/text() DESCResult: Documents are returned in the order C, B, D, A.
Example 4
The following example shows how to sort documents in descending order according to the trip date (date):
/doc/basic/@date DESCResult: Documents are returned in the order B, A, C, D.
If an XML document does not contain the text node specified in the text expression, that XML document will be returned last, regardless of whether a "DESC" specification is in place. In the above example, Document D would be returned last in both an ascending sort and a descending sort.
When a sort is performed using a character string, the first 20 bytes of that string are used as the sort key. Therefore, if the length of the character string specified as the key is greater than 20 bytes, bytes 21 onwards will not become part of the key, and the XML documents will not be sorted correctly. In such cases, the rlen function can be used in the key specification to determine how many characters from the start of the string will be used as the sort key.
Refer to B.2.4 Single-line Function Specification for more information on the rlen function.
Example 5
The following example shows how to sort documents according to the trip destination (destination):
rlen(/doc/detail/destination/text(),10)When the above is specified, the first 10 characters of the trip destination will be used as the sort key.
Example 6
The following example shows how to sort documents according to the trip area (area):
rlen(/doc/detail/destination/@area,30)When the above is specified, the first 30 characters of the trip area will be used as the sort key.
Contents
Index
![]() ![]() |