<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd">
<article>
  <title>XSH Reference</title>
  <sect1 id="intro">
    <title>XSH Language</title>
  
      <para>
	XSH acts as a command interpreter.  Individual commands must
	be separated with a semicolon. Each command may be followed by
	a pipeline redirection to capture the command's output. In the
	interactive shell, backslash may be used at the end of line to
	indicate that the command follows on the next line.
      </para>
      <para>
	A pipeline redirections may be used either to feed the
	command's output to a unix command or to store it in a XSH
	string variable.</para> <para>In the first case, the syntax is
	<literal>xsh-command | shell-command ;</literal> where
	<literal>xsh-command</literal> is any XSH
	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>sh</literal> or
	<literal>csh</literal>, on Windows systems by
	<literal>cmd</literal>).  Brackets may be used to join more shell commands (may
	depend on which shell is used).
      </para>
      <example>
	<title>Count attributes of words containing string foo in its name or value.</title>
	<programlisting>xsh&gt; <userinput>ls //words/@* | grep foo | wc</userinput></programlisting>
      </example>
      <para>
	In order to store a command's output in a string variable, the
	pipeline redirection must take the form <literal>xsh-command
	|&gt; $variable</literal> where <literal>xsh-command</literal> is
	any XSH command and <literal>$variable</literal> is any valid
	name for a string <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>
      <para>
	<literal><link linkend="help_command">help</link> command</literal>
	gives a list of all XSH commands.
      </para>
      <para>
	<literal><link linkend="help_command">help</link> type</literal> gives a
	list of all argument types.
      </para>
      <para>
	<literal><link linkend="help_command">help</link></literal> followed by
	a command or type name gives more information on the
	particular command or argument type.
      </para>
    <sect2 id="Documents">
      <title>Files/Documents</title>
      <para>
	XSH is intended to query and manipulate XML and HTML
	documents. Use one of the
	<literal>open/open-*/create</literal> commands to load an XML
	or HTML document from a local file, external URL (such as
	http:// or ftp://), string or pipe.  While loading, XSH parses
	and optionally validates (see <link linkend="validation">validation</link> and
	<link linkend="load_ext_dtd">load-ext-dtd</link>) the document. 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> quite similarly to a
	local filesystem.
      </para>
      <para>
	Every opened document is associated with an identifier (<link linkend="ID">id</link>), that is a symbolic name for the document in
	  XSH and can be used for example as a prefix of <link linkend="xpath">xpath</link>.
      </para>
      <para>
	In the current version, XSH is only able to save documents
	locally.  To store a document on any other location, use <link linkend="list_command">ls</link> command and pipe redirection to feed
	the XML representation of the document to any external program
	that is able to store it on a remote location.
      </para>
      <example>
	<title>Store XSH document DOC on a remote machine using Secure Shell</title>
	<programlisting>xsh&gt; <userinput>ls DOC:/ | ssh my.remote.org 'cat &gt; test.xml'</userinput></programlisting>
      </example>
    <sect3>
  <title>Related Argument Types and Commands</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="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 (do not save it, though)</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="files_command">documents</link></term>
      <listitem><para>display a list of open files</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="filename">filename</link></term>
      <listitem><para>file name</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="ID">id</link></term>
      <listitem><para>identifier</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="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="select_command">select</link></term>
      <listitem><para>make a given document the current one</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="cdonopen">switch-to-new-documents</link></term>
      <listitem><para>set on/off changing current document to newly
	  open/created files</para></listitem>
    </varlistentry></variablelist>
  </para>
 </sect3></sect2><sect2 id="Navigation">
      <title>Tree navigation</title>
      <para>
	With XSH, it is possible to browse <link linkend="Documents">document trees</link> as if they were a
	local filesystem, except that <link linkend="xpath">XPath</link> expressions are used instead of
	ordinary UNIX paths.</para> <para>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. Remember, that beside <link linkend="chxpath_command">cd</link> command, current node (and
	document) is silently changed by all variant of <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>
	statement.
      </para>
      <para>Documents are specified
	in a similar way as harddrives on DOS/Windows(TM) systems
	(except that their names are not limitted to one letter in
	XSH), i.e. by a prefix of the form doc: where doc is the
	<link linkend="ID">id</link> associated with the document.
      </para>
      <para>
	To mimic the filesystem navigation as closely as
	possible, XSH 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>
      <informalexample>
	<programlisting>xsh scratch:/&gt; <userinput>open docA=&quot;testA.xml&quot;</userinput>
xsh docB:/&gt; <userinput>open docB=&quot;testB.xml&quot;</userinput>
xsh&gt; <userinput>pwd</userinput>
docB:/
xsh docB:/&gt; <userinput>cd docA:/article/chapter[title='Conclusion']</userinput>
xsh docA:/article/chapter[5]&gt; <userinput>pwd</userinput>
docA:/article/chapter[5]
xsh docA:/article/chapter[5]&gt; <userinput>cd previous-sibling::chapter</userinput>
xsh docA:/article/chapter[4]&gt; <userinput>cd ..</userinput>
xsh docA:/article&gt; <userinput>select docB</userinput>
xsh docB:/&gt;
</programlisting>
      </informalexample>
    <sect3>
  <title>Related Argument Types and Commands</title>
  <para>
    <variablelist termlength="24">    <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="locate_command">locate</link></term>
      <listitem><para>show a given node location (as a cannonical XPath)</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="pwd_command">pwd</link></term>
      <listitem><para>show current context node location (as a cannonical 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="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 XHTML namespace prefix to use XPath expressions</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="registerns_xsh_command">register-xsh-namespace</link></term>
      <listitem><para>register XSH namespace prefix to use XPath expressions</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="select_command">select</link></term>
      <listitem><para>make a given document the current one</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="unregisterns_command">unregister-namespace</link></term>
      <listitem><para>unregister namespace prefix</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="xpath">xpath</link></term>
      <listitem><para>XPath expression</para></listitem>
    </varlistentry></variablelist>
  </para>
 </sect3></sect2><sect2 id="Manipulation">
      <title>Tree modification</title>
      <para>
	XSH provides mechanisms not only to browse and inspect the DOM
	tree but also to modify its content by providing commands for
	copying, moving, and deleting its nodes as well as adding
	completely new nodes or XML fragments 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 &quot;x&quot;. This &quot;x&quot;
	stands for &quot;cross&quot;, thus e.g. <link linkend="xcopy_command">xcopy</link> should be read as &quot;cross
	copy&quot;. Let's explain the difference on the
	example of <link linkend="xcopy_command">xcopy</link>.</para>
      <para>
	When you copy, you have to specify what are you copying and
	where are you copying to, so you have to specify the
	<emphasis>source</emphasis> and the
	<emphasis>target</emphasis>.  XSH 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 &quot;cross&quot; 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>xsh&gt; <userinput>create a &quot;&lt;X&gt;&lt;A/&gt;&lt;Y/&gt;&lt;A/&gt;&lt;/X&gt;&quot;;</userinput>
xsh&gt; <userinput>create b &quot;&lt;X&gt;&lt;B/&gt;&lt;C/&gt;&lt;B/&gt;&lt;C/&gt;&lt;B/&gt;&lt;/X&gt;&quot;;</userinput>
xsh&gt; <userinput>xcopy a://A replace b://B;</userinput>
xsh&gt; <userinput>copy b://C before a://A;</userinput>
xsh&gt; <userinput>ls a:/;</userinput>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;X&gt;&lt;C/&gt;&lt;A/&gt;&lt;Y/&gt;&lt;C/&gt;&lt;A/&gt;&lt;/X&gt;

xsh&gt; <userinput>ls b:/;</userinput>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&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 into the resulting node? 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? XSH 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 sofisticated 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>create doc &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>add comment $chapter_xml replace //chapter[1];</userinput>
<userinput>ls / 0;</userinput>
# OUTPUT:
&lt;?xml version=&quot;1.0&quot;?&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>add chunk $comment replace //comment()[1];</userinput>
<userinput>ls / 0;</userinput>
# OUTPUT:
&lt;?xml version=&quot;1.0&quot;?&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>
    <sect3>
  <title>Related Argument Types and Commands</title>
  <para>
    <variablelist termlength="16">    <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="expression">expression</link></term>
      <listitem><para>string-like expression</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>quickly modify node value/data using Perl code</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_enc_command">set-enc</link></term>
      <listitem><para>set document's charset (encoding)</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="strip_ws_command">strip-whitespace</link></term>
      <listitem><para>strip leading and trailing whitespace</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>transform 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>
 </sect3></sect2><sect2 id="Flow">
      <title>Flow control</title>
      <para>
	What a scripting language XSH would be had it not some kind of
	conditional statements, loops and other stuff that
	influences the way in which XSH commands are processed.
      </para>
      <para>
	Most notable XSH's feature in this area is that some of the
	basic flow control statements, namely <link linkend="if">if</link>,
	<link linkend="unless">unless</link>, <link linkend="while">while</link> and <link linkend="foreach">foreach</link> have two variants, an XPath-based
	one and a Perl-based one.  The XPath-based variant uses <link linkend="xpath">xpath</link> expressions to specify the condition or
	node-lists to iterate, while the other one utilizes <link linkend="perl_code">perl-code</link> for this purpose. See 
	descriptions of the individual statements for more detail.
      </para>
    <sect3>
  <title>Related Argument Types and Commands</title>
  <para>
    <variablelist termlength="13">    <varlistentry>
      <term><link linkend="call_command">call</link></term>
      <listitem><para>call user-defined routine (macro)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="block">command-block</link></term>
      <listitem><para>single XSH command or a block of XSH commands</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="def">def</link></term>
      <listitem><para>sub-routine (macro) declaration</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="exit_command">exit</link></term>
      <listitem><para>exit XSH shell</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="foreach">foreach</link></term>
      <listitem><para>loop iterating over a node-list or 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 XSH source in current position</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="include_command">include</link></term>
      <listitem><para>include another XSH 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 (macro)</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>
 </sect3></sect2><sect2 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>), XSH provides commands to obtain other
	information related to open documents as well as the XSH
	interpreter itself. These commands are listed bellow.
      </para>
    <sect3>
  <title>Related Commands</title>
  <para>
    <variablelist termlength="10">    <varlistentry>
      <term><link linkend="count_command">count</link></term>
      <listitem><para>calculate a given XPath expression and enumerate node-lists</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="list_defs_command">defs</link></term>
      <listitem><para>list all user-defined routines (macros)</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="files_command">documents</link></term>
      <listitem><para>display a list of open files</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="print_enc_command">enc</link></term>
      <listitem><para>show document's original character encoding</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="help_command">help</link></term>
      <listitem><para>on-line documentation</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="locate_command">locate</link></term>
      <listitem><para>show a given node location (as a cannonical XPath)</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="namespaces_command">namespaces</link></term>
      <listitem><para>List namespaces in current scope (or in scope of
	  given nodes)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="options_command">options</link></term>
      <listitem><para>list current settings using XSH syntax</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="print_command">print</link></term>
      <listitem><para>print given stuff on standard console output</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="pwd_command">pwd</link></term>
      <listitem><para>show current context node location (as a cannonical XPath)</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="valid_command">valid</link></term>
      <listitem><para>check if the document is valid (according to a
	DTD, RelaxNG, or XSD schemas)</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>display a list of defined variables</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="version">version</link></term>
      <listitem><para>show version information</para></listitem>
    </varlistentry></variablelist>
  </para>
 </sect3></sect2><sect2 id="Argtypes">
      <title>Argument Types</title>
      <para>
	XSH commands accept different types of arguments, such as
	usual strings (<link linkend="expression">expression</link>) or
	<link linkend="xpath">XPath expressions</link>. 
	Notably, these two types and types based on them
	support string variable interpolation. See documentation
	of the individual types for more information.
      </para>
    <sect3>
  <title>Related Argument Types</title>
  <para>
    <variablelist termlength="13">    <varlistentry>
      <term><link linkend="block">command-block</link></term>
      <listitem><para>single XSH command or a block of XSH commands</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="enc_string">enc-string</link></term>
      <listitem><para>character encoding (codepage) identifier</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="expression">expression</link></term>
      <listitem><para>string-like expression</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="filename">filename</link></term>
      <listitem><para>file name</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="ID">id</link></term>
      <listitem><para>identifier</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="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="xpath">xpath</link></term>
      <listitem><para>XPath expression</para></listitem>
    </varlistentry></variablelist>
  </para>
 </sect3></sect2><sect2 id="Variables">
      <title>Variables</title>
      <para>
	In the current version, XSH supports two types of variables:
	string (scalar) variables and node-list variables.  Perl
	programmers that might miss some other kinds of variables
	(arrays or hashes) may use the support for <link linkend="Perl_shell">interacting
	with Perl</link> to access these types (see some examples below).
      </para>
      <para>
	These two kinds of variables differ syntactically in the
	prefix: string variables are prefixed with a dollar sign
	(<literal>$</literal>) while node-list variables are prefixed
	with a percent sign (<literal>%</literal>).
      </para>
      <sect3 id="string_variables">
	<title>String Variables</title>
	<para>
	  Every string variable name consists of a dollar sign
	  (<literal>$</literal>) prefix and an <link linkend="ID">id</link>,
	  that has to be unique among other scalar variables, e.g.
	  <literal>$variable</literal>.  Values are
	  assigned to variables either by simple <link linkend="assign_command">assignments</link> of the form
	  <literal>$variable = <link linkend="xpath">xpath</link></literal> or by
	  capturing the output of some command with a variable
	  redirection of the form <literal>command |&gt;
	  $variable</literal>.
	</para>
	<para>
	  String variables may be used in <link linkend="expression">string expressions</link>, <link linkend="xpath">XPath expressions</link>, or even in
	  perl-code as $<link linkend="ID">id</link> or ${<link linkend="ID">id</link>}. In the first two cases, variables act as
	  macros in the sense that all variables occurences are
	  replaced by the corresponding values before the expression
	  itself is evaluated.
	</para>
	<para>
	  To display current value of a variable, use the 
	  <link linkend="print_command">print</link> command,
	  <link linkend="var_command">variables</link> command or
	  simply the variable name:
	</para>
	<informalexample>
	  <programlisting>xsh&gt; <userinput>$b=&quot;chapter&quot;;</userinput>
xsh&gt; <userinput>$file=&quot;${b}s.xml&quot;;</userinput>
xsh&gt; <userinput>open f=$file;</userinput>
xsh&gt; <userinput>ls //$b[count(descendant::para)&gt;10]</userinput>
xsh&gt; <userinput>print $b</userinput>
chapter
xsh&gt; <userinput>$b</userinput>
$b='chapter';
xsh&gt; <userinput>variables</userinput>
$a='chapters.xml';
$b='chapter';
</programlisting>
	</informalexample>
      </sect3>
      <sect3 id="nodelist_variables">
	<title>Node-list Variables</title>
	<para>
	  Every string variable name consists of a percent sign
	  (<literal>%</literal>) prefix and an <link linkend="ID">id</link>,
	  that has to be unique among other node-list variables, e.g.
	  <literal>%variable</literal>.
	</para>
	<para>
	  Node-list variables can be used to store lists of nodes that
	  result from evaluating an XPath. This is especially useful
	  when several changes are performed on some set of nodes and
	  evaluating the XPath expression repeatedly would take too
	  long. Other important use is to remember a node that would
	  otherwise be extremely hard or even impossible to locate by
	  XPath expressions after some changes to the tree structure
	  are made, since such an XPath cannot be predicted in
	  advance.
	</para>
	<para>
	  Although node-list variables act just like XPath expressions
	  that would result in the same node-list, for implementation
	  reasons it is not possible to use node-list variables as
	  parts of complex XPath expressions except for one case. They
	  <emphasis>may be only used at the very beginning of an XPath
	  expression</emphasis>. So while constructions such as

	  <literal>%creatures[4]</literal>,
	  <literal>%creatures[@race='elf']</literal>, or
	  <literal>%creatures/parents/father</literal> <emphasis>do
	  work</emphasis> as expected,
	  <literal>string(%creatures[2]/@name)</literal>
	  <literal>//creature[%creatures[2]/@name=@name]</literal>, or
	  <literal>%creatures[@race='elf'][2]</literal> <emphasis>do
	  not</emphasis>.  In the first two cases it is because
	  node-list variables cannot be evaluated in the middle of an
	  XPath expression. The third case fails because this
	  construction actually translates into a sequence of
	  evaluations of <literal>self::*[@race='elf'][2]</literal>
	  for each node in the <literal>%creatures</literal>
	  node-list, which is not equivallent to the intended
	  expression as the <literal>[2]</literal> filter does not
	  apply to the whole result of
	  <literal>%creatures[@race='elf']</literal> at once but
	  rather to the partial results.</para> <para>Fortunatelly, it
	  is usually possible to work around these unsupported
	  constructions quite easily.  This is typically done by
	  introducing some more variables as well as using the <link linkend="foreach">foreach</link> statement.  The following
	  example should provide some idea on how to do this:
	</para>
	<informalexample>
	  <programlisting># work around for $name=string(%creatures[2]/@name)
xsh&gt; <userinput>foreach %creatures[2] $name=string(@name)</userinput>
# work around for ls //creature[%creatures[2]/@name=@name]
xsh&gt; <userinput>ls //creature[$name=@name]</userinput>
# work around for ls %creatures[@race='elf'][2]
xsh&gt; <userinput>%elves = %creatures[@race='elf']</userinput>
xsh&gt; <userinput>ls %elves[2]</userinput>
</programlisting>
	</informalexample>
	<para>
	  Remember, that when a node is deleted from a tree it is at
	  the same time removed from all node-lists it occurs in.
	  Note also, that unlike string variables, node-list variables
	  can not be (and are not intended to be) directly accessed
	  from Perl code.
	</para>
      </sect3>
      <sect3 id="other_types">
	<title>Accessing Perl Variables</title>
	<para>
	  All XSH string variables are usual Perl scalar variables
	  from the <literal>XML::XSH::Map</literal> namespace, which
	  is the default namespace for any Perl code evaluated from
	  XSH. Thus it is possible to arbitrarily intermix XSH 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>eval { $b=&quot;CHAPTER 1: &quot;.uc($a); }</userinput>
xsh&gt; <userinput>print $b</userinput>
CHAPTER 1: INTRODUCTION
</programlisting>
	</informalexample>
	<para>
	  If needed, it is, however, possible to use any other type of Perl
	  variables by means of evaluating a corresponding
	  perl code. The following example demonstrates using Perl
	  hashes to collect and print some simple racial statistics
	  about the population of Middle-Earth:
	</para>
	<informalexample>
	  <programlisting>
foreach a:/middle-earth/creature { 
  $race=string(@race);
  eval { $races{$race}++ };
}
print &quot;Middle-Earth Population (race/number of creatures)&quot;
eval { 
  echo map &quot;$_/$races{$_}\n&quot;,
    sort ($a cmp $b), keys(%races); 
};
</programlisting>
	</informalexample>
      </sect3>
    <sect3>
  <title>Related Argument Types and Commands</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="expression">expression</link></term>
      <listitem><para>string-like expression</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="ID">id</link></term>
      <listitem><para>identifier</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="xpath">xpath</link></term>
      <listitem><para>XPath expression</para></listitem>
    </varlistentry></variablelist>
  </para>
 </sect3></sect2><sect2 id="Configuration">
      <title>Options</title> <para>The following commands are used to
	modify the default behaviour of the XML parser or XSH itself.
	Some of the commands are 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 counterpart (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
	misconsistency is due to historical reasons.
      </para>
      <para>
	The <link linkend="encoding">encoding</link> and <link linkend="query_encoding">query-encoding</link> options allow to specify character
	encoding that should be expected from user as well as the
	encoding to be used by XSH on output. This is particularly
	useful when you work with UTF-8 encoded documents on a console
	which supports only 8-bit characters.
      </para>
      <para>
	The <link linkend="options_command">options</link> command
	displays current settings by means of XSH commands.
	Thus it can not only be used to review current values, but
	also to store them future use, e.g. in ~/.xshrc file.
      </para>
      <informalexample>
	<programlisting>xsh&gt; <userinput>options | cat &gt; ~/.xshrc</userinput></programlisting>
      </informalexample>
    <sect3>
  <title>Related Argument Types and Commands</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="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">enc-string</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">options</link></term>
      <listitem><para>list current settings using XSH syntax</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="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 XSH source files and terminal input</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="quiet">quiet</link></term>
      <listitem><para>turn off many XSH 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 XHTML namespace prefix to use XPath expressions</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="registerns_xsh_command">register-xsh-namespace</link></term>
      <listitem><para>register XSH namespace prefix to use XPath expressions</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="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="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 XSH 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></variablelist>
  </para>
 </sect3></sect2><sect2 id="Perl_shell">
      <title>Interacting with Perl and Shell</title>
      <para>
	To allow more complex tasks to be achieved, XSH provides ways
	for interaction with the Perl programming language and the
	system shell.
      </para>
      <sect3 id="binding_perl">
	<title>Calling Perl</title>
	<para>
	  Perl is a language optimized for scanning arbitrary text
	  files, extracting information from those text files, and
	  printing reports based on that information.  It's also a
	  good language for many system management tasks.  The
	  language is intended to be practical (easy to use,
	  efficient, and complete). XSH itself is written in Perl,
	  so it is extremely easy to support this language as an
	  extension to XSH.
	</para>
	<para>
	  Perl <link linkend="perl_code">expressions or blocks of
	  code</link> can either be simply evaluated with the <link linkend="eval_command">perl</link> command, used to do quick changes
	  to nodes of the DOM tree (see <link linkend="map_command">map</link>
	  command), used to provide list of strings to iterate over in
	  a <link linkend="foreach">foreach</link> loop, or to specify more
	  complex 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 XSH internals and the evaluated
	  perl code, XSH runs such code in the context of a special
	  namespace <literal>XML::XSH::Map</literal>. As described in
	  the section <link linkend="Variables">Variables</link>, XSH 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::XSH::Map</literal> namespace.</para>
	<para>
	  The interaction between XSH and Perl actually works also the
	  other way round, so that you may call back XSH from the
	  evaluated Perl code.  For this, Perl function
	  <literal>xsh</literal> is defined in the
	  <literal>XML::XSH::Map</literal> namespace.  All parameters
	  passed to this function are interpreted as XSH commands. To
	  simplify evaluation of XPath expressions, another three
	  functions:  The first one, named <literal>count</literal>,
	  returns the same value as would be printed by <link linkend="count_command">count</link> command in XSH on the same XPath
	  expression. The second function, named <literal>literal</literal>,
	  returns the result of XPath evaluation as if the whole
	  expression was wrapped with the XPath
	  <literal>string()</literal> function. In other words,
	  <literal>literal('doc:expression')</literal> returns the same
	  value as <literal>count('doc:string(expression)')</literal>.
	  The third function, named <literal>xml_list</literal>, returns
	  the result of the XPath search as a XML string which is
	  equivallent to the output of a <link linkend="list_command">ls</link> on the same XPath expression
	  (without indentation and without folding or any other
	  limitation on the depth of the listing).
	</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 $file, &quot;hobbits.txt&quot;';
  perl '@hobbits=&lt;$file&gt;';
  perl 'close $file';
  foreach { @hobbits } {
    insert element &quot;&lt;creature name='$__' race='hobbit'&gt;&quot;
      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, &quot;hobbits.txt&quot;;
    foreach (&lt;$file&gt;) {
      xsh(qq{insert element &quot;&lt;creature name='$_' race='hobbit'&gt;&quot;
        into m:/middle-earth/creatures});
    }
    close $file;
  }
};</programlisting>
	</example>
      </sect3>
      <sect3 id="binding_perl_xpathextensions">
        <title>Writing your own XPath extension functions in Perl</title>
        <para>
	  XSH allows the user to extend the set of XPath functions by
	  providing an extension function 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 either simple scalars or
	  <literal>XML::LibXML::NodeList</literal> objects, depending
	  on the types of the XPath arguments.  The implementation is
	  responsible for checking the argument number and types.  The
	  implementation may use arbitrary
	  <literal>XML::LibXML</literal> methods to process the
	  arguments and return the result.
	  (<literal>XML::LibXML</literal> perl module documentation
	  can be found for example at <ulink url="http://search.cpan.org/author/PHISH/XML-LibXML-1.54/LibXML.pm">http://search.cpan.org/author/PHISH/XML-LibXML-1.54/LibXML.pm</ulink>).
	</para>
	<para>
	  The implementation SHOULD NOT, however, MODIFY the document.
	  Doing so could not only confuse the XPath engine but result
	  in an critical error (such as segmentation fault).
	</para>
	<para>
	  Calling XSH commands from extension function implementations
	  is not currently allowed.
	</para>
	<para>
	  The perl code must return a single value, which can be of
	  one of the following types: a 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 (resulat is a float),
	  <literal>XML::LibXML::Node</literal> (or derived) object
	  reference (result is a nodeset consisting of a single node),
	  or <literal>XML::LibXML::NodeList</literal> (result is a
	  nodeset). For convenience, simple (non-blessed) array
	  references consisting of
	  <literal>XML::LibXML::Node</literal> objects can also be
	  used for a nodeset result instead of a
	  <literal>XML::LibXML::NodeList</literal>.
        </para>
      </sect3>
      <sect3 id="binding_shell">
	<title>Calling the System Shell</title>
	<para>
	  In the interactive mode, XSH interprets all lines starting
	  with a exclamation mark (<literal>!</literal>) as shell
	  commands and invokes the system shell to interpret them
	  (this is to mimic FTP 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
	  XSH construction, use the <link linkend="exec_command">exec</link>
	  command.
	</para>
	<para>
	  Since UNIX shell commands are very powerful tool for
	  processing textual data, XSH supports direct redirection of
	  XSH 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 XSH command. Since semicolon (<literal>;</literal>)
	  is used in XSH 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 &quot;funny&quot; | more</programlisting>
	</example>
      </sect3>
    <sect3>
  <title>Related Argument Types and Commands</title>
  <para>
    <variablelist termlength="10">    <varlistentry>
      <term><link linkend="exec_command">exec</link></term>
      <listitem><para>execute a shell command</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="expression">expression</link></term>
      <listitem><para>string-like expression</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="cd_command">lcd</link></term>
      <listitem><para>change system working directory</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="map_command">map</link></term>
      <listitem><para>quickly modify node value/data using Perl code</para></listitem>
    </varlistentry>    <varlistentry>
      <term><link linkend="eval_command">perl</link></term>
      <listitem><para>evaluate in-line Perl code</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="rename_command">rename</link></term>
      <listitem><para>quickly rename nodes with in-line Perl code</para></listitem>
    </varlistentry></variablelist>
  </para>
 </sect3></sect2></sect1>
  <sect1 id="command">
    <title>Command Reference</title>
  <sect2 id="assign_command"><title>assign</title><simplesect><title>Usage</title><para><literal>assign $<link linkend="ID">id</link>=<link linkend="xpath">xpath</link>
	</literal></para><para><literal>
	  $<link linkend="ID">id</link>=<link linkend="xpath">xpath</link></literal></para><para><literal>
	  assign %<link linkend="ID">id</link>=<link linkend="xpath">xpath</link></literal></para><para><literal>
	  %<link linkend="ID">id</link>=<link linkend="xpath">xpath</link></literal></para></simplesect><simplesect><title>Description</title>
	  <para> 
	    In the first two cases (where dollar sign appears)
	    store the result of evaluation of the <link linkend="xpath">xpath</link> in a variable named $<link linkend="ID">id</link>.
	    In this case, <link linkend="xpath">xpath</link> is evaluated in a
	    simmilar way as in the case of the <link linkend="count_command">count</link>: if it results in a literal
	    value this value is used. If it results in a node-list,
	    number of nodes occuring in that node-list is used.
	    Use the <literal>string()</literal> XPath function
	    to obtain a literal values in these cases.
	  </para>
	  <example>
	    <title>String expressions</title>
	    <programlisting>xsh&gt; <userinput>$a=string(chapter/title)</userinput>
xsh&gt; <userinput>$b=&quot;hallo world&quot;</userinput>
</programlisting>
	  </example>
	  <example>
	    <title>Arithmetic expressions</title>
	    <programlisting>xsh&gt; <userinput>$a=5*100</userinput>
xsh&gt; <userinput>$a</userinput>
$a=500
xsh&gt; <userinput>$a=(($a+5) div 10)</userinput>
xsh&gt; <userinput>$a</userinput>
$a=50.5
</programlisting>
	  </example>
	  <example>
	    <title>Counting nodes</title>
	    <programlisting>xsh&gt; <userinput>$a=//chapter</userinput>
xsh&gt; <userinput>$a</userinput>
$a=10
xsh&gt; <userinput>%chapters=//chapter</userinput>
xsh&gt; <userinput>$a=%chapters</userinput>
xsh&gt; <userinput>$a</userinput>
$a=10
</programlisting>
	  </example>
	  <example>
	    <title>Some caveats of counting node-lists</title>
	    <programlisting>xsh&gt; <userinput>ls ./creature</userinput>
&lt;creature race='hobbit' name=&quot;Bilbo&quot;/&gt;

## WRONG (@name results in a singleton node-list) !!!
xsh&gt; <userinput>$name=@name</userinput>
xsh&gt; <userinput>$name</userinput>
$name=1

## CORRECT (use string() function)
xsh&gt; <userinput>$name=string(@name)</userinput>
xsh&gt; <userinput>$name</userinput>
$name=Bilbo
</programlisting>
	  </example>
	  <para> 
	    In the other two cases (where percent sign appears)
	    find all nodes matching a given <link linkend="xpath">xpath</link>
	    and store the resulting node-list in the variable
	    named %<link linkend="ID">id</link>. The variable may be later used
	    instead of an XPath expression.
	  </para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="var_command">variables</link></para></simplesect></sect2><sect2 id="backups"><title>backups</title><simplesect><title>Usage</title><para><literal>backups</literal></para></simplesect><simplesect><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><title>See Also</title><para><link linkend="nobackups">nobackups</link></para></simplesect></sect2><sect2 id="call_command"><title>call</title><simplesect><title>Usage</title><para><literal>call <link linkend="ID">id</link> [<link linkend="xpath">xpath</link> | <link linkend="expression">expression</link>]*</literal></para></simplesect><simplesect><title>Description</title>
	  <para>
	    Call an XSH subroutine named <link linkend="ID">id</link> previously
	    created using def. If the subroutine requires some
	    paramters, these have to be specified after the <link linkend="ID">id</link>. Node-list parameters are given by means of
	    <link linkend="xpath">xpath</link> expressions. String parameters
	    have to be string <link linkend="expression">expression</link>s.
	  </para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="def">def</link>, <link linkend="return_command">return</link></para></simplesect></sect2><sect2 id="catalog_command"><title>catalog</title><simplesect><title>Usage</title><para><literal>catalog <link linkend="expression">expression</link></literal></para></simplesect><simplesect><title>Description</title>
	  <para>
	    Will use a given catalog file as a catalog during all parsing
	    processes. Using a catalog will significantly speed up
	    parsing processes if many external ressources are loaded
	    into the parsed documents (such as DTDs or XIncludes)
	  </para>
	</simplesect></sect2><sect2 id="chxpath_command"><title>cd</title><simplesect><title>Usage</title><para><literal>cd [<link linkend="xpath">xpath</link>]</literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> chxpath</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>Change current context node (and current document) to
	    the first node matching a given <link linkend="xpath">xpath</link>
	    argument.
          </para>
	</simplesect></sect2><sect2 id="clone_command"><title>clone</title><simplesect><title>Usage</title><para><literal>clone <link linkend="ID">id</link>=<link linkend="ID">id</link></literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> dup</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>
	    Make a copy of the document identified by the <link linkend="ID">id</link> following the equal sign assigning it the
	    identifier of the first <link linkend="ID">id</link>.
	  </para>
	</simplesect><simplesect><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></sect2><sect2 id="close_command"><title>close</title><simplesect><title>Usage</title><para><literal>close [<link linkend="ID">id</link>]</literal></para></simplesect><simplesect><title>Description</title>
	  <para>
	    Close the document identified by <link linkend="ID">id</link>,
	    removing its parse-tree from memory (note also that all
	    nodes belonging to the document are removed from all
	    nodelists they appear in). If <link linkend="ID">id</link> is
	    omitted, the command closes the current document.
	  </para>
	</simplesect></sect2><sect2 id="copy_command"><title>copy</title><simplesect><title>Usage</title><para><literal>copy <link linkend="xpath">xpath</link> <link linkend="loc">location</link> <link linkend="xpath">xpath</link></literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> cp</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>
	    Copies nodes matching the first <link linkend="xpath">xpath</link> to
	    the destinations determined by the <link linkend="loc">location</link> directive relative to the second <link linkend="xpath">xpath</link>. If more than one node matches the first
	    <link linkend="xpath">xpath</link> than it is copied to the position
	    relative to the corresponding node matched by the second
	    <link linkend="xpath">xpath</link> according to the order in which are
	    nodes matched. Thus, the n'th node matching the first <link linkend="xpath">xpath</link> is copied to the location relative to the
	    n'th node matching the second <link linkend="xpath">xpath</link>.
          </para>
          <para>The
	    possible values for <link linkend="loc">location</link> are: after,
	    before, into, replace and cause copying the source nodes
	    after, before, into (as the last child-node).  the
	    destination nodes. If replace <link linkend="loc">location</link> is
	    used, the source node is copied before the destination node
	    and the destination node is removed.
	  </para>
	  <para>
	    Some kind of type conversion is used when the types of the
	    source and destination nodes are not equal.  Thus, text,
	    cdata, comment or processing instruction node data prepend,
	    append or replace value of a destination attribute when
	    copied before,after/into or instead (replace) an attribute,
	    and vice versa.
	  </para>
	  <para>
	    Attributes may be copied after, before or into some other
	    attribute to append, prepend or replace the destination
	    attribute value. They may also replace the destination
	    attribute completely (both its name and value).
	    To copy an attribute from one element to another,
	    simply copy the attribute node into the destination element.
	  </para>
	  <para>
	    Elements may be copied into other elements (which results in
	    appending the child-list of the destination element), or
	    before, after or instead (replace) other nodes of any type
	    except attributes.
	  </para>
	  <example>
	    <title>Replace living-thing elements in the document b with
	      the coresponding creature elements of the document a.</title>
	    <programlisting>xsh&gt; <userinput>copy a://creature replace b://living-thing</userinput></programlisting>             
	  </example>
	</simplesect></sect2><sect2 id="count_command"><title>count</title><simplesect><title>Usage</title><para><literal>count <link linkend="xpath">xpath</link></literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> print_value, get</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>
	    Calculate a given <link linkend="xpath">xpath</link> expression. If
	    the result is a node-list, return number of nodes in the
	    node-list.  If the <link linkend="xpath">xpath</link> results in a
	    boolean, numeric or literal value, return the value.
	  </para>
	</simplesect></sect2><sect2 id="create_command"><title>create</title><simplesect><title>Usage</title><para><literal>create <link linkend="ID">id</link> <link linkend="expression">expression</link></literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> new</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>
	    Create a new document using <link linkend="expression">expression</link> to
	    form the root element and associate it with a given
	    identifier.
	  </para>
	  <informalexample>
	    <programlisting>
xsh&gt; <userinput>create t1 root</userinput>
xsh&gt; <userinput>ls /</userinput>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;root/&gt;

xsh&gt; <userinput>create t2 &quot;&lt;root id='r0'&gt;Just a &lt;b&gt;test&lt;/b&gt;&lt;/root&gt;&quot;</userinput>
xsh&gt; <userinput>ls /</userinput>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;root id='r0'&gt;Just a &lt;b&gt;test&lt;/b&gt;&lt;/root&gt;
xsh&gt; <userinput>files</userinput>
scratch = new_document.xml
t1 = new_document1.xml
t2 = new_document2.xml
</programlisting>
	  </informalexample>
	</simplesect><simplesect><title>See Also</title><para><link linkend="open_command">open</link>, <link linkend="clone_command">clone</link></para></simplesect></sect2><sect2 id="debug"><title>debug</title><simplesect><title>Usage</title><para><literal>debug</literal></para></simplesect><simplesect><title>Description</title>
	  <para>Turn on debugging messages.</para>
	  <para>This is equivalent to setting
	    <literal>$DEBUG</literal> variable to 1.</para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="nodebug">nodebug</link></para></simplesect></sect2><sect2 id="def"><title>def</title><simplesect><title>Usage</title><para><literal>def <link linkend="ID">id</link> [$<link linkend="ID">id</link> | %<link linkend="ID">id</link>]* <link linkend="block">command-block</link>
         or
	def <link linkend="ID">id</link> [$<link linkend="ID">id</link> | %<link linkend="ID">id</link>]*;
	</literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> define</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>
	    Define a new XSH subroutine named <link linkend="ID">id</link>.
	    The subroutine may require zero or more parameters of
	    nodelist or string type. These are declared as a
	    whitespace-separated list of (so called) <emphasis>parametric
	    variables</emphasis> (of nodelist or string type). The body of the
	    subroutine is specified as a <link linkend="block">command-block</link>.
	    Note, that all subroutine declarations are processed
	    during the parsing and not at run-time, so it does not
	    matter where the subroutine is defined.
	  </para>
	  <para>The routine can be later invoked using the <link linkend="call_command">call</link> command followed by the routine
	    name and parameters. Nodelist parameters must be given
	    as an XPath expressions, and are evaluated just before the
	    subroutine's body is executed. String parameters must be
	    given as (string) <link linkend="expression">expression</link>s.
	    Resulting node-lists/strings are stored into the
	    parametric variables before the body is executed. These
	    variables are local to the subroutine's call tree (see also the <link linkend="local_command">local</link> command). If there is a global
	    variable using the same name as some parametric variable,
	    the original value of the global variable is replaced with
	    the value of the parametric variable for the time of the
	    subroutine's run-time.
	  </para>
	  <para>
	    Note that subroutine has to be declared before it is
	    called with <link linkend="call_command">call</link>. If you cannot
	    do so, e.g. if you want to call a subroutine recursively,
	    you have to pre-declare the subroutine using a
	    <literal>def</literal> with no <link linkend="block">command-block</link>.
	    There may be only one full declaration (and possibly one
	    pre-declaration) of a subroutine for one <link linkend="ID">id</link> and the declaration and pre-declaration has
	    to define the same number of arguments and their types
	    must match.
	  </para>
	  <informalexample>
	    <programlisting>def l3 %v {
  ls %v 3; # list given nodes upto depth 3
}
call l3 //chapter;
</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 &quot;Warning: attribute nodes are not supported!&quot;;
    } else {
      echo &quot;Commenting out:&quot;;
      ls .;
      local $node = &quot;&quot;;
      ls . |&gt; $node;
      add comment &quot;$mark$node&quot; replace .;
    }
  }
}

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


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

