Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

Announcement: Try the Ask AI chatbot for answers to your technical questions about Juniper products and solutions.

close
header-navigation
keyboard_arrow_up
close
keyboard_arrow_left
Automation Scripting User Guide
Table of Contents Expand all
list Table of Contents
file_download PDF
{ "lLangCode": "en", "lName": "English", "lCountryCode": "us", "transcode": "en_US" }
English
keyboard_arrow_right

for-each

date_range 13-Jan-21

Syntax

content_copy zoom_out_map
for-each (expression) {
    /* code */
}
content_copy zoom_out_map
/* Syntax added in version 1.1 of the SLAX language.*/
for-each (min ... max) {
    /* code */
}

Description

Include a looping mechanism that repeats script processing for each XML element in the specified node set or each value in the integer set.

If the argument is an XPath expression, the element nodes are selected by the value of the XPath expression. 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. For each iteration, the contents are then evaluated, processed according to the instructions contained in the for-each code block.

Attributes

for-each 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.

SLAX Example

The following code iterates over each chassis-sub-module element that has a part-number child element equal to 750-000610. For each match, the script outputs a <message> element with the name of the module and the name and description of the submodule.

content_copy zoom_out_map
for-each ($inventory/chassis/chassis-module/
           chassis-sub-module[part-number == '750-000610']) {
    <message> "Down rev PIC in " _ ../name _ ", " _ name _ ": " _ description;
}

The following code iterates over the integers 1 through 3. 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 the set.

content_copy zoom_out_map
for-each (1 ... 3) {
    <item> {
        attribute "item-number" {
            expr .;
        }
    }
}

/* Output: <item item-number="1"/><item item-number="2"/><item item-number="3"/> */

Release Information

Statement introduced in version 1.0 of the SLAX language.

Support for iteration operator (...) added in version 1.1 of the SLAX language, which is supported in Junos OS Release 12.2 and later releases.

footer-navigation