<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN" "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd">
<article>
  <title>XSH2 Reference</title>
  <section id="intro">
    <title>XSH2 Language</title>
  
      <para>
	XSH2 acts as a command interpreter. Individual commands must
	be separated with a semicolon. 
	In the interactive shell, backslash may be used at the end of a line to
	indicate that a command continues on the next line.
	Output redirection can be used to pipe output of some XSH <link linkend="command">command</link> to some external program, or to capture the output
	to a variable. See <link linkend="Redirection">Redirection</link> for more info.
      </para>
      <para>
	XSH2 command <link linkend="help_command">help</link> provides a complete
	reference, instantly from the command-line:
      </para>
      <para>
	<literal>help command</literal>
	gives a list of all XSH2 <link linkend="command">commands</link>.
      </para>
      <para>
	<literal>help type</literal> gives a
	list of all argument types.
      </para>
      <para>
	<literal>help topic</literal> followed by
	documentation chapter gives more information on a given topic.
      </para>
      <para>
	<literal>help toc</literal> displays the table of contents.
      </para>
    <section id="Documents">
      <title>Files/Documents</title>
      <para>
	XSH2 is designed as an environment for querying and manipulating XML and
	HTML documents. Use <link linkend="open_command">open</link> or <link linkend="create_command">create</link> commands to load an XML or HTML
	document from a local file, external URL (such as http:// or ftp://),
	string or pipe. XSH2 can optionally validate the document during parse
	process (see <link linkend="validation">validation</link> and
	<link linkend="load_ext_dtd">load-ext-dtd</link>). Parsed documents are stored in memory
	as <ulink url="http://www.w3.org/DOM/">DOM</ulink> trees, that can be
	<link linkend="Navigation">navigated</link> and <link linkend="Manipulation">manipulated</link> with XSH2 commands and XPath
	language, whose names and syntax make working with
	the DOM tree a flavor of working in a UNIX filesystem.
      </para>
      <para>
	A parsed document is usually stored in a variable. 
	XSH2 shares variables
	with the XPath engine, so if e.g. <literal>$doc</literal> is a XSH2
	variable holding a document (or, more generally any node-set), then
	<literal>$doc//section/title</literal> is an
	XPath expression selecting all <literal>title</literal> 
	subelements of all
	<literal>section</literal> elements within the (sub)tree of $doc.
      </para>
      <para>
	Although XSH2 is able to parse remote documents via
	<literal>http://</literal> or <literal>ftp://</literal>, it is only
	able to save them locally.  To upload a document to a remote server
	(e.g. using FTP) or to store it into a database, use <link linkend="save_command">save</link> command with a <literal>--pipe</literal>
	parameter, in connection with an external program able to store
	its standard input (XML) to the desired location. 
	You can also use similar
	parameter with <link linkend="open_command">open</link> in order to parse
	documents from standard output of some external program.
      </para>
      <example>
	<title>Store a XSH2 document 
	  on a remote machine using the Secure Shell</title>
	<programlisting>xsh&gt; <userinput>save --pipe "ssh my.remote.org 'cat &gt; test.xml'" $doc</userinput></programlisting>
      </example>
    <section>
  <title>Related topics</title>
  <para>
    <variablelist termlength="23">    <varlistentry>
      <term><link linkend="backups">backups</link></term>
      <listitem><para>turn on backup file creation</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="catalog_command">catalog</link></term>
      <listitem><para>use a catalog file during all parsing processes</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="cdonopen">switch-to-new-documents</link></term>
      <listitem><para>set on/off changing current document to newly
	  open/created files</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="clone_command">clone</link></term>
      <listitem><para>clone a given document</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="close_command">close</link></term>
      <listitem><para>close document (without saving)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="create_command">create</link></term>
      <listitem><para>make a new document from a given XML fragment</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="document">document</link></term>
      <listitem><para>specifying documents</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="filename">filename</link></term>
      <listitem><para>specifying filenames</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="files_command">documents</link></term>
      <listitem><para>display a list of open documents</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="index_command">index</link></term>
      <listitem><para>index a static document for faster XPath lookup</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="nobackups">nobackups</link></term>
      <listitem><para>turn off backup file creation</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="nodename">nodename</link></term>
      <listitem><para>specifying names of DOM nodes</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="open_command">open</link></term>
      <listitem><para>load an XML, HTML, or Docbook SGML document from a
	file, pipe or URI</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="process_xinclude_command">process-xinclude</link></term>
      <listitem><para>load and insert XInclude sections</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="save_command">save</link></term>
      <listitem><para>save a document as XML or HTML</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="set_filename_command">set_filename</link></term>
      <listitem><para>change filename or URL associated with a document</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="stream_process_command">stream</link></term>
      <listitem><para>process selected elements from an XML stream (EXPERIMENTAL)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="subname">subroutine</link></term>
      <listitem><para>name of a sub-routine</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="varname">$variable</link></term>
      <listitem><para/></listitem>
    </varlistentry></variablelist>
  </para>
 </section></section><section id="Navigation">
      <title>Tree navigation</title>
      <para>
	With XSH2, it is possible to browse a <link linkend="Documents">document tree</link> 
	(XML data represented as a DOM-tree) as if it was a local
        filesystem, except that <link linkend="xpath">XPath</link> 
	expressions are used instead of ordinary directory paths.
      </para> 
      <para>
	To mimic the filesystem navigation as closely as
	possible, XSH2 contains several commands named by analogy of
	UNIX filesystem commands, such as <link linkend="chxpath_command">cd</link>, <link linkend="list_command">ls</link> and
	<link linkend="pwd_command">pwd</link>.	
      </para>
      <para>The current position in the document tree
	is called the <emphasis>current node</emphasis>. Current node's XPath
	may be queried with <link linkend="pwd_command">pwd</link> command.  In the interactive shell, current
	node is also displayed in the command line prompt. 
	(Since there may be
	multiple document trees open at the same time, XSH2 tries to locate a
	variable holding the current document and use it to fully qualify
	current node's XPath in the XSH2 prompt.) Remember, that beside <link linkend="chxpath_command">cd</link> command, current node (and document) is
	also silently changed by <link linkend="open_command">open</link> command, <link linkend="create_command">create</link> command and temporarily also by the
	node-list variant of the <link linkend="foreach">foreach</link> loop without
	a loop variable.
      </para>
      <para>XPath expressions are always evaluated in context
	of the current node. Different documents can be accessed
	through variables: <literal>$doc/foo[1]/bar</literal>.</para>
      <example>
	<title>XSH2 shell</title>
	<programlisting>$scratch:/&gt; <userinput>$docA := open "testA.xml"</userinput>
$docA/&gt; <userinput>$docB := open "testB.xml"</userinput>
$docB/&gt; <userinput>pwd</userinput>
/
$docB/&gt; <userinput>cd $docA/article/chapter[title='Conclusion']</userinput>
$docA/article/chapter[5]&gt; <userinput>pwd</userinput>
/article/chapter[5]
$docA/article/chapter[5]&gt; <userinput>cd previous-sibling::chapter</userinput>
$docA/article/chapter[4]&gt; <userinput>cd ..</userinput>
$docA/article&gt; <userinput>cd $docB</userinput>
$docB:/&gt; <userinput>ls</userinput>
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;article&gt;...&lt;/article&gt;
</programlisting>
      </example>
    <section>
  <title>Related topics</title>
  <para>
    <variablelist termlength="19">    <varlistentry>
      <term><link linkend="c14n_command">canonical</link></term>
      <listitem><para>serialize nodes as to canonical XML</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="chxpath_command">cd</link></term>
      <listitem><para>change current context node</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="fold_command">fold</link></term>
      <listitem><para>mark elements to be folded by list
	command</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="list_command">ls</link></term>
      <listitem><para>list a given part of a document as XML</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="locate_command">locate</link></term>
      <listitem><para>show a given node location (as a canonical XPath)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="pwd_command">pwd</link></term>
      <listitem><para>show current context node location (as a canonical XPath)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="registerfunc_command">register-function</link></term>
      <listitem><para>define XPath extension function (EXPERIMENTAL)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="unfold_command">unfold</link></term>
      <listitem><para>unfold elements folded with fold command</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="unregisterfunc_command">unregister-function</link></term>
      <listitem><para>undefine extension function (EXPERIMENTAL)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="xpath">xpath</link></term>
      <listitem><para>XPath expression</para></listitem>
    </varlistentry></variablelist>
  </para>
 </section></section><section id="Manipulation">
      <title>Tree modification</title>
      <para>
	XSH2 not only provides ways to browse and inspect the DOM tree but also
	many commands to modify its content by various operations,
	such as copying,
	moving, and deleting its nodes as well as creating completely new nodes
	or XML fragments and attaching them to it. It is quite easy to learn
	these commands since their names or aliases mimic their well-known
	filesystem analogies.  On the other hand, many of these commands have
	two versions one of which is prefixed with a letter "x". This
	"x" stands for "cross", thus e.g. <link linkend="xcopy_command">xcopy</link> should be read as "cross copy".
	Let's explain the difference on the example of <link linkend="xcopy_command">xcopy</link>.</para>
      <para>
	In a copy operation, you have to specify what nodes are to be
	copied and where to, in other words, you have to specify the
	<emphasis>source</emphasis> and the
	<emphasis>target</emphasis>.  XSH2 is very much XPath-based so,
	XPath is used here to specify both of them. However, there
	might be more than one node that satisfies an XPath expression. So,
	the rule of thumb is that the "cross" variant of a
	command places <emphasis>one and every</emphasis> of the source nodes
	to the location of <emphasis>one and every</emphasis> destination
	node, while the plain variant works one-by-one, placing the
	first source node to the first destination, the second source
	node to the second destination, and so on (as long as there
	are both source nodes and destinations left).
      </para>
      <informalexample>
	<programlisting>$scratch/&gt; <userinput>$a := create "&lt;X&gt;&lt;A/&gt;&lt;Y/&gt;&lt;A/&gt;&lt;/X&gt;";</userinput>
$a/&gt; <userinput>$b := create "&lt;X&gt;&lt;B/&gt;&lt;C/&gt;&lt;B/&gt;&lt;C/&gt;&lt;B/&gt;&lt;/X&gt;";</userinput>
$b/&gt; <userinput>xcopy $a//A replace $b//B;</userinput>
$b/&gt; <userinput>copy $b//C before $a//A;</userinput>
$b/&gt; <userinput>ls $a;</userinput>
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;X&gt;&lt;C/&gt;&lt;A/&gt;&lt;Y/&gt;&lt;C/&gt;&lt;A/&gt;&lt;/X&gt;

$b/&gt; <userinput>ls $b;</userinput>
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;X&gt;&lt;A/&gt;&lt;A/&gt;&lt;C/&gt;&lt;A/&gt;&lt;A/&gt;&lt;C/&gt;&lt;A/&gt;&lt;A/&gt;&lt;/X&gt;
</programlisting>
      </informalexample>
      <para>
	As already indicated by the example, another issue of tree modification
	is the way in which the destination node determines the target
	location. Should the source node be placed before, after, or somewhere
	among the children of the resulting node? Or maybe, should it replace
	it completely? This information has to be given in the
	<link linkend="loc">location</link> argument that usually precedes the destination
	XPath.
      </para>
      <para>
	Now, what happens if source and destination nodes are of
	incompatible types? XSH2 tries to avoid this by implicitly
	converting between node types when necessary.  For example, if
	a text, comment, and attribute node is copied into, before or
	after an attribute node, the original value of the
	attribute is replaced, prepended or appended respectively with
	the textual content of the source node. Note however, that
	<emphasis>element nodes are never converted</emphasis> into
	text, attribute or any other textual node.  There
	are many combinations here, so try yourself and see the
	results.
      </para>
      <para>You may even use some more sophisticated way to convert
	between node types, as shown in the following example, where
	an element is first commented out and than again uncommented.
	Note, that the particular approach used for resurrecting the
	commented XML material works only for well-balanced chunks of
	XML.
      </para>
      <example>
	<title>Using string variables to convert between different
	  types of nodes</title>
	<programlisting><userinput>$doc := create &lt;&lt;EOF;
&lt;?xml version='1.0'?&gt;
&lt;book&gt;
  &lt;chapter&gt;
    &lt;title&gt;Intro&lt;/title&gt;
  &lt;/chapter&gt;
  &lt;chapter&gt;
    &lt;title&gt;Rest&lt;/title&gt;
  &lt;/chapter&gt;
&lt;/book&gt;
EOF
</userinput>

# comment out the first chapter
<userinput>ls //chapter[1] |&gt; $chapter_xml;</userinput>
<userinput>insert comment $chapter_xml replace //chapter[1];</userinput>
<userinput>ls / 0;</userinput>
# OUTPUT:
&lt;?xml version="1.0"?&gt;
&lt;book&gt;
&lt;!--  &lt;chapter&gt;
    &lt;title&gt;Intro&lt;/title&gt;
  &lt;/chapter&gt;
--&gt;
  &lt;chapter&gt;
    &lt;title&gt;Rest&lt;/title&gt;
  &lt;/chapter&gt;
&lt;/book&gt;


# un-comment the chapter
<userinput>$comment = string(//comment()[1]);</userinput>
<userinput>insert chunk $comment replace //comment()[1];</userinput>
<userinput>ls / 0;</userinput>
# OUTPUT:
&lt;?xml version="1.0"?&gt;
&lt;book&gt;
  &lt;chapter&gt;
    &lt;title&gt;Intro&lt;/title&gt;
  &lt;/chapter&gt;

  &lt;chapter&gt;
    &lt;title&gt;Rest&lt;/title&gt;
  &lt;/chapter&gt;
&lt;/book&gt;
</programlisting>
      </example>
    <section>
  <title>Related topics</title>
  <para>
    <variablelist termlength="16">    <varlistentry>
      <term><link linkend="change_namespace_prefix_command">change-ns-prefix</link></term>
      <listitem><para>change namespace prefix (EXPERIMENTAL)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="change_namespace_uri_command">change-ns-uri</link></term>
      <listitem><para>change namespace URI (EXPERIMENTAL)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="clone_command">clone</link></term>
      <listitem><para>clone a given document</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="copy_command">copy</link></term>
      <listitem><para>copy nodes (in the one-to-one mode)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="declare_namespace_command">declare-ns</link></term>
      <listitem><para>create a special attribute declaring an XML namespace (EXPERIMENTAL)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="edit_command">edit</link></term>
      <listitem><para>Edit parts of a XML document in a text editor.</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="edit_string_command">edit-string</link></term>
      <listitem><para>Edit a string or variable in a text editor.</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="exp">expression</link></term>
      <listitem><para>expression argument type</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="hash_command">hash</link></term>
      <listitem><para>index selected nodes by some key value</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="insert_command">insert</link></term>
      <listitem><para>create a node in on a given target location</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="loc">location</link></term>
      <listitem><para>relative destination specification (such as after, before, etc.)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="map_command">map</link></term>
      <listitem><para>transform node value/data using Perl or XPath expression</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="move_command">move</link></term>
      <listitem><para>move nodes (in the one-to-one mode)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="nodetype">node-type</link></term>
      <listitem><para>node type specification (such as element, attribute, etc.)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="normalize_command">normalize</link></term>
      <listitem><para>normalizes adjacent textnodes</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="process_xinclude_command">process-xinclude</link></term>
      <listitem><para>load and insert XInclude sections</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="prune_command">remove</link></term>
      <listitem><para>remove given nodes</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="rename_command">rename</link></term>
      <listitem><para>quickly rename nodes with in-line Perl code</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="set_command">set</link></term>
      <listitem><para>create or modify document content (EXPERIMENTAL)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="set_dtd_command">set-dtd</link></term>
      <listitem><para>set document's DTD declaration</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="set_enc_command">set-enc</link></term>
      <listitem><para>set document's charset (encoding)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="set_namespace_command">set-ns</link></term>
      <listitem><para>set namespace of the current node (EXPERIMENTAL)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="set_standalone_command">set-standalone</link></term>
      <listitem><para>set document's standalone flag</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="sort_command">sort</link></term>
      <listitem><para>sort a given node-list by given criteria</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="strip_ws_command">strip-whitespace</link></term>
      <listitem><para>strip leading and trailing whitespace</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="wrap_command">wrap</link></term>
      <listitem><para>wrap given nodes into elements</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="wrap_span_command">wrap-span</link></term>
      <listitem><para>wrap spans of nodes into elements</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="xcopy_command">xcopy</link></term>
      <listitem><para>copy nodes (in the all-to-every mode)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="xinsert_command">xinsert</link></term>
      <listitem><para>create nodes on all target locations</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="xmove_command">xmove</link></term>
      <listitem><para>move nodes (in the all-to-every mode)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="xpath">xpath</link></term>
      <listitem><para>XPath expression</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="xslt_command">xslt</link></term>
      <listitem><para>compile a XSLT stylesheet and/or transform a document with XSLT</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="xupdate_command">xupdate</link></term>
      <listitem><para>apply XUpdate commands on a document</para></listitem>
    </varlistentry></variablelist>
  </para>
 </section></section><section id="Flow">
      <title>Flow control</title>
      <para>
	As almost every scripting language, XSH2 supports subroutines,
	various conditional statements, loops and even exceptions.
      </para>
    <section>
  <title>Related topics</title>
  <para>
    <variablelist termlength="16">    <varlistentry>
      <term><link linkend="block">command-or-block</link></term>
      <listitem><para>single XSH2 command or a block of XSH2 commands</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="call_command">call</link></term>
      <listitem><para>indirect call to a user-defined routine (macro)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="def">def</link></term>
      <listitem><para>sub-routine declaration</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="do_command">do</link></term>
      <listitem><para>execute a given block of commands</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="eval_command">eval</link></term>
      <listitem><para>evaluate given expression as XSH commands</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="exit_command">exit</link></term>
      <listitem><para>exit XSH2 shell</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="foreach">foreach</link></term>
      <listitem><para>loop iterating over a node-list or a perl array</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="if">if</link></term>
      <listitem><para>if statement</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="ifinclude_command">ifinclude</link></term>
      <listitem><para>conditionally include another XSH2 source in current position</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="include_command">include</link></term>
      <listitem><para>include another XSH2 source in current position</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="iterate">iterate</link></term>
      <listitem><para>iterate a block over current subtree</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="last_command">last</link></term>
      <listitem><para>immediately exit an enclosing loop</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="next_command">next</link></term>
      <listitem><para>start the next iteration of an enclosing
	loop</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="prev_command">prev</link></term>
      <listitem><para>restart an iteration on a previous node</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="redo_command">redo</link></term>
      <listitem><para>restart the innermost enclosing loop block</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="return_command">return</link></term>
      <listitem><para>return from a subroutine</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="run_mode">run-mode</link></term>
      <listitem><para>switch into normal execution mode (quit <link linkend="test_mode">test-mode</link>)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="stream_process_command">stream</link></term>
      <listitem><para>process selected elements from an XML stream (EXPERIMENTAL)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="test_mode">test-mode</link></term>
      <listitem><para>do not execute any command, only check the syntax</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="throw_command">throw</link></term>
      <listitem><para>throw an exception</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="try_catch">try</link></term>
      <listitem><para>try/catch statement</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="undef">undef</link></term>
      <listitem><para>undefine sub-routine or variable</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="unless">unless</link></term>
      <listitem><para>negated if statement</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="while">while</link></term>
      <listitem><para>simple while loop</para></listitem>
    </varlistentry></variablelist>
  </para>
 </section></section><section id="Information">
      <title>Retrieving more information</title>
      <para> Beside the possibility to browse the DOM tree and
	list some parts of it (as described in <link linkend="Navigation">Navigation</link>), 
        XSH2 provides commands to obtain other
	information related to open documents as well as the XSH2
	interpreter itself. These commands are listed bellow.
      </para>
    <section>
  <title>Related topics</title>
  <para>
    <variablelist termlength="10">    <varlistentry>
      <term><link linkend="apropos_command">apropos</link></term>
      <listitem><para>search the documentation</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="c14n_command">canonical</link></term>
      <listitem><para>serialize nodes as to canonical XML</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="count_command">count</link></term>
      <listitem><para>calculate a <link linkend="exp">expression</link> and enumerate node-lists</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="doc_info_command">doc-info</link></term>
      <listitem><para>displays various information about a document</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="expression_command">get</link></term>
      <listitem><para>calculate a given expression and return the result.</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="files_command">documents</link></term>
      <listitem><para>display a list of open documents</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="help_command">help</link></term>
      <listitem><para>on-line documentation</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="lineno_command">lineno</link></term>
      <listitem><para>print line-numbers corresponding to matching nodes</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="list_command">ls</link></term>
      <listitem><para>list a given part of a document as XML</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="list_defs_command">defs</link></term>
      <listitem><para>list all user-defined subroutines</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="list_dtd_command">dtd</link></term>
      <listitem><para>show document's DTD</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="locate_command">locate</link></term>
      <listitem><para>show a given node location (as a canonical XPath)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="namespaces_command">namespaces</link></term>
      <listitem><para>List namespaces available in a context of
	  a given nodes</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="options_command">settings</link></term>
      <listitem><para>list current settings using XSH2 syntax</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="print_command">print</link></term>
      <listitem><para>print stuff on standard or standard error output</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="print_enc_command">enc</link></term>
      <listitem><para>show document's original character encoding</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="pwd_command">pwd</link></term>
      <listitem><para>show current context node location (as a canonical XPath)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="validate_command">validate</link></term>
      <listitem><para>validate a document against a DTD, RelaxNG, or XSD schemas</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="var_command">variables</link></term>
      <listitem><para>list global variables</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="version">version</link></term>
      <listitem><para>show version information</para></listitem>
    </varlistentry></variablelist>
  </para>
 </section></section><section id="Namespaces">
      <title>Namespaces in XML and XPath</title> <para>Namespaces provide a
	simple method for qualifying element and attribute names in
	XML documents.  Namespaces are represented by a namespace URI
	but, since the URI can be very long, element and attribute
	names are associated with a namespace using a namespace prefix
	(see the <ulink url="http://www.w3.org/TR/REC-xml-names/">W3C
	recommendation</ulink> for details). In an XML document, a
	prefix can be associated with a namespace URI using a
	declaration which takes form of special attribute of the form
	<literal>xmlns:prefix="namespace uri"</literal> on an element.
	The scope of the namespace declaration is then the subtree of the
	element carrying the special <literal>xmlns:prefix</literal> attribute (and includes
	attributes of the element). Moreover, a default namespace can
	be declared using just <literal>xmlns="namespace
	uri"</literal>.  In that case all unprefixed element names
	in the scope of such a
	declaration belong to the namespace. 
	An unprefixed element which is not in scope of 
	a default namespace declaration does not belong to any namespace.
	It is recommended not to combine namespaced elements and
	non-namespaced elements in a single document.
	Note that regardless of default
	namespace declarations, unprefixed attributes do not belong to any namespace
	(because they are uniquely determined by their name
	and the namespace and name of the the element which carries them).
      </para>
      <para>XSH2 tries to deal namespace declarations transparently
	(creating them if necessary when nodes are copied between
	different documents or scopes of namespace declarations). 
	Most commands which create new elements or attributes provide
	means to indicate a namespace. In addition, XSH2 provides commands <link linkend="declare_namespace_command">declare-ns</link>, <link linkend="set_namespace_command">set-ns</link>, <link linkend="change_namespace_uri_command">change-ns-uri</link>, and <link linkend="change_namespace_prefix_command">change-ns-prefix</link> to
	directly manipulate XML namespace declarations on the current
	node.
      </para>
      <para>
	Since XSH2 is heavily XPath-based, it is important to
	remember that XPath 1.0 maps prefixes to namespaces
	independently of the declarations in the current document. The
	mapping is instead provided via so called XPath context.
	Namespaces can be tested in XPath either using the built-in
	<literal>namespace-uri()</literal> function, but it is more
	convenient to use namespace prefixes associated with namespace
	URIs in the XPath context.  This association is independent of
	the documents to which the XPath expression is applied and can
	be established using the command <link linkend="registerns_command">register-namespace</link>.  Additional, XSH2
	automatically propagates the namespace association in the
	scope of the current node to the XPath context, so that
	per-document prefixes in the current scope can also be used.
      </para>
      <para>
	IMPORTANT: <ulink url="http://www.w3.org/TR/xpath">XPath 1.0</ulink> has no concept of a default namespace.
	Unprefixed names in XPath only match names which have no
	namespace. So, if the document uses a default namespace,
	it is required to associate a
	non-empty prefix with the default namespace
        via  <link linkend="registerns_command">register-namespace</link>
	and add that prefix to names in XPath expressions intended to match 
	nodes in the default namespace.
      </para>
      <example>
	<title>Manipulating nodes in XHTML documents</title>
	<programlisting>open "index.xhtml";
$xhtmlns = "http://www.w3.org/1999/xhtml";
register-namespace x $xhtmlns;
wrap --namespace $xhtmlns '&lt;font color="blue"&gt;' //x:a[@href];
# or 
wrap '&lt;x:font color="blue"&gt;' //x:a[@href];</programlisting>
      </example>
      <para>In the preceding example we associate the (typically
	default) namespace of XHTML documents with the prefix
	<literal>x</literal>.  We than use this prefix to match all
	links (<literal>a</literal> elements) in the document.  Note
	that we do not write <literal>@x:href</literal> to match the
	<literal>@href</literal> attribute because unprefixed
	attributes do not belong to the default namespace.  The <link linkend="wrap_command">wrap</link> command is used to create new containing
	elements for the nodes matched by the XPath expression. We may
	either specify the namespace of the containing element
	explicitly, using <literal>--namespace</literal> option, or
	implicitly, by using a prefix associated with the namespace in
	the XPath context.  In the latter case, XSH2 chooses a
	suitable prefix declared for the namespace in the document
	scope (in this case the default, i.e. no, prefix), adding a
	new namespace declaration if necessary.
      </para>
    <section>
  <title>Related topics</title>
  <para>
    <variablelist termlength="24">    <varlistentry>
      <term><link linkend="change_namespace_prefix_command">change-ns-prefix</link></term>
      <listitem><para>change namespace prefix (EXPERIMENTAL)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="change_namespace_uri_command">change-ns-uri</link></term>
      <listitem><para>change namespace URI (EXPERIMENTAL)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="declare_namespace_command">declare-ns</link></term>
      <listitem><para>create a special attribute declaring an XML namespace (EXPERIMENTAL)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="namespaces_command">namespaces</link></term>
      <listitem><para>List namespaces available in a context of
	  a given nodes</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="registerns_command">register-namespace</link></term>
      <listitem><para>register namespace prefix to use XPath expressions</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="registerns_xhtml_command">register-xhtml-namespace</link></term>
      <listitem><para>register a prefix for the XHTML namespace</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="registerns_xsh_command">register-xsh-namespace</link></term>
      <listitem><para>register a prefix for the XSH2 namespace</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="set_namespace_command">set-ns</link></term>
      <listitem><para>set namespace of the current node (EXPERIMENTAL)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="unregisterns_command">unregister-namespace</link></term>
      <listitem><para>unregister namespace prefix</para></listitem>
    </varlistentry></variablelist>
  </para>
 </section></section><section id="Argtypes">
      <title>Argument Types</title>
      <para>
	XSH2 commands accept arguments of various types,
	usually expressed as Perl or XPath
	<link linkend="exp">expression</link>s. Unlike in most languages,
	individual XSH2 commands may evaluate the same expression differently, 
	usually
	to enforce a result
	of a certain type (such as a node-list, a string, a number,
	a filename, a node name, etc.).
	See <link linkend="exp">expression</link> and
	individual argument types for more information.
      </para>
    <section>
  <title>Related topics</title>
  <para>
    <variablelist termlength="16">    <varlistentry>
      <term><link linkend="block">command-or-block</link></term>
      <listitem><para>single XSH2 command or a block of XSH2 commands</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="command">command</link></term>
      <listitem><para>List of XSH2 commands and their general syntax</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="document">document</link></term>
      <listitem><para>specifying documents</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="enc_string">encoding</link></term>
      <listitem><para>character encoding (codepage) identifier</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="exp">expression</link></term>
      <listitem><para>expression argument type</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="filename">filename</link></term>
      <listitem><para>specifying filenames</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="loc">location</link></term>
      <listitem><para>relative destination specification (such as after, before, etc.)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="nodename">nodename</link></term>
      <listitem><para>specifying names of DOM nodes</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="nodetype">node-type</link></term>
      <listitem><para>node type specification (such as element, attribute, etc.)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="perl_code">perl-code</link></term>
      <listitem><para>in-line code in Perl programming language</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="subname">subroutine</link></term>
      <listitem><para>name of a sub-routine</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="varname">$variable</link></term>
      <listitem><para/></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="xpath">xpath</link></term>
      <listitem><para>XPath expression</para></listitem>
    </varlistentry></variablelist>
  </para>
 </section></section><section id="Variables">
      <title>Variables</title>
      <para>
	In XSH2, like in Perl and XPath, 
	<link linkend="varname">variable names</link> are are prefixed
        with a dollar sign ($). 
        Variables can
        contain arbitrary Perl Scalar (string, number, array
        reference, hash reference or an object reference).  XPath
        objects are transparently mapped to Perl objects via
        XML::LibXML objects. 
        Values can be assigned to variables
        either by simple <link linkend="assign_command">assignments</link> of the form
	<literal>$variable = <link linkend="exp">expression</link></literal>,
        where the right hand side is an expression, or by command 
        <link linkend="assign_command">assignments</link> of the form
	<literal>$variable := <link linkend="command">command</link></literal>,
        where the right hand side is a XSH2 command, or by
	capturing the output of some command with a variable
	redirection of the following form:</para>
      <programlisting>command |&gt; $variable;</programlisting>
      <para>
	XSH2 expressions are evaluated either by XPath
	engine or by Perl (the latter only happens if the entire
	expression is enclosed with braces
	<literal>{...}</literal>), and
	both Perl and XPath can access all XSH2 variables
	transparently (Perl expressions may even assign to them).
      </para>
      <para>
	A simple simple expression consisting of a variable name
        (e.g. <literal>$variable</literal>) is always evaluated by the
        XPath engine and the result is the content of the variable as
        it appears to the XPath data model.  Since in XPath object
        cannot be void (undefined), XPath engine complains, if the
        value of the variable is undefined. On the other hand,
        expressions like <literal>{$variable}</literal> are evaluated by
        Perl, which results in the value of the variable as seen
	by Perl.
      </para>
      <para>
	Variables can also be used as macros for complicated XPath
	expressions. Any occurrence of a substring of the form
	<literal>${variable}</literal> in an XPath expression is
	interpolated to the value of <literal>$variable</literal> (if
	<literal>$variable</literal> contains an object rather than a
	string or number, then the object is cast to string first) before the
	entire expression is evaluated.  So, for example, if
	<literal>${variable}</literal> contains string
	"<literal>chapter[title]</literal>" (without the quotes), then
	the XPath expression
	<literal>//sect1/${variable}/para</literal> interpolates to 
	<literal>//sect1/chapter[title]/para</literal> prior
	to evaluation. 
      </para>
      <para>
	To display the current value of a variable, use either
	<link linkend="print_command">print</link>
	or (in case of a global variables - the distinction is discussed below)
	the command <link linkend="var_command">variables</link>:
      </para>
      <informalexample>
	  <programlisting>xsh&gt; <userinput>$b="my_document";</userinput>
xsh&gt; <userinput>$file="${b}s.xml";</userinput>
xsh&gt; <userinput>$f := open $file;</userinput>
xsh&gt; <userinput>ls //$b[count(descendant::para)&gt;10]</userinput>
xsh&gt; <userinput>print $b</userinput>
my_document
xsh&gt; <userinput>variables</userinput>
...
$b='my_document';
...
$file='my_documents.xml';
...
</programlisting>
	</informalexample>
      <para>
	Variables can also serve as containers for documents and can be used to
	store lists of nodes that result from evaluating an XPath
	expression (a.k.a. XPath node-sets). This is especially useful
	when a sequence of commands is to be performed on some fixed
        set of nodes and repetitive evaluation of the same XPath 
	expression would be lengthy.  XPath
	node-sets are represented by
	<literal>XML::LibXML::NodeList</literal> Perl objects (which
	is simply a array reference blessed to the above class, which
	provides some simple operator overloading). In XPath, by a
	node-set by definition can only contain a single copy of each
	node and the nodes within a node-set are processed in the same
	order as they appear in the XML document. Having XPath
	node-sets represented by a list gives us the advantage of having
	the possibility to process the list in a different order than
	the one implied by the document (which is what happens
	if a variable containing a node-list is evaluated by Perl 
	rather than XPath), see an example below.
      </para>
      <informalexample>
	<programlisting>xsh&gt; <userinput>$creatures = //creature[@status='alive']</userinput>
# process creatures in the document order: 
xsh&gt; <userinput>foreach $creature print @name;</userinput>
# process creatures in the reverse document order: 
xsh&gt; <userinput>foreach { reverse @$creature } print @name;</userinput>
# append some more nodes to a node-list (using a variant of
# a simple assignment)
xsh&gt; <userinput>$creatures += //creature[@status='dead'];</userinput>
# again, we can process creatures in order implied by the document:
xsh&gt; <userinput>foreach $creature print @name;</userinput>
# but we can also process first living and then dead creatures,
# since this is how they are listed in $creature
xsh&gt; <userinput>foreach {$creature} print @name;</userinput>
# same as the above is
xsh&gt; <userinput>foreach {@$creature} print @name;</userinput>
</programlisting>
      </informalexample>
      <para>
	XSH2 variables are either globally or lexically scoped.
	Global variables need not to be declared (they can be directly
	assigned to), whereas lexical variables must be declared
	using the command <link linkend="my_command">my</link>. Global variable
	assignment may also be made temporal for the enclosing block,
	using <link linkend="local_command">local</link>.
      </para>
      <informalexample>
	<programlisting>$var1 = "foo";           # a global variable requires no declaration
local $var1 $var2 $var3; # localizes global variables
$var1 = "bar";           # assignment to a localized variable is temporary
local $var4 = "foo";     # localized assignment
my $var1 $var $var3;     # declares lexical variables
my $var1 = "foo";        # lexical variable declaration with assignment
</programlisting>
      </informalexample>
      <para>
	Lexical variables are only defined in the scope of current
	block or subroutine. There is no way to refer to a lexical
	variable form outside of the block it was declared in, nor
	from within a nested subroutine call. Of course, lexical
	variables can be referred to from nested blocks or Perl
	expressions (where they behave just like Perl's lexical
	variables).
      </para>
      <para>
	On the other hand, global or localized XSH2 variables are just
	Perl Scalar variables belonging to the
	<literal>XML::XSH2::Map</literal> namespace, which is also the
	default namespace for any Perl code evaluated from XSH2 (so
	there's no need to use this prefix explicitly in Perl expressions,
	unless of course there is a lexical variable in the current
	scope with the same).
      </para>
      <para>Localizing a variable using the <literal>local</literal>
	keyword makes all assignments to it occurring in the enclosing block
	temporary. The variable itself remains global, only its
	original value is restored at the end of the block that localized it.
      </para>
      <para> 
	In all above cases, it is possible to arbitrarily
	intermix XSH2 and Perl assignments:
      </para>
      <informalexample>
	  <programlisting>xsh&gt; <userinput>ls //chapter[1]/title</userinput>
&lt;title&gt;Introduction&lt;/title&gt;
xsh&gt; <userinput>$a=string(//chapter[1]/title)</userinput>
xsh&gt; <userinput>perl { $b="CHAPTER 1: ".uc($a); }</userinput>
xsh&gt; <userinput>print $b</userinput>
CHAPTER 1: INTRODUCTION
</programlisting>
      </informalexample>
      <para>
	Although all XSH2 variables are in fact Perl Scalars, it is
	still possible to store Perl Array or Hash value to a XSH2
	variable via reference. The following example demonstrates
	using Perl Hashes to collect and print some simple racial
	statistics about the population of Middle-Earth:
      </para>
      <informalexample>
	<programlisting>my $races;
foreach a:/middle-earth/creature { 
  my $race=string(@race);
  perl { $races-&gt;{$race}++ };
}
print "Middle-Earth Population (race/number of creatures)"
print { map "$_/$races-&gt;{$_}\n" keys(%$races); };
</programlisting>
      </informalexample>
    <section>
  <title>Related topics</title>
  <para>
    <variablelist termlength="10">    <varlistentry>
      <term><link linkend="assign_command">assign</link></term>
      <listitem><para>variable assignment</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="document">document</link></term>
      <listitem><para>specifying documents</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="exp">expression</link></term>
      <listitem><para>expression argument type</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="local_command">local</link></term>
      <listitem><para>temporarily assign new value to a variable</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="subname">subroutine</link></term>
      <listitem><para>name of a sub-routine</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="varname">$variable</link></term>
      <listitem><para/></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="xpath">xpath</link></term>
      <listitem><para>XPath expression</para></listitem>
    </varlistentry></variablelist>
  </para>
 </section></section><section id="Redirection">
      <title>Command output redirection</title>
      <para>WARNING: XSH2 redirection syntax is not
	yet finished. It is currently the same as in XSH1
	but this may be changed in the future releases.
      </para>
      <para>
	Output redirection can be used to pipe output of some XSH <link linkend="command">command</link> to some external program, or to capture it
	to a variable. Redirection of output of more than one XSH
	command can be achieved using the <link linkend="do_command">do</link> command.
      </para> 
      <section id="Redirection_pipe">
	<title>Redirect output to an external program</title>
	<para>The syntax for redirecting the output of a XSH command to
	  an external program, is <literal>xsh-command | shell-command
	    ;</literal>, where <literal>xsh-command</literal> is any XSH2
	  command and <literal>shell-command</literal> is any command
	  (or code) recognized by the default shell interpreter of the
	  operating system (i.e. on UNIX systems by
	  <literal>/bin/sh</literal> or <literal>/bin/csh</literal>, on
	  Windows systems by <literal>cmd</literal>).  The shell command
	  may contain further redirections (as supported by the system
	  shell interpreter), but should not contain semicolons, except when
	  the whole shell command is enclosed in brackets.
	</para>
	<example>
	  <title>Use well-known UNIX commands to filter XPath-based XML
	    listing from a document and count the results</title>
	  <programlisting>xsh&gt; <userinput>ls //something/* | grep foo | wc</userinput></programlisting>
	</example>
      </section>
      <section id="Redirection_variable">
	<title>Capture output to a variable</title>
	<para>
	  The syntax for capturing the output of an XSH command to a variable is
	  <literal>xsh-command |&gt; $variable</literal>, 
	  where <literal>xsh-command</literal> is
	  any XSH <link linkend="command">command</link> and 
	  <literal>$variable</literal> is any valid
	  name for a <link linkend="Variables">variable</link>.
	</para>
	<example>
	  <title>Store the number of all words in a variable named count.</title>
	  <programlisting>xsh&gt; <userinput>count //words |&gt; $count</userinput></programlisting>
	</example>
      </section>
    </section><section id="Configuration">
      <title>Global settings</title> <para>The commands listed below
      can be used to modify the default behavior of the XML parser or
      XSH2 itself.  Some of the commands switch between two different
      modes according to a given expression (which is expected to
      result either in zero or non-zero value). Other commands also
      working as a flip-flop have their own explicit counterparts
      (e.g.  <link linkend="verbose">verbose</link> and <link linkend="quiet">quiet</link> or
      <link linkend="debug">debug</link> and <link linkend="nodebug">nodebug</link>).  This
      inconsistency is due to historical reasons.
      </para>
      <para>
	The <link linkend="encoding">encoding</link> and <link linkend="query_encoding">query-encoding</link>
	settings 
	allow to specify character
	encodings of user's input and XSH2's own output. This is particularly
	useful when you work with UTF-8 encoded documents on a console
	which only supports 8-bit characters.
      </para>
      <para>
	The <link linkend="options_command">settings</link> command
	displays current settings by means of XSH2 commands.
	Thus it can not only be used to review current values, but
	also to store them for future use, e.g. in ~/.xshrc file.
      </para>
      <informalexample>
	<programlisting>xsh&gt; <userinput>settings | cat &gt; ~/.xshrc</userinput></programlisting>
      </informalexample>
    <section>
  <title>Related topics</title>
  <para>
    <variablelist termlength="27">    <varlistentry>
      <term><link linkend="backups">backups</link></term>
      <listitem><para>turn on backup file creation</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="cdonopen">switch-to-new-documents</link></term>
      <listitem><para>set on/off changing current document to newly
	  open/created files</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="complete_attributes">parser-completes-attributes</link></term>
      <listitem><para>turn on/off parser's ability to fill default attribute values</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="debug">debug</link></term>
      <listitem><para>display many annoying debugging messages</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="empty_tags">empty-tags</link></term>
      <listitem><para>turn on/off serialization of empty tags</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="enc_string">encoding</link></term>
      <listitem><para>character encoding (codepage) identifier</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="encoding">encoding</link></term>
      <listitem><para>choose output charset</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="indent">indent</link></term>
      <listitem><para>turn on/off pretty-printing</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="keep_blanks">keep-blanks</link></term>
      <listitem><para>turn on/off ignorable whitespace preservation</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="load_ext_dtd">load-ext-dtd</link></term>
      <listitem><para>turn on/off external DTD fetching</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="nobackups">nobackups</link></term>
      <listitem><para>turn off backup file creation</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="nodebug">nodebug</link></term>
      <listitem><para>turn off debugging messages</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="options_command">settings</link></term>
      <listitem><para>list current settings using XSH2 syntax</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="parser_expands_entities">parser-expands-entities</link></term>
      <listitem><para>turn on/off parser's tendency to expand entities</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="parser_expands_xinclude">parser-expands-xinclude</link></term>
      <listitem><para>turn on/off transparent XInclude insertion by parser</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="pedantic_parser">pedantic-parser</link></term>
      <listitem><para>make the parser more pedantic</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="query_encoding">query-encoding</link></term>
      <listitem><para>declare the charset of XSH2 source files and terminal input</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="quiet">quiet</link></term>
      <listitem><para>turn off many XSH2 messages</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="recovering">recovering</link></term>
      <listitem><para>turn on/off parser's ability to fix broken XML</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="registerfunc_command">register-function</link></term>
      <listitem><para>define XPath extension function (EXPERIMENTAL)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="registerns_command">register-namespace</link></term>
      <listitem><para>register namespace prefix to use XPath expressions</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="registerns_xhtml_command">register-xhtml-namespace</link></term>
      <listitem><para>register a prefix for the XHTML namespace</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="registerns_xsh_command">register-xsh-namespace</link></term>
      <listitem><para>register a prefix for the XSH2 namespace</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="run_mode">run-mode</link></term>
      <listitem><para>switch into normal execution mode (quit <link linkend="test_mode">test-mode</link>)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="skip_dtd">skip-dtd</link></term>
      <listitem><para>turn on/off serialization of DTD DOCTYPE declaration</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="test_mode">test-mode</link></term>
      <listitem><para>do not execute any command, only check the syntax</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="unregisterfunc_command">unregister-function</link></term>
      <listitem><para>undefine extension function (EXPERIMENTAL)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="unregisterns_command">unregister-namespace</link></term>
      <listitem><para>unregister namespace prefix</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="validation">validation</link></term>
      <listitem><para>turn on/off validation in XML parser</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="verbose">verbose</link></term>
      <listitem><para>make XSH2 print many messages</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="xpath_axis_completion_command">xpath-axis-completion</link></term>
      <listitem><para>sets TAB completion for axes in xpath expressions in
	the interactive mode</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="xpath_completion_command">xpath-completion</link></term>
      <listitem><para>turn on/off TAB completion for xpath expressions in
	the interactive mode</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="xpath_extensions_command">xpath-extensions</link></term>
      <listitem><para>map predefined XSH2 XPath extension functions to
	  no or other namespace
	</para></listitem>
    </varlistentry></variablelist>
  </para>
 </section></section><section id="Perl_shell">
      <title>Interacting with Perl and Shell</title>
      <para>
	Along with XPath, Perl is one of two XSH2 expression languages,
	and borrows XSH2 its great expressive power.
	Perl is a language optimized for scanning arbitrary text
	files, extracting information from those text files, and
	printing reports based on that information. It has built-in
	regular expressions and powerful yet easy to learn 
	data structures (scalars, arrays, hash tables). It's also a
	good language for many system management tasks.
	XSH2 itself is written in Perl (except for the XML engine,
	which uses libxml2 library written in C by Daniel Veillard).
      </para>
      <section id="binding_perl">
	<title>Calling Perl</title>
	<para>
	  Perl <link linkend="perl_code">expressions or blocks of
	  code</link> can either be used as arguments to any XSH2 command.
	  One of them is 
	  <link linkend="perl_command">perl</link> command
	  which simply evaluates the given Perl block.
	  Other commands, such as <link linkend="map_command">map</link>,
	  even require Perl expression argument and allow
	  quickly change DOM node content.
	  Perl expressions may also provide lists of strings to iterate over
	  with a <link linkend="foreach">foreach</link> loop, or serve as
	  conditions for <link linkend="if">if</link>, <link linkend="unless">unless</link>, and 
	  <link linkend="while">while</link> statements.
	</para>
	<para>
	  To prevent conflict between XSH2 internals and the evaluated
	  Perl code, XSH2 runs such code in the context of a special
	  namespace <literal>XML::XSH2::Map</literal>. As described in
	  the section <link linkend="Variables">Variables</link>, XSH2 string
	  variables may be accessed and possibly assigned from Perl
	  code in the most obvious way, since they actually
	  <emphasis>are</emphasis> Perl variables defined in the
	  <literal>XML::XSH2::Map</literal> namespace.</para>
	<para>
	  The interaction between XSH2 and Perl actually works the
	  other way round as well, so that you may call back XSH2 from the
	  evaluated Perl code.  For this, Perl function
	  <literal>xsh</literal> is defined in the
	  <literal>XML::XSH2::Map</literal> namespace.  All parameters
	  passed to this function are interpreted as XSH2 commands.</para>
	<para>Moreover, the following Perl helper functions are defined:
	</para>
	<para><literal>xsh(string,....)</literal> - evaluates
	  given string(s) as XSH2 commands.
	</para>
	<para><literal>call(name)</literal> - call a given
	  XSH2 subroutine.
	</para>
	<para><literal>count(string)</literal> - evaluates
	  given string as an XPath expression and returns
	  either literal value of the result (in case of
	  boolean, string and float result type) or
	  number of nodes in a returned node-set.
	</para>
	<para><literal>literal(string|object)</literal> -
	  if passed a string, evaluates it as a XSH2 expression 
	  and returns the literal value of the result;
	  if passed an object, returns literal value of
	  the object.
	  For example,
	  <literal>literal('$doc/expression')</literal> returns the same
	  value as <literal>count('string($doc/expression)')</literal>.
	</para>
	<para>
	  <literal>serialize(string|object)</literal> - 
	  if passed a string, it first evaluates the string
	  as a XSH2 expression to obtain a node-list object. 
	  Then it serializes the object into XML.
	  The resulting string is equal to the output of the XSH2 command <link linkend="list_command">ls</link> applied on the same expression or object
	  expression only without indentation and folding.
	</para>
	<para>
	  <literal>type(string|object)</literal> - 
	  if passed a string, it first evaluates
	  the string as XSH2 expression to obtain a node-list object.
	  It returns a list of strings representing
	  the types of nodes in the node-list
	  (ordered in the canonical document order).
	  The returned type strings are: 
	  <literal>element</literal>,
	  <literal>attribute</literal>,
	  <literal>text</literal>,
	  <literal>cdata</literal>,
	  <literal>pi</literal>,
	  <literal>entity_reference</literal>,
	  <literal>document</literal>,
	  <literal>chunk</literal>,
	  <literal>comment</literal>,
	  <literal>namespace</literal>,
	  <literal>unknown</literal>.
	</para>
	<para>
	  <literal>nodelist(string|object,...)</literal> - 
	  converts its arguments to objects if necessary
	  and returns a node-list consisting of the objects.
	</para>
	<para>
	  <literal>xpath(string, node?)</literal> - 
	  evaluates a given string as an XPath expression
	  in the context of a given node and returns
	  the result.
	</para>
	<para>
	  <literal>echo(string,...)</literal> - prints
	  given strings on XSH2 output.
	  Note, that in the interactive mode,
	  XSH2 redirects all output to a specific terminal file handle
	  stored in the variable <literal>$OUT</literal>.
	  So, if you for example mean to pipe the result 
	  to a shell command, you should avoid using STDOUT filehandle
	  directly. You may either use the usual <literal>print</literal>
	  without a filehandle, 
	  use the <literal>echo</literal> function,
	  or use <literal>$OUT</literal> as a filehandle.
	</para>
	<para>
	  In the following examples we use Perl to populate the
	  Middle-Earth with Hobbits whose names are read from a text
	  file called <literal>hobbits.txt</literal>, unless there are
	  some Hobbits in Middle-Earth already.
	</para>
	<example>
	  <title>Use Perl to read text files</title>
	  <programlisting>unless (//creature[@race='hobbit']) {
  perl {
    open my $fh, "hobbits.txt" };
    @hobbits=&lt;$file&gt;;
    close $fh;
  }
  foreach { @hobbits } {
    copy xsh:new-element("creature","name",.,"race","hobbit")
      into m:/middle-earth/creatures;
  }
}
</programlisting>
	</example>
	<example>
	  <title>The same code as a single Perl block</title>
	  <programlisting>perl {
  unless (count(//creature[@race='hobbit'])) {
    open my $file, "hobbits.txt";
    foreach (&lt;$file&gt;) {
      xsh(qq{insert element "&lt;creature name='$_' race='hobbit'&gt;"
        into m:/middle-earth/creatures});
    }
    close $file;
  }
};</programlisting>
	</example>
      </section>
      <section id="binding_perl_xpathextensions">
        <title>Writing your own XPath extension functions in Perl</title>
        <para>
	  XSH2 allows users to extend the set of XPath functions by
	  providing extension functions written in Perl.  This can
	  be achieved using the <link linkend="registerfunc_command">register-function</link>
	  command. The perl code implementing an extension function
	  works as a usual perl routine accepting its arguments in
	  <literal>@_</literal> and returning the result. The
	  following conventions are used:
	</para>
	<para>
	  The arguments passed to the perl implementation by the XPath
	  engine are simple scalars for string, boolean and float
	  argument types and
	  <literal>XML::LibXML::NodeList</literal> objects for node-set
	  argument types. The implementation is
	  responsible for checking the argument number and types. The
	  implementation may use general Perl functions as well as
	  <literal>XML::LibXML</literal>
	  methods to process the arguments and return the result.
	  Documentation for the <literal>XML::LibXML</literal> Perl module
	  can be found for example at <ulink url="http://search.cpan.org/~pajas/XML-LibXML/">http://search.cpan.org/~pajas/XML-LibXML/</ulink>.
	</para>
	<para>
	  Extension functions SHOULD NOT MODIFY the document DOM tree.
	  Doing so could not only confuse the XPath engine but possibly 
	  even result in an critical error (such as segmentation fault).
	  Calling XSH2 commands from extension function implementations
	  is also dangerous and isn't generally recommended.
	</para>
	<para>
	  The extension function implementation must return 
	  a single value, which can be of
	  one of the following types: simple scalar (a number or
	  string), <literal>XML::LibXML::Boolean</literal> object
	  reference (result is a boolean value),
	  <literal>XML::LibXML::Literal</literal> object reference
	  (result is a string), <literal>XML::LibXML::Number</literal>
	  object reference (result is a float),
	  <literal>XML::LibXML::Node</literal> (or derived) object
	  reference (result is a node-set consisting of a single node),
	  or <literal>XML::LibXML::NodeList</literal> (result is a
	  node-set). For convenience, simple (non-blessed) array
	  references consisting of
	  <literal>XML::LibXML::Node</literal> objects can also be
	  used for a node-set result instead of a
	  <literal>XML::LibXML::NodeList</literal>.
        </para>
      </section>
      <section id="binding_shell">
	<title>Calling the System Shell</title>
	<para>
	  In the interactive mode, XSH2 interprets all lines starting
	  with the exclamation mark (<literal>!</literal>) as shell
	  commands and invokes the system shell to interpret the line
	  (this is to mimic FTP and similar command-line interpreters).
	</para>
	<informalexample>
	  <programlisting>xsh&gt; <userinput>!ls -l</userinput>
-rw-rw-r--    1 pajas    pajas        6355 Mar 14 17:08 Artistic
drwxrwxr-x    2 pajas    users         128 Sep  1 10:09 CVS
-rw-r--r--    1 pajas    pajas       14859 Aug 26 15:19 ChangeLog
-rw-r--r--    1 pajas    pajas        2220 Mar 14 17:03 INSTALL
-rw-r--r--    1 pajas    pajas       18009 Jul 15 17:35 LICENSE
-rw-rw-r--    1 pajas    pajas         417 May  9 15:16 MANIFEST
-rw-rw-r--    1 pajas    pajas         126 May  9 15:16 MANIFEST.SKIP
-rw-r--r--    1 pajas    pajas       20424 Sep  1 11:04 Makefile
-rw-r--r--    1 pajas    pajas         914 Aug 26 14:32 Makefile.PL
-rw-r--r--    1 pajas    pajas        1910 Mar 14 17:17 README
-rw-r--r--    1 pajas    pajas         438 Aug 27 13:51 TODO
drwxrwxr-x    5 pajas    users         120 Jun 15 10:35 blib
drwxrwxr-x    3 pajas    users        1160 Sep  1 10:09 examples
drwxrwxr-x    4 pajas    users          96 Jun 15 10:35 lib
-rw-rw-r--    1 pajas    pajas           0 Sep  1 16:23 pm_to_blib
drwxrwxr-x    4 pajas    users         584 Sep  1 21:18 src
drwxrwxr-x    3 pajas    users         136 Sep  1 10:09 t
-rw-rw-r--    1 pajas    pajas          50 Jun 16 00:06 test
drwxrwxr-x    3 pajas    users         496 Sep  1 20:18 tools
-rwxr-xr-x    1 pajas    pajas        5104 Aug 30 17:08 xsh</programlisting>
	</informalexample>
	<para>
	  To invoke a system shell command or program
	  from the non-interactive mode or from a complex
	  XSH2 construction, use the <link linkend="exec_command">exec</link>
	  command.
	</para>
	<para>
	  Since UNIX shell commands are very powerful tool for
	  processing textual data, XSH2 supports direct redirection of
	  XSH2 commands output to system shell command.  This is very
	  similarly to the redirection known from UNIX shells, except
	  that here, of course, the first command in the pipe-line
	  colone is an XSH2 <link linkend="command">command</link>. Since semicolon (<literal>;</literal>)
	  is used in XSH2 to separate commands, it has to be prefixed
	  with a backslash if it should be used for other purposes.
	</para>
	<example>
	  <title>Use grep and less to display context of `funny'</title>
	  <programlisting>xsh&gt; ls //chapter[5]/para | grep funny | less</programlisting>
	</example>
	<example>
	  <title>The same on Windows 2000/XP systems</title>
	  <programlisting>xsh&gt; ls //chapter[5]/para | find "funny" | more</programlisting>
	</example>
      </section>
    <section>
  <title>Related topics</title>
  <para>
    <variablelist termlength="10">    <varlistentry>
      <term><link linkend="cd_command">lcd</link></term>
      <listitem><para>change system working directory</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="exec_command">exec</link></term>
      <listitem><para>execute a shell command</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="exp">expression</link></term>
      <listitem><para>expression argument type</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="hash_command">hash</link></term>
      <listitem><para>index selected nodes by some key value</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="map_command">map</link></term>
      <listitem><para>transform node value/data using Perl or XPath expression</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="perl_code">perl-code</link></term>
      <listitem><para>in-line code in Perl programming language</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="perl_command">perl</link></term>
      <listitem><para>evaluate in-line Perl code</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="rename_command">rename</link></term>
      <listitem><para>quickly rename nodes with in-line Perl code</para></listitem>
    </varlistentry></variablelist>
  </para>
 </section></section><section id="Prompt">
      <title>Prompt in the interactive shell</title>
      <para>
	Like many other shells, XSH2 provides means for customizing
	the format of its interactive shell prompt.  The prompt is
	displayed according to the content of the variable
	<literal>$PROMPT</literal> on which the following
	substitutions and interpolations are performed
	(in this order):
      </para>
<para>1. Prompt-string replacements</para>
<programlisting>
%% - percent sign
%p - XPath location of the current node
%P - like %p but without an initial document variable
%l - XPath location of the current node with ID-shortcuts
%L - like %l but without an initial document variable
%n - name of the current node
%N - local name of the current node
%c - canonical XPath name of the current node
%y - type of the current node (element,attribute,...)
%i - ID of the current node
%d - current document variable
%h - the hostname up to the first '.'
%H - the hostname
%s - XSH shell name (basename of $0)
%t - the current time in 24-hour HH:MM:SS format
%T - the current time in 12-hour HH:MM:SS format
%@ - the current time in 12-hour am/pm format
%A - the current time in 24-hour HH:MM format
%u - the username of the current user
%v - the version of XSH2 (e.g., 2.1.0)
%V - the revision number of XML::XSH2::Functions (e.g. 2.40)
%w - current working directory (on the local filesystem)
%W - basename of %w
</programlisting>
<para>2. Variable, XPath and Perl interpolations</para>
<para>
Substrings of the forms <literal>${variable}</literal>,
<literal>${{...perl...}}</literal> and
<literal>${(...xpath...)}</literal> are interpolated as in XSH2
<link linkend="exp">expressions</link>.
</para>
      <para>3. Special character substitution</para>
<programlisting>
\n - newline character
\r - line-feed character
\t - tab character
\a - bell character
\b - backspace character
\f - form feed character
\e - escape character (\033)
\\ - backslash character
\nnn - the character corresponding to the octal number nnn
(useful for non-printable terminal control characters)
</programlisting>
<para>The default value of <literal>$PROMPT</literal> is <literal>"%p&gt;"</literal>.</para>
      <para>Note that you must escape <literal>${...}</literal>
	interpolators like <literal>\${...}</literal> if you
	want them to be evaluated at each prompt
	rather than at the time of the assignment to <literal>$PROMPT</literal>.
	For example:
      </para>
      <example>
	<title>Let `uname` be computed once, `date` at every prompt</title>
	<programlisting>$PROMPT="[${{ chomp($u=`uname`);$u }} \${{ chomp($d=`date`);$d }}] %p&gt;"</programlisting>
      </example>
    </section><section id="xsh2delta">
      <title>Changes since XSH 1.x</title>
      <para>
	This section briefly describes 
	differences between XSH2 and previous XSH 1.x releases.
	The list should not be considered complete.
	Some syntax variations or amendments in 
	the semantics of various commands may not be documented
	in this section, neither are various improvements in the
	XSH interpreter.
      </para>
      <section id="new_in_xsh2">
	<title>Changes in XSH2</title>
	<orderedlist>
	  <listitem>
	<para>
	  In XSH2, subroutines can be called without a 
	  <link linkend="call_command">call</link>. They can be 
	  <link linkend="def">redefined</link> and 
	  <link linkend="undef">undefined</link>.
	  The command <link linkend="call_command">call</link>
	  can still be used, but it's use only makes sense
	  in indirect calls, where subroutine's name is computed
	  from an expression.
	</para>
	<programlisting>def foo $param1 $param2 { 
  # param1 and $param2 are lexical (a.k.a. my)
  ls $param1; 
  echo $param2 
}
foo (//chapter)[1] (//chapter)[1]/title

def inc $param1 { return ($param1 + 1) }
$two := inc 1;
</programlisting>
	  </listitem>
	  <listitem>
	    <para>
	  XSH2 uses variables of the form <link linkend="varname">$variable</link> 
	  for all kinds of objects, including node-sets
	  (which, if evaluated as Perl expressions, preserve node order).
	  Node-list variables of XSH 1.x have been deprecated.
	</para>
	<programlisting>$var = //foo/bar;                 # node set
$var = "hallo world";             # string
$var = xsh:new-element("foo");    # node object
$var = { ['a','b','c'] };         # Perl array reference
$var = {{ 'a'=&gt;'A', 'b'=&gt;'B' }};  # Perl hash reference
</programlisting>
	  </listitem>
	  <listitem>
	    <para>
	  XSH2 allows variables to be used in XPath just as they are used in XSLT:
	</para>
	<programlisting>$var = //foo/bar;
ls //baz[ . = $var[@test=1]/any ]
</programlisting>
	    <para>
	  Variable interpolation is still available in XSH2 via ${var},
	  but it's importance is diminished compared to XSH 1.x, 
	  because the XPath engine now evaluates
	  variables directly. Interpolation can still be used for
	  things like "XPath-macros":
	</para>
	<programlisting>$filter = "[ . = $var[@test=1]/any ]";
ls //baz${filter};
</programlisting>
	  </listitem>
	  <listitem>
	    <para>
	  XSH2 equally supports XPath and
	  Perl <link linkend="exp">expressions</link> 
	  (written in braces { ... }).
	  Unfortunately, Perl expressions can't be embedded in XPath 
	  <link linkend="exp">expressions</link>,
	  but one can still use variables as an agent:
	</para>
	<programlisting>perl { use MIME::Base64 };
my $encoded = { encode_base64('open sesame') }
ls //secret-cave[string(password) = $encoded]
</programlisting>
	<para>We can, however, use Perl-only expressions complemented with auto-conversion
	      to do things like:</para>
	<programlisting>copy { encode_base64('Pe do mellon a minno!') } replace //secret-cave/password/text();
</programlisting>
	  </listitem>
	  <listitem>
	    <para>
	  Commands return values 
	  (see <link linkend="assign_command">:= assignment</link>, 
	  or <link linkend="exp">&amp;{ } expressions</link>).
	</para>
	<programlisting>$moved_paras := xmove //para replace .;
$chapter := wrap chapter $moved_paras;
ls $chapter;

# or just

ls &amp;{ wrap chapter &amp;{ xmove //para replace . } };
</programlisting>
	  </listitem>
	  <listitem>
	    <para>
	  XSH2 deprecates "string" expressions of XSH 1.x.  However,
	  for convenience, some XSH2 commands interpret name-like
	  XPath expressions on certain argument positions as strings
	  (mostly commands that expect file-name or node-name
	  arguments):
	</para>
	<programlisting>insert element my_document into .;
insert text "foo" into my_document;

$doc := open my_document;         # opens file named "my_document"
$doc := open "my_document";       # same
$doc := open (my_document);       # opens file named "foo"
$doc := open string(my_document); # same
</programlisting>
	  </listitem>
	  <listitem>
	<para>
	  In XSH2, XML documents
	  have no ID. 
	  They are referred to using variables (which fits in well with the unified variable concept):
	</para>
	<programlisting>$doc1 := open "foo1.xml";
$doc2 := open "foo2.xml";
ls ($doc1//para|$doc2//para);
cd $doc1;
ls id('intro');             # finds ID intro in the current document ($doc1)
ls xsh:id2($doc2, 'intro'); # finds ID intro in $doc2
</programlisting>
	  </listitem>
	  <listitem>
	    <para>
	      XSH2 commands have options and flags instead of many optional (positional) arguments.
	      Options/flags usually have both long forms (like --flag) and equivalent
	      short forms (like :f) (colon is borrowed from Scheme, because dash is reserved for XPath minus).
	    </para>
	<programlisting>$doc := open --format html "version1.html";
save --file "version2.xml" $doc;

ls --fold /;
ls :f /;
ls --depth 1 /;
ls :d 1 /;

# all the same:
$sorted = sort --key @name --locale --descending //user;
$sorted = sort :l:d:k@name //user;
$sorted = sort --key @name --compare { use locale; $b cmp $a } //user;

validate --relaxng --file "test.rng" $mydoc;
validate --public "-//OASIS//DTD DocBook XML V4.1.2//EN" $mydoc;
validate --yesno $mydoc;
</programlisting>
	  </listitem>
	  <listitem>
	    <para>Finally, <link linkend="eval_command">eval</link> is no longer
	      an alias for <link linkend="perl_command">perl</link> in XSH2,
	      but instead evaluates strings containing XSH2 commands
	      (so <literal>eval $string</literal> now practically works like old ugly
	      <literal>perl { xsh($string) }</literal>). See the documentation for 
	      <link linkend="eval_command">eval</link> for a handy usage example
	      (no more PHP, XSTL and XPathScript :-)).
	    </para>
	  </listitem>
	</orderedlist>
      </section>
      <section id="xsh2delta_examples">
	<title>Examples</title>
	<example>
	  <title>Open command has changed.</title>
	  <programlisting>XSH1:
foo = file.xml;
or
foo = "file.xml";
</programlisting>
	  <programlisting>XSH2:
$foo := open file.xml;        # file.xml is a bareword in file-name context
or
$foo := open "file.xml";      # "file.xml" is a XPath string
or
$foo := open {"file.xml"};    # "file.xml" is a Perl string
or
$foo = xsh:open("file.xml");  # righthand side is an XPath extension function
</programlisting>
	</example>
	<example>
	  <title>XSH2 commands have options</title>
	  <programlisting>XSH1:
open HTML FILE foo2 = "file.html";
</programlisting>
	  <programlisting>XSH2:
$foo2 := open --format html "file.html";
</programlisting>
	</example>


	<example>
	  <title>documents</title>
	  <programlisting>XSH1:
foo = file.xml;
ls foo:(//bar|//baz);
</programlisting>
	  <programlisting>XSH2:
$foo := open file.xml;
ls ($foo//bar|$foo//baz);
</programlisting>
	</example>


	<example>
	  <title>variable interpretation</title>
	  <programlisting>XSH1:
$family = "Arial";
ls //font[@family="$family"];   # interpolation
or
ls //font[@family="${family}"]; # interpolation
</programlisting>
	  <programlisting>XSH2:
$family = "Arial";
ls //font[@family=$family];     # evaluation by XPath engine
or
ls //font[@family="${family}"]; # interpolation
</programlisting>
	</example>



	<example>
	  <title>adding new nodes</title>
	  <programlisting>XSH1:
insert attribute "foo=bar" into /scratch;
</programlisting>
	  <programlisting>XSH2:
insert attribute "foo=bar" into /scratch;
or
copy xsh:new-attribute("foo","bar") into /scratch;
</programlisting>
	</example>


	<example>
	  <title>foreach with perl expression</title>
	  <programlisting>XSH1:
foreach { glob('*.xml') } {
  open doc = $__;
  ...
}
</programlisting>
	  <programlisting>XSH2:
foreach { glob('*.xml') } {
  my $doc := open .;
  ...
} 
</programlisting>
	</example>
	<example>
	  <title>foreach (perl expression) with variable</title>
	  <programlisting>XSH2:
foreach my $filename in { glob('*.xml') } {
  my $doc := open $filename;
  ...
} 
</programlisting>
	</example>
	<example>
	  <title>sorting nodes</title>
	  <programlisting>XSH1:
%list = //player;
sort @best_score { $a &lt;=&gt; $b } %list;
copy %list into .;
</programlisting>
	  <programlisting>XSH2:
$list := sort --numeric --key @best_score //player;
copy { $list } into .;
or
copy &amp;{ sort --numeric --key @best_score //player } into .;
or (using short options)
copy &amp;{ sort :n :k @best_score //player } into .;
</programlisting>
	</example>
      </section>
    </section></section>
  <section id="cmd">
    <title>Command Reference</title>
  <section id="apropos_command" role="command"><title>apropos</title><simplesect role="usage"><title>Usage</title><para><literal>apropos [--fulltext] [--regexp] <link linkend="exp">expression</link></literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    Print all help topics containing given expression
	    in their short description. The
	    <literal>--fulltext</literal> flag forces
	    the search to be performed over the full text
	    of help. 
	    <literal>--fulltext</literal> indicates,
	    that the given <link linkend="exp">expression</link>
	    is a regular expression instead of a literal string.
	  </para>
	</simplesect></section><section id="assign_command" role="command"><title>assign</title><simplesect role="usage"><title>Usage</title><para><literal>[assign] <link linkend="varname">$variable</link> = <link linkend="exp">expression</link></literal><literal>[assign] <link linkend="varname">$variable</link> := <link linkend="command">command</link></literal><literal>[assign]
	  <link linkend="varname">$variable</link> [-= | += | *= | /= | %= | x= | .= | ||= | &amp;&amp;= ] <link linkend="exp">expression</link></literal><literal>[assign]
	  <link linkend="varname">$variable</link> [-:= | +:= | *:= | /:= | %:= | x:= | .:= | ||:= | &amp;&amp;:= ] <link linkend="command">command</link></literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>Evaluate the expression (= assignment) 
	    or command (:= assignment) on the right 
	    side of the assignment and store the result in 
	    a given variable. Optionally a Perl operator
	    (- subtraction, + addition, * multiplication,
	    / division, % modulo, x repeat string n-times,
	    . concatenation, || logical OR, &amp;&amp; logical AND)
	    can precede the assignment, in which
	    case the variable is assigned the result
	    of applying given operator on its previous
	    value and the value of the right side of the assignment.
	  </para>
	  <example>
	    <title>Assign XPath (node-set, string), or Perl results</title>
	    <programlisting>xsh&gt; <userinput>$a=chapter/title;</userinput>
xsh&gt; <userinput>$b="hallo world";</userinput>
xsh&gt; <userinput>$c={ `uname` };</userinput>
xsh&gt; <userinput>ls $a;</userinput>
</programlisting>
	  </example>
	  <example>
	    <title>Arithmetic expressions (XPath)</title>
	    <programlisting>xsh&gt; <userinput>$a=5*100 </userinput># assign 500 to $a
xsh&gt; <userinput>$a += 20 </userinput># add 20 to $a
xsh&gt; <userinput>$a = (($a+5) div 10) </userinput>
</programlisting>
	  </example>
	  <example>
	    <title>Arithmetic expressions (Perl)</title>
	    <programlisting>xsh&gt; <userinput>$a={ 5*100 } </userinput>
xsh&gt; <userinput>$a = { join ';', split //,"hallo" } </userinput># assigns "h;a;l;l;o" to $a
</programlisting>
	  </example>
	  <example>
	    <title>Command result assignment</title>
	    <programlisting>xsh&gt; <userinput>$doc := open "file.xml" </userinput># open a document
xsh&gt; <userinput>$copies := xcopy //foo into //bar </userinput># copy elements and store the copies
xsh&gt; <userinput>$wrappers := wrap "wrapper" $copies </userinput># wrap each node from $copies to a new element "wrapper" and store the wrapping elements
</programlisting>
	  </example>
	</simplesect><simplesect role="seealso"><title>See Also</title><para><link linkend="var_command">variables</link></para></simplesect></section><section id="backups" role="command"><title>backups</title><simplesect role="usage"><title>Usage</title><para><literal>backups</literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>Enable creating backup files on save (default).</para>
	  <para>This command is equivalent to setting the
	    <literal>$BACKUPS</literal> variable to 1.</para>
	</simplesect><simplesect role="seealso"><title>See Also</title><para><link linkend="nobackups">nobackups</link></para></simplesect></section><section id="call_command" role="command"><title>call</title><simplesect role="usage"><title>Usage</title><para><literal>call <link linkend="exp">expression</link> [<link linkend="exp">expression</link> ...]</literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    Call a subroutine whose name is computed by evaluating
	    the first argument <link linkend="exp">expression</link>. All other
	    expressions are evaluated too and the results are passed
	    to the subroutine as arguments.
	  </para>
	  <para>
	    This command should only be used if the name of the subroutine
	    isn't known at the compile time. Otherwise it is recommended
	    to use a direct subroutine call of the form:
	  </para>
	  <programlisting>subroutine-name [argument1 argument2 ...]</programlisting>
	  <informalexample>
	    <programlisting>def a $arg { echo "A says" $arg }
def b $arg { echo "B says" $arg }
a "hallo!";  # call subroutine a
b "hallo!";  # call subroutine b
call { chr(ord("a")+rand(2)) } "surprise!"; # call a or b randomly 
</programlisting>
	  </informalexample>
	</simplesect><simplesect role="seealso"><title>See Also</title><para><link linkend="def">def</link>, <link linkend="return_command">return</link></para></simplesect></section><section id="c14n_command" role="command"><title>canonical</title><simplesect role="usage"><title>Usage</title><para><literal>canonical [--comments|:c] [--filter|:f <link linkend="xpath">xpath</link>] [<link linkend="exp">expression</link>]</literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    This commands prints a canonical XML representing nodes specified
	    by its argument (or current node, if no argument is given).</para>
	  <para><literal>--comments</literal> or
	    <literal>:c</literal> removes comments
	    from the resulting XML.</para>
	  <para>
	    <literal>--filter</literal> or <literal>:f</literal>
	    can be used to filter
	    the resulting XML so that it only contains
	    nodes explicitly included in the given node-set.</para>
	  <para>
	    For
	    details see <ulink url="http://www.w3.org/TR/xml-c14n">"Canonical
	      XML"</ulink> or <ulink url="http://www.w3.org/TR/xml-exc-c14n">"Exclusive XML Canonicalization"</ulink> W3C recommendations.
	  </para>
	</simplesect><simplesect role="seealso"><title>See Also</title><para><link linkend="list_command">ls</link></para></simplesect></section><section id="catalog_command" role="command"><title>catalog</title><simplesect role="usage"><title>Usage</title><para><literal>catalog <link linkend="filename">filename</link></literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    Make use of a given XML file as a catalog during all parsing
	    processes. Using a catalog may significantly speed up
	    parsing processes if many external resources are loaded
	    into the parsed documents (such as DTDs or XIncludes).
	  </para>
	</simplesect></section><section id="chxpath_command" role="command"><title>cd</title><simplesect role="usage"><title>Usage</title><para><literal>cd [<link linkend="exp">expression</link>]</literal></para></simplesect> <simplesect role="aliases">
   <title>Aliases</title>
   <para><literal> chxpath</literal></para>
 </simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    Evaluate given <link linkend="exp">expression</link>
	    to a node-list and 
	    change current context node to
	    the first node in it.
          </para>
	</simplesect></section><section id="change_namespace_prefix_command" role="command"><title>change-ns-prefix</title><simplesect role="usage"><title>Usage</title><para><literal>change-ns-prefix <link linkend="exp">expression</link> [<link linkend="exp">expression</link>]</literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    This command takes one or two arguments.  The first
	    argument is a new prefix and the second, optional,
	    argument is an old namespace prefix. It changes the prefix
	    of a namespace declaration of the context node to the new
	    value. If no old prefix is given, the change applies to a
	    declaration on the context node whose prefix equals to the
	    prefix of the context node, otherwise the command changes
	    the declaration with the given old prefix.
	  </para>
	  <para>
	    The command throws an exception if the new prefix is
	    already taken by some other declaration in the scope.
	  </para>
	</simplesect><simplesect role="seealso"><title>See Also</title><para><link linkend="change_namespace_uri_command">change-ns-uri</link>, <link linkend="set_namespace_command">set-ns</link>, <link linkend="declare_namespace_command">declare-ns</link>, <link linkend="namespaces_command">namespaces</link></para></simplesect></section><section id="change_namespace_uri_command" role="command"><title>change-ns-uri</title><simplesect role="usage"><title>Usage</title><para><literal>change-ns-uri <link linkend="exp">expression</link> [<link linkend="exp">expression</link>]</literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    This command takes one or two arguments.  The first
	    argument is a new namespace URI and the second, optional,
	    argument is a namespace prefix. It changes the URI value
	    of a namespace declaration of the context node to the new
	    value. If no prefix is given, the change applies to a
	    declaration on the context node whose prefix equals to the
	    prefix of the context node, otherwise the change applies
	    to a declaration with the given prefix.
	  </para>
	</simplesect><simplesect role="seealso"><title>See Also</title><para><link linkend="change_namespace_prefix_command">change-ns-prefix</link>, <link linkend="set_namespace_command">set-ns</link>, <link linkend="declare_namespace_command">declare-ns</link>, <link linkend="namespaces_command">namespaces</link></para></simplesect></section><section id="clone_command" role="command"><title>clone</title><simplesect role="usage"><title>Usage</title><para><literal>$doc := clone <link linkend="document">document</link></literal></para></simplesect> <simplesect role="aliases">
   <title>Aliases</title>
   <para><literal> dup</literal></para>
 </simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    Create and return a copy of a given <link linkend="document">document</link>.
	    Unless <link linkend="cdonopen">switch-to-new-documents</link>
	    configuration flag is turned off,
	    the root node of the new document
	    becomes the current node.
	  </para>
	  <para>Calling this command only makes sense if
	    either
	    <link linkend="cdonopen">switch-to-new-documents</link> is set, or
	    if the result is assigned to a variable or
	    passed to another XSH2 command using the <literal>&amp;{...}</literal>
	    syntax, since otherwise the newly 
	    created copy of the document is automatically garbage-collected and
	    destroyed.
	  </para>
	</simplesect><simplesect role="seealso"><title>See Also</title><para><link linkend="open_command">open</link>, <link linkend="close_command">close</link>, <link linkend="print_enc_command">enc</link>, <link linkend="files_command">documents</link></para></simplesect></section><section id="close_command" role="command"><title>close</title><simplesect role="usage"><title>Usage</title><para><literal>close [<link linkend="document">document</link>]</literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    Close a given <link linkend="document">document</link>
	    (or, if called with no argument,
	    the current document)
	    by trying to remove all references
	    from XSH2 variables to nodes
	    belonging to the document. If no references
	    to the document are left,
	    the garbage-collector destroys
	    the DOM tree and frees the memory it occupied
	    for later reuse (depending on architecture, this
	    may or may not give the allocated memory back
	    to the system).
	  </para>
	</simplesect></section><section id="copy_command" role="command"><title>copy</title><simplesect role="usage"><title>Usage</title><para><literal>copy [--respective|:r] <link linkend="exp">expression</link> <link linkend="loc">location</link> <link linkend="exp">expression</link></literal><literal>$results := copy [--respective|:r] <link linkend="exp">expression</link> <link linkend="loc">location</link> <link linkend="exp">expression</link></literal></para></simplesect> <simplesect role="aliases">
   <title>Aliases</title>
   <para><literal> cp</literal></para>
 </simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    Copies nodes in the first node-list <link linkend="exp">expression</link>
	    (source nodes) to the destinations determined by the 
	    the <link linkend="loc">location</link> directive
	    applied to nodes in the second node-list <link linkend="exp">expression</link>
	    (target nodes). If the source node-list contains
	    more than one node, than N'th node in the source node-list
	    is copied to the location relative to the N'th node in
	    the target node-list.
          </para>
	  <para>
	    If <literal>--respective|:r</literal> option
	    is used, then the target node-list <link linkend="exp">expression</link>
	    is evaluated in the context of the source node
	    being copied.
	  </para>
          <para>Possible values for <link linkend="loc">location</link> are:
	    <literal>after</literal>, <literal>before</literal>,
	    <literal>into</literal>, <literal>replace</literal>,
	    <literal>append</literal> and <literal>prepend</literal>. The first
	    three location directives cause making a copy of the source nodes
	    after, before, and within (as the last child-node) the target
	    nodes, respectively. 
	    If <literal>replace</literal> location directive is used,
            source node are copied before the respective
	    target nodes and target nodes are removed.
	    The <literal>append</literal> and <literal>prepend</literal>
	    location directives allow,
	    depending on the destination node type,
	    either inserting copies of the
	    source nodes as the first or last child nodes of
	    a destination element or appending/prepending
	    destination node data in case of non-element destination nodes.
	    See <link linkend="loc">location</link> argument type
	    for more detail.
	  </para>
	  <para>The command returns a node-list consisting of 
	    the copies of all source nodes created by the command.
	  </para>
	  <para>Despite the fact the command is named "copy", 
	    nodes resulting from copying
	    the source nodes may pass through certain type conversion before
	    they are inserted at the appointed destinations. This, however,
	    only happens in cases where the types of the source and target
	    nodes are not compatible with the location
	    directive. See <link linkend="loc">location</link> argument
	    type for more detail.
	  </para>
	  <para>Note that XSH2 refuses to create multiple top-level
	    elements using <literal>copy</literal>, 
	    <link linkend="move_command">move</link> and similar commands.</para>
	  <example>
	    <title>Replace living-thing elements in the document b with copies
	      of the corresponding creature elements from the document $a.</title>
	    <programlisting>xsh&gt; <userinput>copy $a//creature replace $b//living-thing</userinput></programlisting>             
	  </example>
	  <example>
	    <title>Copy every element into itself</title>
	    <programlisting>xsh&gt; <userinput>copy --respective $a//* into .</userinput></programlisting>
	    <programlisting>xsh&gt; <userinput>copy $a//* into $a//*</userinput>#same as
above</programlisting>
	  </example>
	</simplesect><simplesect role="seealso"><title>See Also</title><para><link linkend="xcopy_command">xcopy</link>, <link linkend="move_command">move</link>, <link linkend="xmove_command">xmove</link>, <link linkend="insert_command">insert</link>, <link linkend="xinsert_command">xinsert</link></para></simplesect></section><section id="count_command" role="command"><title>count</title><simplesect role="usage"><title>Usage</title><para><literal>count [--quiet|:q] <link linkend="xpath">xpath</link></literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    Calculates a given <link linkend="exp">expression</link> expression. If
	    the result is a node-list, print number of nodes in the
	    node-list.  If the <link linkend="exp">expression</link> results in a
	    boolean, numeric or literal value, print the value.
	  </para>
	  <para>
	    If <literal>--quiet</literal> or <literal>:q</literal>
	    option is used, output is suppressed and the value
	    is returned.
	  </para>
	</simplesect><simplesect role="seealso"><title>See Also</title><para><link linkend="expression_command">get</link></para></simplesect></section><section id="create_command" role="command"><title>create</title><simplesect role="usage"><title>Usage</title><para><literal>$doc := create <link linkend="nodename">nodename</link>|<link linkend="exp">expression</link></literal></para></simplesect> <simplesect role="aliases">
   <title>Aliases</title>
   <para><literal> new</literal></para>
 </simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    Returns a new document object. The
	    argument must evaluate either to 
	    a valid element name (possibly followed
	    by some attribute declarations) to be used
	    for the document element, or to 
	    a well-formed XML string.
	  </para>
	  <para>
	    Unless <link linkend="cdonopen">switch-to-new-documents</link> option is turned off,
	    this command also changes current node
	    to the new document.
	  </para>
	  <informalexample>
	    <programlisting>$scratch/&gt; <userinput>$t1 := create root</userinput>
$t1&gt; <userinput>ls $t1</userinput>
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;root/&gt;

$t1&gt; <userinput>$t2 := create "root id='r1'"</userinput>
$t2&gt; <userinput>ls $t2</userinput>
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;root id="r1"/&gt;

$t2&gt; <userinput>create "&lt;root id='r0'&gt;Just a &lt;b&gt;test&lt;/b&gt;&lt;/root&gt;"</userinput>
/&gt; <userinput>ls /</userinput>
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;root id='r0'&gt;Just a &lt;b&gt;test&lt;/b&gt;&lt;/root&gt;
</programlisting>
	  </informalexample>
	</simplesect><simplesect role="seealso"><title>See Also</title><para><link linkend="open_command">open</link>, <link linkend="clone_command">clone</link></para></simplesect></section><section id="debug" role="command"><title>debug</title><simplesect role="usage"><title>Usage</title><para><literal>debug</literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>Turn on debugging messages.</para>
	  <para>This is equivalent to setting
	    <literal>$DEBUG</literal> variable to 1.</para>
	</simplesect><simplesect role="seealso"><title>See Also</title><para><link linkend="nodebug">nodebug</link></para></simplesect></section><section id="declare_namespace_command" role="command"><title>declare-ns</title><simplesect role="usage"><title>Usage</title><para><literal>declare-ns <link linkend="exp">expression</link> <link linkend="exp">expression</link></literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    This command takes one or two arguments: prefix and URI,
	    both evaluated as names.
	    It creates a namespace declaration
	    of the form <literal>xmlns:prefix="URI"</literal>
	    on the current node.
	    The command produces an error if
	    the prefix is already declared in the scope of the current node
	    with a different namespace URI.
	  </para>
	</simplesect><simplesect role="seealso"><title>See Also</title><para><link linkend="set_namespace_command">set-ns</link>, <link linkend="change_namespace_uri_command">change-ns-uri</link>, <link linkend="change_namespace_prefix_command">change-ns-prefix</link>, <link linkend="namespaces_command">namespaces</link></para></simplesect></section><section id="def" role="command"><title>def</title><simplesect role="usage"><title>Usage</title><para><literal>def <link linkend="subname">subroutine</link> [<link linkend="varname">$variable</link> ...] <link linkend="block">command-or-block</link>
	</literal></para></simplesect> <simplesect role="aliases">
   <title>Aliases</title>
   <para><literal> define</literal></para>
 </simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    Define a new XSH2 sub-routine named <link linkend="subname">subroutine</link>.
	    The subroutine may require zero or more parameters. 
	    These are declared as a
	    whitespace-separated list of <emphasis>parametric
	    variables</emphasis>. The body of the
	    subroutine is specified as a <link linkend="block">command-or-block</link>.
	  </para>
	  <para>A sub-routine can be invoked directly by its name
	    followed by its arguments
	    just as any XSH2 command, or indirectly
	    using the <link linkend="call_command">call</link>
	    command followed by an expression evaluating
	    to the routine name and sub-routine arguments. 
	    Sub-routine arguments can be arbitrary 
	    expressions. These expressions are evaluated
	    <emphasis>prior</emphasis> the sub-routine's code
	    execution and are assigned to the sub-routine's 
	    parametric variables in the respective order.
	    The number of parameter variables in a sub-routine
	    definition and the number of arguments in a call to it
	    must match.
	    Calling a sub-routine with less or more arguments
	    than declared is a run-time error.
	  </para>
	  <para>	    
	    Parametric variables are lexical 
	    variables within the sub-routine body as if they
	    were declared with <link linkend="my_command">my</link>.
	  </para>
	  <para>
	    Note that a subroutine has to be defined before it is
	    first called (in terms of execution -- depending on the structure
	    of the program, the actual definition of the sub-routine
	    must not necessarily precede all references to it).
	  </para>
	  <informalexample>
	    <programlisting>def l3 $nodes {
  ls --depth 3 $nodes; # list given nodes upto depth 3
}
l3 //chapter;           # direct call
$subref = 'l3';
call $subref //chapter; # in-direct call
</programlisting>
	  </informalexample>
	  <example>
	    <title>Commenting and un-commenting pieces of document</title>
	    <programlisting>def comment
    $n      # nodes to move to comments
    $mark   # maybe some handy mark to recognize such comments
{
  foreach $n {
    if ( . = ../@* ) {
      echo "Warning: attribute nodes are not supported!";
    } else {
      echo "Commenting out:";
      ls --depth 0 .;
      add comment concat($mark,xsh:serialize(.)) replace .;
    }
  }
}

def uncomment $n $mark {
  foreach $n {
    if (. = ../comment()) { # is this node a comment node
      local $string = substring-after(.,"$mark");
      add chunk $string replace .;
    } else {
      echo "Warning: Ignoring non-comment node:";
      ls --depth 0 .;
    }
  }
}

# comment out all chapters with no paragraphs
comment //chapter[not(para)] "COMMENT-NOPARA";

# uncomment all comments stamped with COMMENT-NOPARA
$mark="COMMENT-NOPARA";
uncomment //comment()[starts-with(.,"$mark")] $mark;
</programlisting>
	  </example>
	</simplesect><simplesect role="seealso"><title>See Also</title><para><link linkend="call_command">call</link>, <link linkend="return_command">return</link>, <link linkend="my_command">my</link>, <link linkend="local_command">local</link></para></simplesect></section><section id="list_defs_command" role="command"><title>defs</title><simplesect role="usage"><title>Usage</title><para><literal>defs</literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>List names and parametric variables for all user-defined XSH2 subroutines.</para>
	</simplesect><simplesect role="seealso"><title>See Also</title><para><link linkend="def">def</link>, <link linkend="var_command">variables</link></para></simplesect></section><section id="do_command" role="command"><title>do</title><simplesect role="usage"><title>Usage</title><para><literal>do <link linkend="block">command-or-block</link></literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    Execute <link linkend="block">command-or-block</link>.
	    This command is probably only useful when one wants to
	    redirect output of more than one command.
	  </para>
	</simplesect><simplesect role="seealso"><title>See Also</title><para><link linkend="block">command-or-block</link></para></simplesect></section><section id="doc_info_command" role="command"><title>doc-info</title><simplesect role="usage"><title>Usage</title><para><literal>doc-info [<link linkend="document">document</link>]</literal></para></simplesect> <simplesect role="aliases">
   <title>Aliases</title>
   <para><literal> doc_info</literal></para>
 </simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    In the present implementation, this command displays
	    information provided in the <literal>&lt;?xml
	    ...?&gt;</literal> declaration of a document:
	    <literal>version, encoding, standalone</literal>, 
	    plus information about
	    level of <literal>gzip</literal> compression of the
	    original XML file and
	    the original XML file URI.
	  </para>
	</simplesect><simplesect role="seealso"><title>See Also</title><para><link linkend="set_enc_command">set-enc</link>, <link linkend="set_standalone_command">set-standalone</link></para></simplesect></section><section id="files_command" role="command"><title>documents</title><simplesect role="usage"><title>Usage</title><para><literal>documents</literal></para></simplesect> <simplesect role="aliases">
   <title>Aliases</title>
   <para><literal> files, docs</literal></para>
 </simplesect><simplesect role="description"><title>Description</title>
	  <para>Try to identify open documents and list their URIs
	    and variables that contain them.
	  </para>
	</simplesect><simplesect role="seealso"><title>See Also</title><para><link linkend="open_command">open</link>, <link linkend="close_command">close</link></para></simplesect></section><section id="list_dtd_command" role="command"><title>dtd</title><simplesect role="usage"><title>Usage</title><para><literal>dtd [<link linkend="document">document</link>]</literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    Print external or internal DTD for a given document. 
	    If used without arguments prints DTD of the current document.
	  </para>
	</simplesect><simplesect role="seealso"><title>See Also</title><para><link linkend="set_dtd_command">set-dtd</link>, <link linkend="validate_command">validate</link></para></simplesect></section><section id="edit_command" role="command"><title>edit</title><simplesect role="usage"><title>Usage</title><para><literal>edit [--editor|:E <link linkend="filename">filename</link>]
	  [--all|:A] [--noindent|:n] [--recover|:r] [--keep-blanks|:k]
	  [--allow-empty|:0] [--no-coment|:q] [--encoding|:e <link linkend="enc_string">encoding</link>] <link linkend="exp">expression</link>
	</literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>This command may be used to interactively edit parts of a XML
	    document directly in your favorite editor.
	  </para>
	  <para>
	    A given <link linkend="exp">expression</link> is evaluated to a node-list and the
	    first the first resulting node is opened
	    in an external editor as a XML fragment. 
	    When the editor exits, the (possibly modified) fragment
	    is parsed and returned to the original document. Unless
	    <literal>--no-comment</literal> (<literal>:q</literal>) flag is
	    used, the XML fragment is preceded with a XML comment specifying
	    canonical XPath of the node being edited.
	  </para>
	  <para>
	    The command returns a node-list consisting of nodes
	    that resulted from parsing the individual edits.
	  </para>
	  <para>
	    <literal>--editor</literal> or <literal>:E</literal>
	    option may be used to specify external editor command.
	    If not specified, environment variables
	    <literal>EDITOR</literal> and <literal>VISUAL</literal>
	    are tried first, then <literal>vi</literal> editor
	    is used as a fallback.
	  </para>
	  <para>
	    If <literal>--all</literal> or <literal>:A</literal>
	    flag is present, all nodes from the node-list
	    are opened in the editor, one at a time.
	  </para>
	  <para>
	    If <literal>--recover</literal> or <literal>:r</literal>
	    is specified, the parser tries to recover from possible 
	    syntax errors when parsing the resulting XML.
	  </para>
	  <para>
	    <literal>--keep-blanks</literal> or <literal>:b</literal>
	    option may be used to force the parser to
	    include ignorable white space.
	  </para>
	  <para>If the result saved
	    by the editor is empty, the interactive
	    XSH2 shell asks user to confirm this was correct.
	    This confirmation can be suppressed using
	    <literal>--allow-empty</literal> or
	    <literal>:0</literal> (zero) options.
	  </para>
	  <para>
	    The <literal>--encoding</literal> or <literal>:e</literal>
	    parameter can be used to specify character encoding to
	    use when communicating with the external editor.
	  </para>
	  <example>
	    <title>Edit all chapter elements (one by one) with emacs</title>
	    <programlisting>edit --editor 'emacs -nw' --encoding iso-8859-1 --all //chapter</programlisting>
	  </example>
	</simplesect></section><section id="edit_string_command" role="command"><title>edit-string</title><simplesect role="usage"><title>Usage</title><para><literal>edit-string [--editor|:E <link linkend="filename">filename</link>] 
	  [--encoding|:e <link linkend="enc_string">encoding</link>]
	  [--allow-empty|:0] <link linkend="exp">expression</link></literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    Evaluating given <link linkend="exp">expression</link> to a string,
	    save it in a temporary file,
	    open the file an external editor as a plain text,
	    and when the editor exits,
	    read and return the result. The <literal>--editor</literal>
	    (<literal>:E</literal>) parameter can be used
	    to provide an editor command, whereas
	    <literal>--encoding</literal> (<literal>:e</literal>)
	    can be used to specify character encoding used for
	    communication with the editor. If the result
	    is empty, the interactive XSH2 shell asks user for
	    confirmation before returning the result in order
	    to prevent data loss due to some unexpected error.
	    To suppress this feature, use <literal>--allow-empty</literal>
	    or <literal>:0</literal> flag.
	  </para>
	</simplesect></section><section id="empty_tags" role="command"><title>empty-tags</title><simplesect role="usage"><title>Usage</title><para><literal>empty-tags <link linkend="exp">expression</link></literal></para></simplesect> <simplesect role="aliases">
   <title>Aliases</title>
   <para><literal> empty_tags</literal></para>
 </simplesect><simplesect role="description"><title>Description</title>
	  <para>If the value of <link linkend="exp">expression</link> is 1
	    (non-zero), empty tags are serialized as a
	    start-tag/end-tag pair
	    (<literal>&lt;foo&gt;&lt;/foo&gt;</literal>).  This option
	    affects both <link linkend="list_command">ls</link> and <link linkend="save_command">save</link> and possibly other
	    commands. Otherwise, they are compacted into a short-tag
	    form (<literal>&lt;foo/&gt;</literal>). Default value is
	    <literal>0</literal>.
	  </para>
	  <para>This command is equivalent to setting the
	    <literal>$EMPTY_TAGS</literal> variable.</para>
	</simplesect></section><section id="print_enc_command" role="command"><title>enc</title><simplesect role="usage"><title>Usage</title><para><literal>enc [<link linkend="document">document</link>]</literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    Print the original document encoding string.
	    If no <link linkend="document">document</link> is given, the current document is
	    used.
	  </para>
	</simplesect><simplesect role="seealso"><title>See Also</title><para><link linkend="set_enc_command">set-enc</link></para></simplesect></section><section id="encoding" role="command"><title>encoding</title><simplesect role="usage"><title>Usage</title><para><literal>encoding <link linkend="enc_string">encoding</link></literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>Set the default character encoding used
	    for standard (e.g. terminal) output. This doesn't influence
	    the encoding used for saving XML documents.
	  </para>
	  <para>This command is equivalent to setting the
	    <literal>$ENCODING</literal> variable.</para>
	</simplesect><simplesect role="seealso"><title>See Also</title><para><link linkend="query_encoding">query-encoding</link></para></simplesect></section><section id="eval_command" role="command"><title>eval</title><simplesect role="usage"><title>Usage</title><para><literal>eval <link linkend="exp">expression</link></literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>NOTE: This command has very different behavior from XSH1,
	    where it used to be an alias for <link linkend="perl_command">perl</link>.
	  </para>
	  <para>This command first evaluates a given <link linkend="exp">expression</link> to obtain a string,
	    then evaluates this string as XSH2 code in the current context, returning
	    the return value of the last evaluated command.
	    This command raises
	    an exception if either <link linkend="exp">expression</link> evaluates to
	    invalid XSH2 code or if evaluating the code raises an exception.
	  </para>
	  <example>
	    <title>Evaluate "in-line" XSH snippets within a XML document</title>
	    <programlisting>foreach //inline-xsh eval .;</programlisting>
	  </example>
	</simplesect></section><section id="exec_command" role="command"><title>exec</title><simplesect role="usage"><title>Usage</title><para><literal>exec <link linkend="exp">expression</link> [<link linkend="exp">expression</link> ...]</literal></para></simplesect> <simplesect role="aliases">
   <title>Aliases</title>
   <para><literal> system</literal></para>
 </simplesect><simplesect role="description"><title>Description</title>
	  <para>This command executes given <link linkend="exp">expression</link>(s)
	    as a system command and returns the exit code.
	  </para>
	  <example>
	    <title>Count words in "hallo wold" string, then print name
	      of your machine's operating system.</title>
	    <programlisting>exec echo hallo world;             # prints hallo world
exec "echo hallo word" | wc;       # counts words in hallo world
exec uname;                        # prints operating system name
</programlisting>
	  </example>
	</simplesect></section><section id="exit_command" role="command"><title>exit</title><simplesect role="usage"><title>Usage</title><para><literal>exit [<link linkend="exp">expression</link>]</literal></para></simplesect> <simplesect role="aliases">
   <title>Aliases</title>
   <para><literal> quit</literal></para>
 </simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    Exit xsh, optionally with an exit-code
	    resulting from evaluation of a given <link linkend="exp">expression</link>.
	  </para>
	  <para>
	    WARNING: No files are saved on exit.
	  </para>
	</simplesect></section><section id="fold_command" role="command"><title>fold</title><simplesect role="usage"><title>Usage</title><para><literal>fold [--depth|:d <link linkend="exp">expression</link>] [<link linkend="exp">expression</link>]</literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    This feature is still EXPERIMENTAL and
	    may change in the future! 
	    Fold command may be used to
	    mark elements with a
	    <literal>xsh:fold</literal> attribute from the
	    <literal>http://xsh.sourceforge.net/xsh/</literal> namespace. 
	    When
	    listing the DOM tree using 
	    <literal><link linkend="list_command">ls</link> --fold <link linkend="xpath">xpath</link></literal>, 
	    elements marked in this
	    way are folded to a given depth (default is 0 = fold immediately).
	  </para>
	  <para>
	    The option <literal>--depth</literal> (<literal>:d</literal>)
	    may be used to specify the depth at which
	    subtrees of given elements are to be folded.
	  </para>
	  <para>
	    If called without arguments,
	    the command applies to the current element,
	    otherwise the <link linkend="exp">expression</link> is evaluated to the node-list
	    and folding is applied to all elements within this node-list.
	  </para>
	  <informalexample>
	    <programlisting>xsh&gt; <userinput>fold --depth 1 //chapter</userinput>
xsh&gt; <userinput>ls --fold  //chapter[1]</userinput>
&lt;chapter id="intro" xsh:fold="1"&gt;
  &lt;title&gt;...&lt;/title&gt;
  &lt;para&gt;...&lt;/para&gt;
  &lt;para&gt;...&lt;/para&gt;
&lt;/chapter&gt;
</programlisting>
	  </informalexample>
	</simplesect><simplesect role="seealso"><title>See Also</title><para><link linkend="unfold_command">unfold</link>, <link linkend="list_command">ls</link></para></simplesect></section><section id="foreach" role="command"><title>foreach</title><simplesect role="usage"><title>Usage</title><para><literal>foreach <link linkend="exp">expression</link> 
	  <link linkend="command">command</link>|<link linkend="block">command-or-block</link>
	</literal><literal>foreach [my|local] <link linkend="varname">$variable</link> in <link linkend="exp">expression</link>
	  <link linkend="command">command</link>|<link linkend="block">command-or-block</link>
	</literal></para></simplesect> <simplesect role="aliases">
   <title>Aliases</title>
   <para><literal> for</literal></para>
 </simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    Evaluate given <link linkend="exp">expression</link> to a node-list and for each
	    resulting node execute given <link linkend="command">command</link> or 
	    <link linkend="block">command-or-block</link>. If used without a loop <link linkend="varname">$variable</link>, the loop temporarily sets current node to
	    the node being processed. Otherwise, the processed node is assigned
	    to the loop variable.
	  </para>
	  <para>
	    The <link linkend="exp">expression</link> may be <link linkend="xpath">xpath</link> as well as
	    a <link linkend="perl_code">perl-code</link>.
	    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.
	  </para>
	  <example>
	    <title>Move all employee sub-elements in a company element
	      into the first staff subelement of the same company</title>
	    <programlisting>xsh&gt; <userinput>foreach //company xmove employee into staff[1];</userinput></programlisting>
	  </example>
	  <example>
	    <title>List content of all XML files in current directory</title>
	    <programlisting>xsh&gt; <userinput>foreach my $filename in { glob('*.xml') } {
  $f := open $filename;
  do_something $f; 
}</userinput>
</programlisting>
	  </example>
	</simplesect></section><section id="expression_command" role="command"><title>get</title><simplesect role="usage"><title>Usage</title><para><literal>get <link linkend="exp">expression</link></literal></para></simplesect> <simplesect role="aliases">
   <title>Aliases</title>
   <para><literal> exp, expr</literal></para>
 </simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    Calculate a given <link linkend="exp">expression</link> and return the value.
	  </para>
	</simplesect><simplesect role="seealso"><title>See Also</title><para><link linkend="count_command">count</link></para></simplesect></section><section id="hash_command" role="command"><title>hash</title><simplesect role="usage"><title>Usage</title><para><literal>$hash := hash <link linkend="exp">expression</link> <link linkend="exp">expression</link></literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    This command takes two arguments: an expression computing
	    a key from a given node (1st argument) and a node-set (2nd
	    argument). For each node in the node-set, the key value is
	    computed and the node is stored under the given key in
	    the resulting hash. For a given key, the value stored
	    in the hash table is a node-list consisting of all nodes
	    for which the 1st expression evaluated to an object
	    string-wise equal to the key. It is therefore possible to
	    index more than one node under the same key.
	  </para>
	  <para>
	    The XPath function
	    <literal>xsh:lookup(varname,key)</literal> can
            be used to retrieve values from hashes in XPath expressions.
	  </para>
	  <example>
	    <title>Index books by author</title>
	    <programlisting>my $books_by_author := hash concat(author/firstname," ",author/surname) //book;</programlisting>
	  </example>
	  <example>
	    <title>Lookup books by Jack London.</title>
	    <programlisting>ls { $books_by_author-&gt;{'Jack London'} };
ls xsh:lookup('books_by_author','Jack London');</programlisting>
	  </example>
	</simplesect><simplesect role="seealso"><title>See Also</title><para><link linkend="lookup_function">xsh:lookup</link></para></simplesect></section><section id="help_command" role="command"><title>help</title><simplesect role="usage"><title>Usage</title><para><literal>help <link linkend="command">command</link>|argument-type|xsh:xpath-function</literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>Print help on a given command, argument type or
	    XPath extension function 
	    (use <literal>xsh:</literal> as a prefix to XPath extensions
	    function names, e.g <literal>help xsh:id2</literal>).
	  </para>
	</simplesect></section><section id="if" role="command"><title>if</title><simplesect role="usage"><title>Usage</title><para><literal>if <link linkend="exp">expression</link> <link linkend="command">command</link>
	</literal><literal>
          if <link linkend="exp">expression</link>
	  <link linkend="block">command-or-block</link> [ elsif <link linkend="block">command-or-block</link> ]* [ else <link linkend="block">command-or-block</link> ]
	</literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    Executes <link linkend="block">command-or-block</link> if a given <link linkend="exp">expression</link>
	    expression evaluates to a non-emtpty node-list, true
	    boolean-value, non-zero number or non-empty literal. If
	    the first expression fails, then 
	    <literal>elsif</literal> conditions are tested (if any) and the
	    <link linkend="block">command-or-block</link> corresponding to the first one of
	    them which is true is executed. If none of the conditions is satisfied, an
	    optional <literal>else</literal> <link linkend="block">command-or-block</link> is executed.
	  </para>
	  <example>
	    <title>Display node type</title>
	    <programlisting>def node_type %n {
  foreach (%n) {
    if ( . = self::* ) { # XPath trick to check if . is an element
      echo 'element';
    } elsif ( . = ../@* ) { # XPath trick to check if . is an attribute
      echo 'attribute';
    } elsif ( . = ../processing-instruction() ) {
      echo 'pi';
    } elsif ( . = ../text() ) {
      echo 'text';
    } elsif ( . = ../comment() ) {
      echo 'comment'
    } else { # well, this should not happen, but anyway, ...
      echo 'unknown-type';
    }
  }
}
</programlisting>
	  </example>
	  <example>
	    <title>Check a environment variable</title>
	    <programlisting>if { defined($ENV{HOME}) } lcd { $ENV{HOME} }</programlisting>
	  </example>
	</simplesect></section><section id="ifinclude_command" role="command"><title>ifinclude</title><simplesect role="usage"><title>Usage</title><para><literal>ifinclude [--encoding|:e <link linkend="enc_string">encoding</link>] <link linkend="filename">filename</link></literal></para></simplesect><simplesect role="description"><title>Description</title>
	  <para>
	    Unless the file <link linkend="filename">filename</link> has already
	    been included using either <link linkend="include_command">include</link> of
	    <link linkend="ifinclude_command">ifinclude</link>, load the file and
	    execute it as a XSH2 script.
	  </para>
	  <para>Use <literal>--encoding</literal> or <literal>:e</literal>
	    parameter to specify character encoding used in
	    the included file.
	  </para>
	</simplesect><simplesect role="seealso"><title>See Also</title><para><link linkend="include_command">include</link></para></simplesect></section><section id="include_command" role="command"><title>include</title><simplesect role="usage"><