for-each
Syntax
for-each (expression) {
/* code */
}
for-each (min ... max) {
/* code */
}/* Syntax with optional parentheses added in SLAX version 1.3 */
for-each expression {
/* code */
}
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 value of the XPath
expression selects the element nodes. 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. For each
iteration, the contents are evaluated, processed according to the instructions
contained in the for-each 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. |
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 module's name and the
submodule's name and description.
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. Each <item>
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
Release Information
Statement introduced in SLAX version 1.0.
Support for iteration operator (...) added in SLAX version 1.1.
Support for syntax with optional parentheses added in SLAX version 1.3.