# uncomment all comments (may not always be valid!)
$mark=&quot;COMMENT-NOPARA&quot;;
call uncomment //comment()[starts-with(.,&quot;$mark&quot;)] $mark;
</programlisting>
	  </example>
	</simplesect><simplesect><title>See Also</title><para><link linkend="call_command">call</link>, <link linkend="return_command">return</link>, <link linkend="local_command">local</link></para></simplesect></sect2><sect2 id="list_defs_command"><title>defs</title><simplesect><title>Usage</title><para><literal>defs</literal></para></simplesect><simplesect><title>Description</title>
	  <para>List names and parametric variables for all defined XSH routines.</para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="def">def</link>, <link linkend="var_command">variables</link></para></simplesect></sect2><sect2 id="doc_info_command"><title>doc-info</title><simplesect><title>Usage</title><para><literal>doc-info [<link linkend="expression">expression</link>]</literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> doc_info</literal></para>
 </simplesect><simplesect><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</literal>, <literal>encoding</literal>,
	    <literal>standalone</literal>, plus information about
	    level of <literal>gzip</literal> compression of the
	    original XML file.
	  </para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="set_enc_command">set-enc</link>, <link linkend="set_standalone_command">set-standalone</link></para></simplesect></sect2><sect2 id="files_command"><title>documents</title><simplesect><title>Usage</title><para><literal>files</literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> files, docs</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>List open files and their identifiers.</para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="open_command">open</link>, <link linkend="close_command">close</link></para></simplesect></sect2><sect2 id="list_dtd_command"><title>dtd</title><simplesect><title>Usage</title><para><literal>dtd [<link linkend="ID">id</link>]</literal></para></simplesect><simplesect><title>Description</title>
	  <para>
	    Print external or internal DTD for a given document.  If
	    no document identifier is given, the current document is used.
	  </para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="valid_command">valid</link>, <link linkend="validate_command">validate</link></para></simplesect></sect2><sect2 id="empty_tags"><title>empty-tags</title><simplesect><title>Usage</title><para><literal>empty-tags <link linkend="expression">expression</link></literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> empty_tags</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>If the value of <link linkend="expression">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></sect2><sect2 id="print_enc_command"><title>enc</title><simplesect><title>Usage</title><para><literal>enc [<link linkend="ID">id</link>]</literal></para></simplesect><simplesect><title>Description</title>
	  <para>
	    Print the original document encoding string.
	    If no document identifier is given, the current document is
	    used.
	  </para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="set_enc_command">set-enc</link></para></simplesect></sect2><sect2 id="encoding"><title>encoding</title><simplesect><title>Usage</title><para><literal>encoding <link linkend="enc_string">enc-string</link></literal></para></simplesect><simplesect><title>Description</title>
	  <para>Set the default output character encoding.</para>
	  <para>This command is equivalent to setting the
	    <literal>$ENCODING</literal> variable.</para>
	</simplesect></sect2><sect2 id="exec_command"><title>exec</title><simplesect><title>Usage</title><para><literal>exec <link linkend="expression">expression</link> [<link linkend="expression">expression</link> ...]</literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> system</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>execute the system command(s) in <link linkend="expression">expression</link>s.
	  </para>
	  <example>
	    <title>Count words in &quot;hallo wold&quot; string, then print name
	      of your machine's operating system.</title>
	    <programlisting>
