MashZone NextGen 10.2 | Appendix | Legacy Presto components | Mashables and Mashups | Mashups in EMML | Writing Mashups in EMML | Transforming Intermediate Results | <sort> | <sort> Examples
 
<sort> Examples
<sort> Basics
Sorting variables is somewhat like filtering variables with a little more information:
*The input variable that should be sorted
*The specific nodes to be sorted are identified by a relative XPath expression.
*The nodes that are the key for sorting are also identified by a relative XPath expression. By default, sorting treats key values as strings. You can also Sort Numbers or Dates and Sort With Multiple Keys.
*The direction of sorting (ascending is the default). You can also control the direction individually when you use multiple keys. See Sort Directions for Multiple Keys for information.
*The output variable to hold the sorted results. This can be another variable, or just update the input variable.
For example:

<variables>
<variable name="queryResult" type="document"/>
<variable name="westCoastOnly" type="document"/>
</variables>
...
<sort inputvariable="$westCoastOnly" sortdir="descending"
sortexpr="/customers" sortkeys="salesperson"
outputvariable="$westCoastOnly"/>
...
Note: The sortexpr is relative to the input variable and sortkeys is relative to sortexpr.
Sort Numbers or Dates
To sort numbers or dates you must explicitly cast the datatype of the sort keys to a numeric or date datatype using an XPath function. Typically, you use the constructor functions in XPath that correspond to specific datatypes in XML Schema, such as xs:decimal or xs:date.
Important: You must use xs as the namespace for XPath datatype constructor functions. You do not have to declare this namespace because it is built-in.
For example:

...
<sort inputvariable="$salesResults"
sortexpr="region/categories/category"
sortkeys="xs:decimal(periodSales)"
outputvariable="$highPeriodSales"/>
...
<sort inputvariable="$troubleTickets"
sortexpr="ticket"
sortkeys="xs:date(created)"
outputvariable="$troubleTickets"/>
Note: The constructor functions work only if the content of the sort key nodes is a valid lexical representation of that datatype. Dates, for example, must be expressed in a specific syntax. You can use other XPath functions to manipulate sort key content to obtain the correct lexical representation.
See XPath 2.0 Data Model Types for a complete list of constructor functions.
Sort With Multiple Keys
To sort with multiple keys, you add XPath expressions to the sortkeys attribute, separated by commas. The sort keys are used in the order that you specify them. This example sorts category nodes by category name and then by sales for each period within category:

...
<sort inputvariable="$salesResults"
sortexpr="region/categories/category"
sortkeys="@name, xs:decimal(periodSales)"
outputvariable="$categoryPeriodSales"/>
...
Sort Directions for Multiple Keys
You can control the sort direction for individual sort keys when you use multiple keys by specifying the sort direction after each key. This overrides the value of the sortdir attribute.
For this example, tickets are sorted by creation date in descending order (most recent first) and then by customer name in ascending order:

...
<sort inputvariable="$troubleTickets"
sortexpr="ticket"
sortkeys="xs:date(created) descending, customer ascending"
outputvariable="$troubleTickets"/>
...
Working Samples
The following sample mashups use the <sort> statement: rsssort, sort, sortDate
*SortSample (sort.emml) for the basics
*SortDates (sortdate.emml)
*YahooJobsSortAndFilterSample (rsssort.emml) for a filter and sort example
See Mashup Samples for a list of MashZone NextGen mashup samples and where to find them.

Copyright © 2013-2018 | Software AG, Darmstadt, Germany and/or Software AG USA, Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.
Innovation Release