foreach command


foreach xpath|perl-code command|command-block




If the first argument is an xpath expression, execute the command-block for each node matching the expression making it temporarily the current node, so that all relative XPath expressions are evaluated in its context.

If the first argument is a perl-code, it is evaluated and the resulting perl-list is iterated setting the variable $__ (note that there are two underscores!) to be each element of the list in turn. It works much like perl's foreach, except that the variable used consists of two underscores.

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

xsh> foreach //company xmove ./employee into ./staff;

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

xsh> foreach { glob('*.xml') } { open f=$__; list f:/; }


Flow control