for
Syntax
for name (expression) {
/* code */
}
for name (min ... max) {
/* code *//* Syntax with optional parentheses added in SLAX version 1.3 */
for name in expression {
/* code */
}
for name in min ... max {
/* code */Description
Iterate through an integer set or a node set without changing the context, and execute a block of statements using each member of the integer or node set as the value of the given variable.
If the argument is an XPath expression, the variable is assigned each member of the
node set selected by the expression in sequence. If the argument is an integer set,
the iteration operator (...) generates a sequence of nodes with the value of each
integer between the left and right operands. If the left operand is greater than the
right operand, the operator generates the numbers in decreasing order. The variable
takes on the value of each integer in sequence. For each iteration, the contents are
evaluated, processed according to the instructions contained in the
for code block.
Starting in SLAX version 1.3, the parentheses enclosing the statement expression or
range are optional. When you omit the parentheses, you must include the keyword
in before the expression or range.
Attributes
expression |
XPath expression that selects the nodes to be processed. |
max |
Integer or variable that defines the end value of the integer sequence. If the end value is less than the start value, the numbers are generated in decreasing order. |
min |
Integer or variable that defines the starting value of the integer sequence. If the start value is greater than the end value, the numbers are generated in decreasing order. |
name |
Identifier of the |
SLAX Example
In the following example, the for loop iterates over the
interfaces node. The XPath expression selects each
name node that is a child of the interface
node and that has a value beginning with the 'ge-' designator. The selection is
assigned to the $name variable, which is used within that iteration
of the for loop code block. The for loop outputs a
<name> element for each selection. The content of each
<name> element is the interface name currently stored in
the $name variable for that iteration. The end result is a list of
all Gigabit Ethernet interfaces on the device.
for $name (interfaces/interface[starts-with(name, 'ge-')]) {
<name> {
expr $name;
}
}
In the following example, the for loop iterates over the integers 1
through 3, and the variable $int assumes each integer value. For
each iteration, the code block generates an <item> element,
which contains the attribute item-number with a value equal to the
current integer value of $int.
for $int (1 ... 3) {
<item> {
attribute "item-number" {
expr $int;
}
}
}
/* Output:
<item item-number="1"/>
<item item-number="2"/>
<item item-number="3"/> */The following example is similar to the previous one, but the for
statement uses the syntax without parentheses.
for $int in 4 ... 6 {
<item> {
attribute "item-number" {
expr $int;
}
}
}
/* Output:
<item item-number="4"/>
<item item-number="5"/>
<item item-number="6"/> */Release Information
Statement introduced in SLAX version 1.1.
Support for syntax with optional parentheses added in SLAX version 1.3.