Supported Platforms
Related Documentation
- ACX, EX, M, MX, PTX, SRX, T Series
- SLAX Statements Overview
- XPath Overview
- for-each
for
Syntax
for name (expression) {
/* code */
}
for name (min ... max) {
/* code */
}
Release Information
Statement introduced in version 1.1 of the SLAX language, which is supported in Junos OS Release 12.2 and later releases.
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 numbers are generated in decreasing order. The variable takes on the value of each integer in sequence. For each iteration, the contents are then evaluated, processed according to the instructions contained in the for code block.
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 for loop variable, which takes on the values of each member of the integer or node set. This variable can be referenced within the for loop code block. |
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"/> */
Related Documentation
- ACX, EX, M, MX, PTX, SRX, T Series
- SLAX Statements Overview
- XPath Overview
- for-each
Published: 2013-07-26
Supported Platforms
Related Documentation
- ACX, EX, M, MX, PTX, SRX, T Series
- SLAX Statements Overview
- XPath Overview
- for-each