map command


map perl-code xpath




This command provides an easy way to modify node's data (content) using arbitrary Perl code.

Each of the nodes matching xpath is passes its data to the perl-code via the $_ variable and receives the (possibly) modified data using the same variable.

Since element nodes do not really have any proper content (they are only a storage for other nodes), node's name (tag) is used in case of elements. Note, however, that recent versions of XSH provide a special command rename with a very similar syntax to map, that should be used for renaming element, attribute, and processing instruction nodes.

Example 1. Capitalises all hobbit names

xsh> map { $_=ucfirst($_) } //hobbit/@name

Example 2. Changes goblins to orcs in all hobbit tales.

xsh> map { s/goblin/orc/gi } //hobbit/tale/text()


Interacting with Perl and Shell, Tree modification