exec echo hallo world;                 # prints hallo world
exec &quot;echo hallo word&quot; | wc; # counts words in hallo world
exec uname;                            # prints operating system name
</programlisting>
	  </example>
	</simplesect></sect2><sect2 id="exit_command"><title>exit</title><simplesect><title>Usage</title><para><literal>exit [<link linkend="expression">expression</link>]</literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> quit</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>
	    Exit xsh immediately, optionally with the exit-code
	    resulting from a given expression.
	  </para>
	  <para>
	    WARNING: No files are saved on exit.
	  </para>
	</simplesect></sect2><sect2 id="fold_command"><title>fold</title><simplesect><title>Usage</title><para><literal>fold <link linkend="xpath">xpath</link> [<link linkend="expression">expression</link>]</literal></para></simplesect><simplesect><title>Description</title>
	  <para>
	    This feature is still EXPERIMENTAL! Fold command may be
	    used to mark elements matching the <link linkend="xpath">xpath</link>
	    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> <link linkend="xpath">xpath</link>
	    fold</literal>, elements marked in this way are folded to
	    the depth given by the <link linkend="expression">expression</link>
	    (default depth is 0 = fold immediately).
	  </para>
	  <informalexample>
	    <programlisting>xsh&gt; <userinput>fold //chapter 1</userinput>
