Navigation
Guide That Contains This Content
[+] Expand All
[-] Collapse All

    for-each

    Syntax

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

    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.

    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.

    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.

    for-each (1 ... 3) {
        <item> {
            attribute "item-number" {
                expr .;
            }
        }
    }
    
    /* Output: <item item-number="1"/><item item-number="2"/><item item-number="3"/> */

    Usage Examples

    Published: 2013-03-05