foreach command


Usage

foreach expression command|command-or-block

foreach [my|local] $variable in expression command|command-or-block

Aliases

for

Description

Evaluate given expression to a node-list and for each resulting node execute given command or command-or-block. If used without a loop $variable, the loop temporarily sets current node to the node being processed. Otherwise, the processed node is assigned to the loop variable.

The expression may be xpath as well as a perl-code. In the latter case, if used without a loop variable, the loop automatically converts Perl objects to nodes. No conversion is performed when a loop variable is used.

Example 1. Move all employee sub-elements in a company element into the first staff subelement of the same company

xsh> foreach //company xmove employee into staff[1];

Example 2. List content of all XML files in current directory

xsh> foreach my $filename in { glob('*.xml') } {
  $f := open $filename;
  do_something $f; 
}

Sections

Flow control