xsh&gt; <userinput>ls //chapter[1] fold</userinput>
&lt;chapter id=&quot;intro&quot; xsh:fold=&quot;1&quot;&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><title>See Also</title><para><link linkend="unfold_command">unfold</link>, <link linkend="list_command">ls</link></para></simplesect></sect2><sect2 id="foreach"><title>foreach</title><simplesect><title>Usage</title><para><literal>foreach <link linkend="xpath">xpath</link>|<link linkend="perl_code">perl-code</link> 
	  <link linkend="command">command</link>|<link linkend="block">command-block</link></literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> for</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>
	    If the first argument is an <link linkend="xpath">xpath</link>
	    expression, execute the command-block for each node
	    matching the expression making it temporarily the current
	    node, so that all relative XPath
	    expressions are evaluated in its context.
	  </para>
	  <para>
	    If the first argument is a <link linkend="perl_code">perl-code</link>, it
	    is evaluated and the resulting perl-list is iterated
	    setting the variable $__ (note that there are
	    <emphasis>two</emphasis> underscores!)  to be each element
	    of the list in turn. It works much like perl's foreach,
	    except that the variable used consists of two underscores.
	  </para>
	  <example>
	    <title>Move all employee elements in a company element
	      into a staff subelement of the same company</title>
	    <programlisting>xsh&gt; <userinput>foreach //company xmove ./employee into ./staff;</userinput></programlisting>
	  </example>
	  <example>
	    <title>List content of all XML files in current directory</title>
	    <programlisting>
