sort
Syntax
sort expression {;
case-order "upper-first" | "lower-first";
data-type "text" | "number" | type-name;
order "ascending" | "descending";
}Description
Control the order in which the for-each and apply-templates statements iterate
through the current node list. By default, the for-each and apply-templates statements consider
nodes in document order, but the sort statement
defines the order prior to iterating through the node list. Insert
the sort statement immediately after the for-each or apply-templates statement. The sort statement is only
processed when the loop is first initiated.
The sort statement has an optional
XPath expression and three optional parameters: case-order, data-type, and order. The XPath expression determines each node’s comparison string
used for sorting. The script evaluates the expression with the node
as its context, and then translates the result into the comparison
string for that node. If you do not specify an XPath expression, the
default value is ".", which causes the string content of each node
in the list to be compared. SLAX-specific operators such as == and
_ cannot be used within the expression string. If the sort statement does not include any optional parameters,
the list is sorted based on the string value of each node.
The sort statement does not permanently
sort the underlying XML data structure, only the order of the current
node list being used by the for-each or apply-templates statement. Multiple sort statements can be assigned to a single for-each or apply-templates statement. They are
applied, in order, until a difference is found.
Attributes
expression |
XPath expression that determines each node’s comparison string used for sorting. The default value is ".". |
case-order |
Specify whether to sort lowercase first or uppercase first. Acceptable values are “lower-first” or “upper-first”. The default is “upper-first”. |
data-type |
Specify the element type, which determines whether a numerical, lexical, or other sort is performed. Acceptable values are “number” and “text”. The default is "text". Setting |
order |
Specify whether to sort in ascending or descending order. Acceptable values are “descending” or “ascending”. The default is “ascending”. |
SLAX Example
The following example SLAX script executes the Junos
XML API get-interface-information command
and parses the resulting output. The for-each loop prints the name of each physical interface on the device sorted
in ascending order.
version 1.1;
ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
match / {
<op-script-results> {
var $results = jcs:invoke("get-interface-information");
for-each ($results/physical-interface/name) {
sort . {
data-type "text";
order "ascending";
}
<interface-name> .;
}
}
}Release Information
Statement introduced in version 1.1 of the SLAX language, which is supported in Junos OS Release 12.2 and later releases.