It can be necessary to loop through several returned nodes within a node-set as a result of location paths. The for-each statement does this. It instructs the script engine to execute a code block for the first node and then loop back through the code block for every node until the node list is exhausted, at which point the script engine continues past the for-each statement.

The syntax of a for-each loop is similar to an if statement but the contents within its parenthesis consist of a location path or node-set variable rather than a conditional expression:
for-each( $results/physical-interface/mtu ) {
/* looped code */
}

version 1.0;

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";

import "../import/junos.xsl";

match / {
    <op-script-results> {
        /* Send Junos XML API Element via jcs:invoke */
        var $results = jcs:invoke( "get-interface-information" );
        /* Create node list based on location path, loop through each node */
        for-each( $results/physical-interface/mtu ) {
            /* Output the MTU for all interfaces that don’t have Unlimited MTU */
            if( . != "Unlimited" ) {
                <output> "The MTU for " _ ../name _ " is " _ .;
            }
        }
    }
}