xsh&gt; <userinput>foreach { glob('*.xml') } { open f=$__; list f:/; }</userinput>
</programlisting>
	  </example>
	</simplesect></sect2><sect2 id="help_command"><title>help</title><simplesect><title>Usage</title><para><literal>help <link linkend="command">command</link>|argument-type</literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> ?</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>Print help on a given command or argument type.</para>
	</simplesect></sect2><sect2 id="if"><title>if</title><simplesect><title>Usage</title><para><literal>if <link linkend="xpath">xpath</link>|<link linkend="perl_code">perl-code</link> <link linkend="command">command</link>
	</literal></para><para><literal>
          if <link linkend="xpath">xpath</link>|<link linkend="perl_code">perl-code</link>
	  <link linkend="block">command-block</link> [ elsif <link linkend="block">command-block</link> ]* [ else <link linkend="block">command-block</link> ]
	</literal></para></simplesect><simplesect><title>Description</title>
	  <para>
	    Execute <link linkend="block">command-block</link> if a given <link linkend="xpath">xpath</link> or <link linkend="perl_code">perl-code</link>
	    expression evaluates to a non-emtpty node-list, true
	    boolean-value, non-zero number or non-empty literal. If
	    the first test fails, check all possibly following
	    <literal>elsif</literal> conditions and execute the
	    corresponding <link linkend="block">command-block</link> for the first one of
	    them which is true. If none of them succeeds, execute the
	    <literal>else</literal> <link linkend="block">command-block</link> (if any).
	  </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>
	</simplesect></sect2><sect2 id="ifinclude_command"><title>ifinclude</title><simplesect><title>Usage</title><para><literal>ifinclude <link linkend="filename">filename</link></literal></para></simplesect><simplesect><title>Description</title>
	  <para>
	    Include a file named <link linkend="filename">filename</link> and execute all XSH
	    commands therein unless the file was already included
	    using either <link linkend="include_command">include</link> of <link linkend="ifinclude_command">ifinclude</link>.
	  </para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="include_command">include</link></para></simplesect></sect2><sect2 id="include_command"><title>include</title><simplesect><title>Usage</title><para><literal>include <link linkend="filename">filename</link></literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> .</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>
	    Include a file named <link linkend="filename">filename</link> and execute all XSH
	    commands therein.
	  </para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="ifinclude_command">ifinclude</link></para></simplesect></sect2><sect2 id="indent"><title>indent</title><simplesect><title>Usage</title><para><literal>indent <link linkend="expression">expression</link></literal></para></simplesect><simplesect><title>Description</title>
	  <para>If the value of <link linkend="expression">expression</link> is 1,
	    format the XML output while saving a document by adding
	    some nice ignorable whitespace. If the value is 2 (or
	    higher), XSH will act as in case of 1, plus it will add a
	    leading and a trailing linebreak to each text node.
	  </para>
	  <para>Note, that since the underlying C library
	    (libxml2) uses a hardcoded indentation of 2 space
	    characters per indentation level, the amount of
	    whitespace used for indentation can not be
	    altered on runtime.
	  </para>
	  <para>This command is equivalent to setting the
	    <literal>$INDENT</literal> variable.</para>
	</simplesect></sect2><sect2 id="insert_command"><title>insert</title><simplesect><title>Usage</title><para><literal>insert <link linkend="nodetype">node-type</link> <link linkend="expression">expression</link> [namespace <link linkend="expression">expression</link>] <link linkend="loc">location</link> <link linkend="xpath">xpath</link>
	</literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> add</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>
	    Works just like xadd, except that the new node is attached
	    only the first node matched.
	  </para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="xinsert_command">xinsert</link>, <link linkend="move_command">move</link>, <link linkend="xmove_command">xmove</link></para></simplesect></sect2><sect2 id="iterate"><title>iterate</title><simplesect><title>Usage</title><para><literal>iterate <link linkend="xpath">xpath</link> <link linkend="block">command-block</link></literal></para></simplesect><simplesect><title>Description</title>
	  <para>
	    Iterate works very much like the XPath variant of <link linkend="foreach">foreach</link>, except that <literal>iterate</literal>
	    evaluates the <link linkend="block">command-block</link> as soon as 
	    a new node matching a given <link linkend="xpath">xpath</link> 
	    is found. As a limitation, the <link linkend="xpath">xpath</link>
	    expresion used with <literal>iterate</literal>
	    may only consist of one XPath step, i.e. it cannot
	    contain an XPath step separator <literal>/</literal>.
	  </para>
	  <para>
	    What are the benefits of <literal>iterate</literal> over a
	    <link linkend="foreach">foreach</link> loop, then? Well, under some
	    circumstances it is efficiency, under other there are
	    none. To clarify this, we have to dive a bit deeper into
	    the details of XPath implementation.  By definition, the
	    node-list resulting from evaluation of an XPath has to be
	    ordered in the canonical document order. That means that
	    an XPath implementation must contain some kind of a
	    sorting algorithm. This would not itself be much trouble
	    if a relative document order of two nodes of a DOM tree
	    could be determined in a constant time. Unfortunately, the
	    libxml2 library, used behind XSH, does not implement
	    mechanisms that would allow this complexity restriction
	    (which is, however, quite natural and reasonable approach
	    if all the consequences are considered). Thus, when
	    comparing two nodes, libxml2 traverses the tree to find
	    their nearest common ancestor and at that point determines
	    the relative order of the two subtrees by trying to seek
	    one of them in a list of right siblings of the other. This
	    of course cannot be handled in a constant time. As a
	    result, the sorting algorithm, reasonably efficient for a
	    constant time comparison (polynomial of a degree &lt; 1.5) or
	    small node-lists, becomes rather unusable for huge
	    node-lists with linear time comparison (still polynomial
	    but of a degree &gt; 2).
	  </para>
	  <para>
	    The <literal>iterate</literal> command provides a way to
	    avoid sorting the resulting nodelist by limiting allowed
	    XPath expression to one step (and thus one axis) at a
	    time. On the other hand, since <literal>iterate</literal>
	    is implemented in Perl, a proxy object glueing the C and
	    Perl layers has to be created for every node the iterator
	    passes by. This (plus some extra subroutine calls) makes
	    it about two to three times slower compared to a similar
	    tree-traversing algorithm used by libxml2 itself during
	    XPath evaluation.
	  </para>
	  <para>
	    Our experience shows that <literal>iterate</literal> beats
	    <link linkend="foreach">foreach</link> in performance on large
	    node-lists (&gt;=1500 nodes, but your milage may vary) while
	    <link linkend="foreach">foreach</link> wins on smaller node-lists.
	  </para>
	  <para>
	    The following two examples give equivallent results.
	    However, the one using iterate may be faster esp. if
	    the number of nodes being counted is very large.
	  </para>
	  <example>
	    <title>Count inhabitants of the kingdom of Rohan in productive age</title>
	    <programlisting>cd rohan/inhabitants;</programlisting>
	    <programlisting>iterate child::*[@age&gt;=18 and @age&lt;60] { perl $productive++ };</programlisting>
	    <programlisting>echo &quot;$productive inhabitants in productive age&quot;;</programlisting>
	  </example>
	  <example>
	    <title>Using XPath</title>
	    <programlisting>$productive=count(rohan/inhabitants/*[@age&gt;=18 and @age&lt;60]);</programlisting>
	    <programlisting>echo &quot;$productive inhabitants in productive age&quot;;</programlisting>
	  </example>
	  <para>
	    Use e.g. <literal>| time cut</literal> pipe-line
	    redirection to benchmark a XSH command on a UNIX system.
	  </para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="foreach">foreach</link>, <link linkend="next_command">next</link>, <link linkend="prev_command">prev</link>, <link linkend="last_command">last</link></para></simplesect></sect2><sect2 id="keep_blanks"><title>keep-blanks</title><simplesect><title>Usage</title><para><literal>keep_blanks <link linkend="expression">expression</link></literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> keep_blanks</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>
	    Allows you to turn off XML::LibXML's default behaviour of
	    maintaining whitespace in the document. Non-zero expression
	    forces the XML parser to preserve all whitespace.
	  </para>
	  <para>This command is equivalent to setting the
	    <literal>$KEEP_BLANKS</literal> variable.</para>
	</simplesect></sect2><sect2 id="last_command"><title>last</title><simplesect><title>Usage</title><para><literal>last [<link linkend="expression">expression</link>]</literal></para></simplesect><simplesect><title>Description</title>
	  <para>
	    The last command is like the break statement in C (as used
	    in loops); it immediately exits an enclosing loop.  The
	    optional <link linkend="expression">expression</link> argument may
	    evaluate to a positive integer number that indicates which
	    level of the nested loops to quit.  If this argument is
	    omitted, it defaults to 1, i.e. the innermost loop.
	  </para>
	  <para>
	    Using this command outside a subroutine causes an
	    immediate run-time error.
	  </para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="foreach">foreach</link>, <link linkend="while">while</link>, <link linkend="iterate">iterate</link>, <link linkend="next_command">next</link>, <link linkend="last_command">last</link></para></simplesect></sect2><sect2 id="cd_command"><title>lcd</title><simplesect><title>Usage</title><para><literal>lcd <link linkend="expression">expression</link></literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> chdir</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>
	    Changes the filesystem working directory to <link linkend="expression">expression</link>, if possible.  If <link linkend="expression">expression</link> is omitted, changes to the directory
	    specified in HOME environment variable, if set; if not,
	    changes to the directory specified by LOGDIR environment
	    variable.
	  </para>
	</simplesect></sect2><sect2 id="load_ext_dtd"><title>load-ext-dtd</title><simplesect><title>Usage</title><para><literal>load_ext_dtd <link linkend="expression">expression</link></literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> load_ext_dtd</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>
	    If the expression is non-zero, XML parser loads external DTD
	    subsets while parsing. By default, this option is enabled.
	  </para>
	  <para>This command is equivalent to setting the
	    <literal>$LOAD_EXT_DTD</literal> variable.</para>
	</simplesect></sect2><sect2 id="local_command"><title>local</title><simplesect><title>Usage</title><para><literal>local $<link linkend="ID">id</link> = <link linkend="xpath">xpath</link></literal></para><para><literal>local %<link linkend="ID">id</link> = <link linkend="xpath">xpath</link></literal></para><para><literal>local $<link linkend="ID">id</link>|%<link linkend="ID">id</link> [ $<link linkend="ID">id</link>|%<link linkend="ID">id</link> ... ] </literal></para></simplesect><simplesect><title>Description</title>
	  <para> 
	    This command acts in a very similar way as
	    <link linkend="assign_command">assign</link> does, except
	    that the variable assignment is done temporarily
	    and lasts only for the rest of the nearest enclosing
	    <link linkend="block">command-block</link>. At the end of the
	    enclosing block or subroutine the original value
	    is restored. This command may also be used without the
	    assignment part and assignments may be done later using
	    the usual <link linkend="assign_command">assign</link> command.
	  </para>
	  <para>Note, that the variable itself is not lexically is
	    still global in the sense that it is still visible to any
	    subroutine called subsequently from within the same block.
	    A local just gives temporary values to global (meaning
	    package) variables. Unlike Perl's <literal>my</literal>
	    declarations it does not <emphasis>create</emphasis> a
	    local variable.  This is known as dynamic scoping.
	    Lexical scoping is not implemented in XSH.
	  </para>
	  <para>
	    To sum up for Perl programmers: <literal>local</literal>
	    in XSH works exactly the same as <literal>local</literal>
	    in Perl.
	  </para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="assign_command">assign</link>, <link linkend="def">def</link></para></simplesect></sect2><sect2 id="locate_command"><title>locate</title><simplesect><title>Usage</title><para><literal>locate <link linkend="xpath">xpath</link></literal></para></simplesect><simplesect><title>Description</title>
	  <para>Print canonical XPaths leading to nodes matched by
	    a given <link linkend="xpath">xpath</link>.</para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="pwd_command">pwd</link></para></simplesect></sect2><sect2 id="list_command"><title>ls</title><simplesect><title>Usage</title><para><literal>ls <link linkend="xpath">xpath</link> [<link linkend="expression">expression</link>]</literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> list</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>
	    List the XML representation of all nodes matching <link linkend="xpath">xpath</link>. The optional <link linkend="expression">expression</link> argument may be provided to
	      specify the depth of XML tree listing. If negative, the
	      tree will be listed to unlimited depth.  If the <link linkend="expression">expression</link> results in the word
	      <literal>fold</literal>, elements marked with the <link linkend="fold_command">fold</link> command are folded, i.e. listed
	      only to a certain depth (this feature is still
	      EXPERIMENTAL!).
	  </para>
	  <para>
	    Unless in quiet mode, this command prints also number of
	    nodes matched on stderr.
	  </para>
	  <para>
	    If the <link linkend="xpath">xpath</link> parameter is omitted,
	    current context node is listed to the depth of 1.
	  </para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="count_command">count</link>, <link linkend="fold_command">fold</link>, <link linkend="unfold_command">unfold</link></para></simplesect></sect2><sect2 id="map_command"><title>map</title><simplesect><title>Usage</title><para><literal>map <link linkend="perl_code">perl-code</link> <link linkend="xpath">xpath</link></literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> sed</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>
	    This command provides an easy way to modify node's data
	    (content) using arbitrary Perl code.
	  </para>
	  <para>
	    Each of the nodes matching <link linkend="xpath">xpath</link> is
	    passes its data to the <link linkend="perl_code">perl-code</link> via
	    the <literal>$_</literal> variable and receives the (possibly)
	    modified data using the same variable.
	  </para>
	  <para>Since element nodes do not really have any proper
	    content (they are only a storage for other nodes), node's
	    name (tag) is used in case of elements. Note, however,
	    that recent versions of XSH provide a special command
	    <link linkend="rename_command">rename</link> with a very similar
	    syntax to <literal>map</literal>, that should be used for
	    renaming element, attribute, and processing instruction
	    nodes.
	  </para>
	  <example>
	    <title>Capitalises all hobbit names</title>
	    <programlisting>xsh&gt; <userinput>map { $_=ucfirst($_) } //hobbit/@name</userinput></programlisting>	  
	  </example>
	  <example>
	    <title>Changes goblins to orcs in all hobbit tales.</title>
	    <programlisting>xsh&gt; <userinput>map { s/goblin/orc/gi } //hobbit/tale/text()</userinput></programlisting>
	  </example>
	</simplesect></sect2><sect2 id="move_command"><title>move</title><simplesect><title>Usage</title><para><literal>move <link linkend="xpath">xpath</link> <link linkend="loc">location</link> <link linkend="xpath">xpath</link></literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> mv</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para><literal>move</literal> command acts exactly like
	      <link linkend="copy_command">copy</link>, except that it
	      <emphasis>removes</emphasis> the source nodes after a
	      succesfull copy. Remember that the moved nodes are
	      actually <emphasis>different nodes</emphasis> from the
	      original ones (which may not be obvious when moving
	      nodes within a single document into locations that do
	      not require type conversion). So, after the move, the
	      original nodes do not exist neither in the document
	      itself nor any nodelist variable.
	  </para>
	  <para>
	    See <link linkend="copy_command">copy</link> for more details on how
	    the copies of the moved nodes are created.
	  </para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="copy_command">copy</link>, <link linkend="xmove_command">xmove</link>, <link linkend="insert_command">insert</link>, <link linkend="xinsert_command">xinsert</link></para></simplesect></sect2><sect2 id="namespaces_command"><title>namespaces</title><simplesect><title>Usage</title><para><literal>namespaces [<link linkend="xpath">xpath</link>]</literal></para></simplesect><simplesect><title>Description</title>
	  <para>
	    For each node matching given <link linkend="xpath">xpath</link> lists
	    all namespaces that are valid in its scope in the form of
	    <literal>xmlns:prefix=&quot;uri&quot;</literal> declarations.
	    If no <link linkend="xpath">xpath</link> is given, lists namespaces
	    in the scope of the current node.
	  </para>
	</simplesect></sect2><sect2 id="next_command"><title>next</title><simplesect><title>Usage</title><para><literal>next [<link linkend="expression">expression</link>]</literal></para></simplesect><simplesect><title>Description</title>
	  <para>
	    The next command is like the continue statement in C; it starts the
	    next iteration of an enclosing loop. The optional <link linkend="expression">expression</link> argument may evaluate
	    to a positive integer number that indicates which
	    level of the nested loops should be restarted.
	    If omitted, it defaults to 1, i.e. the innermost loop.
	  </para>
	  <para>
	    Using this command outside a loop causes an
	    immediate run-time error.
	  </para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="foreach">foreach</link>, <link linkend="while">while</link>, <link linkend="iterate">iterate</link>, <link linkend="redo_command">redo</link>, <link linkend="last_command">last</link>, <link linkend="prev_command">prev</link></para></simplesect></sect2><sect2 id="nobackups"><title>nobackups</title><simplesect><title>Usage</title><para><literal>nobackups</literal></para></simplesect><simplesect><title>Description</title>
	  <para>Disable creating backup files on save.</para>
	  <para>This command is equivalent to setting the
	    <literal>$BACKUPS</literal> variable to 0.</para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="nobackups">nobackups</link></para></simplesect></sect2><sect2 id="nodebug"><title>nodebug</title><simplesect><title>Usage</title><para><literal>nodebug</literal></para></simplesect><simplesect><title>Description</title>
	  <para>Turn off debugging messages.</para>
	  <para>This is equivalent to setting
	    <literal>$DEBUG</literal> variable to 0.</para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="debug">debug</link></para></simplesect></sect2><sect2 id="normalize_command"><title>normalize</title><simplesect><title>Usage</title><para><literal>normalize <link linkend="xpath">xpath</link></literal></para></simplesect><simplesect><title>Description</title>
	  <para><literal>normalize</literal> 
	    puts all text nodes in the full depth of the sub-tree
	      underneath each node selected by a given <link linkend="xpath">xpath</link>,
	    into a &quot;normal&quot; form where only structure (e.g., elements,
	    comments, processing instructions, CDATA sections, and
	    entity references) separates text nodes, i.e., there are
	    neither adjacent Text nodes nor empty Text nodes.
	  </para>
	</simplesect></sect2><sect2 id="open_command"><title>open</title><simplesect><title>Usage</title><para><literal>[open [HTML|XML|DOCBOOK] [FILE|PIPE|STRING]] <link linkend="ID">id</link>=<link linkend="expression">expression</link></literal></para></simplesect><simplesect><title>Description</title>
	  <para>
	    Load a new XML, HTML or SGML DOCBOOK document from the file
	    (actually arbitrary URL), command output or string provided by
	    the <link linkend="expression">expression</link>.  In XSH the document is given a
	    symbolic name <link linkend="ID">id</link>.  To identify the documentin commands like close,
	    save, validate, dtd or enc simply use <link linkend="ID">id</link>. In commands which work on document nodes, give
	    <link linkend="ID">id</link>: prefix to XPath expressions to point the
	    XPath to the document.
	  </para>
	  <informalexample>
	    <programlisting>
xsh&gt; <userinput>open x=mydoc.xml # open a document</userinput>

# open a HTML document from the Internet
xsh&gt; <userinput>open HTML h=&quot;http://www.google.com/?q=xsh&quot;</userinput>
# quote file name if it contains whitespace
xsh&gt; <userinput>open y=&quot;document with a long name with spaces.xml&quot;</userinput>

# you may omit the word open when loading an XML file/URI.
xsh&gt; <userinput>z=mybook.xml</userinput>

# use HTML or DOCBOOK keywords to load these types
xsh&gt; open HTML <userinput>z=index.htm</userinput>

# use PIPE keyword to read output of a command
xsh&gt; open HTML PIPE <userinput>z='wget -O - xsh.sourceforge.net/index.html'</userinput>

# use z: prefix to identify the document opened with the
# previous comand in an XPath expression.
xsh&gt; <userinput>ls z://chapter/title	    </userinput>
</programlisting>
	  </informalexample>
	</simplesect></sect2><sect2 id="options_command"><title>options</title><simplesect><title>Usage</title><para><literal>options</literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> flags</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>List current values of all XSH flags and options (such as
	  validation flag or query-encoding).</para>
	  <example>
	    <title>Store current settings in your .xshrc</title>
	    <programlisting>xsh&gt; <userinput>options | cat &gt; ~/.xshrc</userinput></programlisting>
	  </example>
	</simplesect></sect2><sect2 id="complete_attributes"><title>parser-completes-attributes</title><simplesect><title>Usage</title><para><literal>parser-completes-attributes <link linkend="expression">expression</link></literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> complete_attributes, complete-attributes, parser_completes_attributes</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>
	    If the expression is non-zero, this command allows XML parser
	    to complete the elements attributes lists with the ones
	    defaulted from the DTDs.  By default, this option is enabled.
	  </para>
	  <para>This command is equivalent to setting the
	    <literal>$PARSER_COMPLETES_ATTRIBUTES</literal> variable.</para>
	</simplesect></sect2><sect2 id="parser_expands_entities"><title>parser-expands-entities</title><simplesect><title>Usage</title><para><literal>parser_expands_entities <link linkend="expression">expression</link></literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> parser_expands_entities</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>
	    Enable the entity expansion during the parse process if the
	    <link linkend="expression">expression</link> is non-zero, disable it otherwise.
	    If entity
	    expansion is off, any external parsed entities in the
	    document are left as entities. Defaults to on.
	  </para>
	  <para>This command is equivalent to setting the
	    <literal>$PARSER_EXPANDS_ENTITIES</literal> variable.</para>
	</simplesect></sect2><sect2 id="parser_expands_xinclude"><title>parser-expands-xinclude</title><simplesect><title>Usage</title><para><literal>parser_expands_xinclude <link linkend="expression">expression</link></literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> parser_expands_xinclude</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>
	    If the <link linkend="expression">expression</link> is non-zero, the parser is
	    allowed to expand XIinclude tags imidiatly while parsing the
	    document.
	  </para>
	  <para>This command is equivalent to setting the
	    <literal>$PARSER_EXPANDS_XINCLUDE</literal> variable.</para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="process_xinclude_command">process-xinclude</link></para></simplesect></sect2><sect2 id="pedantic_parser"><title>pedantic-parser</title><simplesect><title>Usage</title><para><literal>pedantic_parser <link linkend="expression">expression</link></literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> pedantic_parser</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>
	    If you wish, you can make XML::LibXML more pedantic by passing
	    a non-zero <link linkend="expression">expression</link> to this command.
	  </para>
	  <para>This command is equivalent to setting the
	    <literal>$PEDANTIC_PARSER</literal> variable.</para>
	</simplesect></sect2><sect2 id="eval_command"><title>perl</title><simplesect><title>Usage</title><para><literal>eval <link linkend="perl_code">perl-code</link></literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> eval</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>Evaluate a given perl expression.</para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="count_command">count</link></para></simplesect></sect2><sect2 id="prev_command"><title>prev</title><simplesect><title>Usage</title><para><literal>prev [<link linkend="expression">expression</link>]</literal></para></simplesect><simplesect><title>Description</title>
	  <para>
	    This command is only allowed inside an
	    <literal>iterate</literal> loop. It returns the iteration
	    one step back, to the previous node on the iterated axis.
	    The optional <link linkend="expression">expression</link> argument may be
	    used to indicate to which level of nested loops the
	    command applies to.
	  </para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="iterate">iterate</link>, <link linkend="redo_command">redo</link>, <link linkend="last_command">last</link>, <link linkend="next_command">next</link></para></simplesect></sect2><sect2 id="print_command"><title>print</title><simplesect><title>Usage</title><para><literal>print <link linkend="expression">expression</link> [<link linkend="expression">expression</link> ...]</literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> echo</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>Interpolate and print a given expression(s).</para>
	</simplesect></sect2><sect2 id="process_xinclude_command"><title>process-xinclude</title><simplesect><title>Usage</title><para><literal>process_xinclude [<link linkend="ID">id</link>]</literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> process_xinclude, process-xincludes, process_xincludes, xinclude, xincludes, load_xincludes, load-xincludes, load_xinclude, load-xinclude</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>
	    Process any xinclude tags in the document <link linkend="ID">id</link>.
	  </para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="parser_expands_xinclude">parser-expands-xinclude</link></para></simplesect></sect2><sect2 id="pwd_command"><title>pwd</title><simplesect><title>Usage</title><para><literal>pwd</literal></para></simplesect><simplesect><title>Description</title>
	  <para>Print XPath leading to the current context node.  This
	    is equivalent to <literal>locate .</literal>.
	  </para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="locate_command">locate</link></para></simplesect></sect2><sect2 id="query_encoding"><title>query-encoding</title><simplesect><title>Usage</title><para><literal>query-encoding <link linkend="enc_string">enc-string</link></literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> query_encoding</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>Set the default query character encoding.</para>
	  <para>This command is equivalent to setting the
	    <literal>$QUERY_ENCODING</literal> variable.</para>
	</simplesect></sect2><sect2 id="quiet"><title>quiet</title><simplesect><title>Usage</title><para><literal>quiet</literal></para></simplesect><simplesect><title>Description</title>
	  <para>Turn off verbose messages.</para>
	  <para>This command is equivalent to setting the
	    <literal>$QUIET</literal> variable.</para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="verbose">verbose</link></para></simplesect></sect2><sect2 id="recovering"><title>recovering</title><simplesect><title>Usage</title><para><literal>recovering <link linkend="expression">expression</link></literal></para></simplesect><simplesect><title>Description</title>
	  <para>
	    Turn on recovering parser mode if the
	    <link linkend="expression">expression</link> is non-zero or off otherwise.
	    Defaults to off. Note, that the in the recovering mode,
            validation is not performed by the parser even if
	    the validation flag is on and that recovering mode flag
            only influences parsing of XML documents (not HTML).
	  </para>
	  <para>The recover mode helps to efficiently recover
	  documents that are almost well-formed. This for example
	  includes documents without a close tag for the document
	  element (or any other element inside the document).</para>
	  <para>This command is equivalent to setting the
	    <literal>$RECOVERING</literal> variable.</para>
	</simplesect></sect2><sect2 id="redo_command"><title>redo</title><simplesect><title>Usage</title><para><literal>redo [<link linkend="expression">expression</link>]</literal></para></simplesect><simplesect><title>Description</title>
	  <para>
	    The redo command restarts a loop block without evaluating
	    the conditional again. The optional <link linkend="expression">expression</link> argument may evaluate
	    to a positive integer number that indicates which
	    level of the nested loops should be restarted.
	    If omitted, it defaults to 1, i.e. the innermost loop.
	  </para>
	  <para>
	    Using this command outside a loop causes an immediate
	    run-time error.
	  </para>
	  <example>
	    <title>Restart a higher level loop from an inner one</title>
	    <programlisting>while ($i&lt;100) { 
  # ...
  foreach //para {
    # some code
    if $param { 
      redo; # redo foreach loop
    } else {
      redo 2; # redo while loop
    }
  }
}
</programlisting>
	  </example>
	</simplesect><simplesect><title>See Also</title><para><link linkend="foreach">foreach</link>, <link linkend="while">while</link>, <link linkend="iterate">iterate</link>, <link linkend="next_command">next</link>, <link linkend="last_command">last</link></para></simplesect></sect2><sect2 id="registerfunc_command"><title>register-function</title><simplesect><title>Usage</title><para><literal>register-function <link linkend="expression">expression</link> <link linkend="perl_code">perl-code</link></literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> regfunc</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>
	    EXPERIMENTAL!  Register given perl code as a new XPath
	    extension function under a name provided in the first
	    argument (<link linkend="expression">expression</link>). XML::LibXML DOM
	    API may be used in the perl code for object processing. If
	    the name contains a colon, then the first part before the
	    colon must be a registered namespace prefix (see <link linkend="registerns_command">register-namespace</link>) and the function is
	    registered within the corresponding namespace.
	  </para>
	</simplesect></sect2><sect2 id="registerns_command"><title>register-namespace</title><simplesect><title>Usage</title><para><literal>register-namespace <link linkend="expression">expression</link> <link linkend="expression">expression</link></literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> regns</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>
             Registers the first argument
             as a prefix for the namespace given in the second argument.
             The prefix can later be used in XPath expressions.
	  </para>
	</simplesect></sect2><sect2 id="registerns_xhtml_command"><title>register-xhtml-namespace</title><simplesect><title>Usage</title><para><literal>register-xhtml-namespace <link linkend="expression">expression</link></literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> regns-xhtml</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>
             Registers a prefix for the XHTML namespace.
             The prefix can later be used in XPath expressions.
	  </para>
	</simplesect></sect2><sect2 id="registerns_xsh_command"><title>register-xsh-namespace</title><simplesect><title>Usage</title><para><literal>register-xsh-namespace <link linkend="expression">expression</link></literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> regns-xsh</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>
	    Registers a new prefix for the XSH namespace.  The prefix
	    can later be used in XPath expressions.  Note, that XSH
	    namespace is by default registered with
	    <literal>xsh</literal> prefix. This command is thus, in
	    general, useful only when some document uses
	    <literal>xsh</literal> prefix for a different namespace.
	  </para>
	</simplesect></sect2><sect2 id="prune_command"><title>remove</title><simplesect><title>Usage</title><para><literal>remove <link linkend="xpath">xpath</link></literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> rm, prune, delete, del</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>Remove all nodes matching <link linkend="xpath">xpath</link>.</para>
	  <example>
	    <title>Get rid of all evil creatures.</title>
	    <programlisting>xsh&gt; <userinput>del //creature[@manner='evil']</userinput></programlisting>
	  </example>
	</simplesect></sect2><sect2 id="rename_command"><title>rename</title><simplesect><title>Usage</title><para><literal>rename <link linkend="perl_code">perl-code</link> <link linkend="xpath">xpath</link></literal></para></simplesect><simplesect><title>Description</title>
	  <para>
	    This command is very similar to the <link linkend="map_command">map</link>
	    command, except that it operates on nodes' names rather
	    than their data/values. For every element, attribute or
	    processing-instruction matched by the <link linkend="xpath">xpath</link> expression the following procedure is
	    used: 1) the name of the node is stored into Perl's
	    <literal>$_</literal> variable, 2) the <link linkend="perl_code">perl-code</link> is evaluated, and 3) the (posibly
	    changed) content of the <literal>$_</literal> variable is
	    used as a new name for the node.
	  </para>
	  <example>
	    <title>Renames all hobbits to halflings</title>
	    <programlisting>xsh&gt; <userinput>map $_='halfling' //hobbit</userinput></programlisting>
	  </example>
	  <example>
	    <title>Make all elements and attributes uppercase</title>
	    <programlisting>xsh&gt; <userinput>map { $_=uc($_) } (//*|//@*)</userinput></programlisting>	  
	  </example>
	</simplesect><simplesect><title>See Also</title><para><link linkend="map_command">map</link></para></simplesect></sect2><sect2 id="return_command"><title>return</title><simplesect><title>Usage</title><para><literal>return</literal></para></simplesect><simplesect><title>Description</title>
	  <para>
	    This command immediatelly stops the execution of a
	    procedure it occurs in and returns the execution to the
	    place of the script from which the subroutine was called.
	  </para>
	  <para>
	    Using this command outside a subroutine causes an
	    immediate run-time error.
	  </para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="def">def</link>, <link linkend="call_command">call</link></para></simplesect></sect2><sect2 id="run_mode"><title>run-mode</title><simplesect><title>Usage</title><para><literal>run-mode</literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> run_mode</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>
	    Switch into normal XSH mode in which all commands are
	    executed.
	  </para>
	  <para>This is equivalent to setting
	    <literal>$TEST_MODE</literal> variable to 0.</para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="test_mode">test-mode</link></para></simplesect></sect2><sect2 id="save_command"><title>save</title><simplesect><title>Usage</title><para><literal>save [HTML|XML|XInclude] [FILE|PIPE|STRING] <link linkend="ID">id</link> <link linkend="expression">expression</link> [encoding <link linkend="enc_string">enc-string</link>]
or
	  save <link linkend="ID">id</link>
or
	  save
</literal></para></simplesect><simplesect><title>Description</title>
	  <para>
	    Save the document identified by <link linkend="ID">id</link>.
	    Using one of the <literal>FILE</literal>,
	    <literal>PIPE</literal>, <literal>STRING</literal>
	    keywords the user may choose to save the document to a
	    file send it to a given command's input via a pipe or
	    simply return its content as a string. If none of the
	    keywords is used, it defaults to FILE. If saving to a
	    PIPE, the <link linkend="expression">expression</link> argument must
	    provide the coresponding command and all its parameters.
	    If saving to a FILE, the <link linkend="expression">expression</link>
	    argument may provide a filename; if omitted, it defaults
	    to the original filename of the document. If saving to a
	    STRING, the <link linkend="expression">expression</link> argument
	    is ignored and may freely be omitted.
	  </para>
	  <para>
	    The output format is controlled using one of the XML,
	    HTML, XInclude keywords (see below). If the format keyword
	    is ommited, save it defaults to XML.
	  </para>
	  <para>
	    Note, that a document should be saved as HTML only if it
	    actually is a HTML document.  Note also, that the optional
	    encoding parameter forces character conversion only; it is
	    up to the user to declare the document encoding in the
	    appropriate HTML &lt;META&gt; tag.
	  </para>
	  <para>
	    The XInclude keyword automatically implies XML format and
	    can be used to force XSH to save all already expanded
	    XInclude sections back to their original files while
	    replacing them with &lt;xi:include&gt; tags in the main
	    XML file. Moreover, all material included within
	    &lt;include&gt; elements from the
	    <ulink url="http://www.w3.org/2001/XInclude">http://www.w3.org/2001/XInclude</ulink>
	    namespace is saved to separate files too according to the
	    <literal>href</literal> attribute, leaving only empty
	    &lt;include&gt; element in the root file. This feature may
	    be used to split the document to new XInclude fragments.
	  </para>
	  <para>
	    The encoding keyword followed by a <link linkend="enc_string">enc-string</link> can be used to convert the document
	    from its original encoding to a different encoding.  In
	    case of XML output, the &lt;?xml?&gt; declaration is
	    changed accordingly.  The new encoding is also set as the
	    document encoding for the particular document.
	  </para>
	  <example>
	    <title>Use save to preview a HTML document in Lynx</title>
	    <programlisting>save HTML PIPE mydoc 'lynx -stdin'</programlisting>
	  </example>
	</simplesect><simplesect><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></sect2><sect2 id="select_command"><title>select</title><simplesect><title>Usage</title><para><literal>select <link linkend="ID">id</link></literal></para></simplesect><simplesect><title>Description</title>
	  <para>
	    Make <link linkend="ID">id</link> the document identifier to be used in the next
	    xpath evaluation without identifier prefix.
	  </para>
	  <informalexample>
	    <programlisting>
xsh&gt; <userinput>a=mydoc1.xml</userinput># opens and selects a
xsh&gt; <userinput>ls /</userinput># lists a
xsh&gt; <userinput>b=mydoc2.xml</userinput># opens and selects b
xsh&gt; <userinput>ls /</userinput># lists b
xsh&gt; <userinput>ls a:/</userinput># lists and selects a
xsh&gt; <userinput>select b</userinput># does nothing except selecting b
xsh&gt; <userinput>ls /</userinput># lists b
</programlisting>
	  </informalexample>
	</simplesect></sect2><sect2 id="set_enc_command"><title>set-enc</title><simplesect><title>Usage</title><para><literal>set-enc <link linkend="enc_string">enc-string</link> [<link linkend="ID">id</link>]</literal></para></simplesect><simplesect><title>Description</title>
	  <para>
	    Changes character encoding of a given document. If no document
	    <link linkend="ID">id</link> is given, the command applies to the
	    current document. This has two effects:
	    changing the XMLDecl encoding declaration 
	    in the document prolog to display the new
	    encoding and making all future <link linkend="save_command">save</link> operations
	    on the document default to the given charset.
	  </para>
	  <informalexample>
	    <programlisting>xsh&gt; <userinput>ls</userinput>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;iso-8859-1&quot;?&gt;
&lt;foo&gt;...&lt;/foo&gt;
xsh&gt; <userinput>set-enc &quot;utf-8&quot;</userinput>
xsh&gt; <userinput>ls</userinput>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;foo&gt;...&lt;/foo&gt;
xsh&gt; <userinput>save</userinput># saves the file in UTF-8 encoding
</programlisting>
	  </informalexample>
	</simplesect><simplesect><title>See Also</title><para><link linkend="print_enc_command">enc</link>, <link linkend="doc_info_command">doc-info</link></para></simplesect></sect2><sect2 id="set_standalone_command"><title>set-standalone</title><simplesect><title>Usage</title><para><literal>set-standalone <link linkend="expression">expression</link> [<link linkend="ID">id</link>]</literal></para></simplesect><simplesect><title>Description</title>
	  <para>
	    Changes the value of <literal>standalone</literal> declaration
            in the XMLDecl prolog of a document. The <link linkend="expression">expression</link> should evaluate to either 1 or 0 
	    or <literal>'yes'</literal> or <literal>'no'</literal>.
	    The result of applying the command on other values is not specified.
	    If no document <link linkend="ID">id</link> is given, 
	    the command applies to the current document. 
	  </para>
	</simplesect><simplesect><title>See Also</title><para><link linkend="doc_info_command">doc-info</link></para></simplesect></sect2><sect2 id="skip_dtd"><title>skip-dtd</title><simplesect><title>Usage</title><para><literal>skip-dtd <link linkend="expression">expression</link></literal></para></simplesect> <simplesect>
   <title>Aliases</title>
   <para><literal> skip_dtd</literal></para>
 </simplesect><simplesect><title>Description</title>
	  <para>If the value of <link linkend="expression">expression</link> is 1 (non-zero),
	    DTD DOCTYPE declaration is omitted from any serialization
	    of XML documents (including <link linkend="list_command">ls</link>
	    and <link linkend="save_command">save</link>). Default value is
	    <literal>0</literal>.
	  </para>
	  <para>This command is equivalent to setting the
	    <literal>$SKIP_DTD</literal> variable.</para>
	</simplesect></sect2><sect2 id="sort_command"><title>sort</title><simplesect><title>Usage</title><para><literal>sort <link linkend="xpath">xpath</link>|<link linkend="perl_code">perl-code</link> <link linkend="perl_code">perl-code</link> %<link linkend="ID">id</link></literal></para></simplesect><simplesect><title>Description</title>
	  <para>
	    EXPERIMENTAL! This command is not yet guaranteed to remain in the 
	    future releases.
	  </para>
	  <para>
	    DOCUMENTATION OBSOLETE! Syntax changed!
	  </para>
	  <para>This command may be used to sort the node-list stored
	    in the node-list variable <link linkend="ID">id</link>. First, for
	    each node in the node-list %<link linkend="ID">id</link>, the
	    first argument (either a <link linkend="xpath">xpath</link> or <link linkend="perl_code">perl-code</link> expression), which serves as a
	    sorting criterion, is evaluated in the context of the node
	    and the obtained value is stored together with the
	    node. (In case of <link linkend="xpath">xpath</link> the result of
	    whatever type is cast to a string). Then perl's sorting
	    algorithm is used to sort the nodelist, consulting the
	    second, <link linkend="perl_code">perl-code</link>, argument to compare
	    nodes. Before the <link linkend="perl_code">perl-code</link> is
	    evaluated, the values obtained from the previous
	    evaluation of the sorting crierion argument on the two
	    nodes being compared are stored into <literal>$a</literal>
	    and <literal>$b</literal> variables in the respective
	    order.  The <link linkend="perl_code">perl-code</link> being consulted is
	    supposed to return either -1 (the first node should come
	    first), 0 (no order precedence), or 1 (the second node
	    should come first). Note that Perl provides very
	    convenient operators <literal>cmp</literal> and
	    <literal>&lt