XML/XSH/Help.pm

filter: Statementstmt 100% 197 / 197 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 100% 199 / 199 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« XML/XSH/CompletionList.pm ↑ Summary
1 # This file was automatically generated from src/xsh_grammar.xml on
2 # Wed Sep 10 17:53:03 2003
3  
4 package XML::XSH::Help;
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use vars qw($HELP %HELP);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 4 $HELP=<<'END';
10 General notes:
11  
12 XSH acts as a command interpreter. Individual commands must be separated
13 with a semicolon. Each command may be followed by a pipeline redirection
14 to capture the command's output. In the interactive shell, backslash may
15 be used at the end of line to indicate that the command follows on the
16 next line.
17  
18 A pipeline redirections may be used either to feed the command's output
19 to a unix command or to store it in a XSH string variable.
20  
21 In the first case, the syntax is `xsh-command | shell-command ;' where
22 `xsh-command' is any XSH command and `shell-command' is any command (or
23 code) recognized by the default shell interpreter of the operating system
24 (i.e. on UNIX systems by `sh' or `csh', on Windows systems by `cmd').
25 Brackets may be used to join more shell commands (may depend on which
26 shell is used).
27  
28 Example: Count attributes of words containing string foo in its name or
29 value.
30  
31 xsh> ls //words/@* | grep foo | wc
32  
33 In order to store a command's output in a string variable, the pipeline
34 redirection must take the form `xsh-command |> $variable' where
35 `xsh-command' is any XSH command and `$variable' is any valid name for a
36 string <variable>variable.
37  
38 Example: Store the number of all words in a variable named count.
39  
40 xsh> count //words |> $count
41  
42 `<help> command' gives a list of all XSH commands.
43  
44 `<help> type' gives a list of all argument types.
45  
46 `<help>' followed by a command or type name gives more information on the
47 particular command or argument type.
48  
49 END
50  
51 4 $HELP{'toc'}=[<<'END'];
52  
53 Help items:
54 -----------
55  
56 toc - this page
57  
58 XSH Language Topics:
59  
60 Argtypes - Argument Types
61 Configuration - Options
62 Documents - Files/Documents
63 Flow - Flow control
64 Information - Retrieving more information
65 Manipulation - Tree modification
66 Navigation - Tree navigation
67 Perl_shell - Interacting with Perl and Shell
68 Variables - Variables
69  
70 XSH Commands:
71  
72 assign, backups, call, catalog, cd, clone, close, copy, count, create,
73 debug, def, defs, doc-info, documents, dtd, empty-tags, enc, encoding,
74 exec, exit, fold, foreach, help, if, ifinclude, include, indent,
75 insert, iterate, keep-blanks, last, lcd, load-ext-dtd, local, locate,
76 ls, map, move, namespaces, next, nobackups, nodebug, normalize, open,
77 options, parser-completes-attributes, parser-expands-entities,
78 parser-expands-xinclude, pedantic-parser, perl, prev, print,
79 process-xinclude, pwd, query-encoding, quiet, recovering, redo,
80 register-function, register-namespace, register-xhtml-namespace,
81 register-xsh-namespace, remove, rename, return, run-mode, save, select,
82 set-enc, set-standalone, skip-dtd, sort, stream, strip-whitespace,
83 switch-to-new-documents, test-mode, throw, try, undef, unfold, unless,
84 unregister-function, unregister-namespace, valid, validate, validation,
85 variables, verbose, version, while, xcopy, xinsert, xmove,
86 xpath-axis-completion, xpath-completion, xslt, xupdate
87  
88 XSH Argument Types:
89  
90 command-block, enc-string, expression, filename, id, location,
91 node-type, perl-code, xpath
92  
93 END
94  
95 4 $HELP{'command'}=[<<'END'];
96 List of XSH commands
97  
98 description:
99 assign, backups, call, catalog, cd, clone, close, copy, count,
100 create, debug, def, defs, doc-info, documents, dtd,
101 empty-tags, enc, encoding, exec, exit, fold, foreach, help,
102 if, ifinclude, include, indent, insert, iterate, keep-blanks,
103 last, lcd, load-ext-dtd, local, locate, ls, map, move,
104 namespaces, next, nobackups, nodebug, normalize, open,
105 options, parser-completes-attributes, parser-expands-entities,
106 parser-expands-xinclude, pedantic-parser, perl, prev, print,
107 process-xinclude, pwd, query-encoding, quiet, recovering,
108 redo, register-function, register-namespace,
109 register-xhtml-namespace, register-xsh-namespace, remove,
110 rename, return, run-mode, save, select, set-enc,
111 set-standalone, skip-dtd, sort, stream, strip-whitespace,
112 switch-to-new-documents, test-mode, throw, try, undef, unfold,
113 unless, unregister-function, unregister-namespace, valid,
114 validate, validation, variables, verbose, version, while,
115 xcopy, xinsert, xmove, xpath-axis-completion,
116 xpath-completion, xslt, xupdate
117  
118 END
119  
120  
121 4 $HELP{'command-block'}=[<<'END'];
122 command-block argument type
123  
124 description:
125 XSH command or a block of semicolon-separated commands
126 enclosed within curly brackets.
127  
128 Example: Count paragraphs in each chapter
129  
130 $i=0;
131 foreach //chapter {
132 $c=count(./para);
133 $i=$i+1;
134 print "$c paragraphs in chapter no.$i";
135 }
136  
137 END
138  
139  
140 4 $HELP{'type'}=[<<'END'];
141 List of command argument types
142  
143 description:
144 command-block, enc-string, expression, filename, id, location,
145 node-type, perl-code, xpath
146  
147 END
148  
149  
150 4 $HELP{'expression'}=[<<'END'];
151 expression argument type
152  
153 description:
154 Expression is a string consisting of unquoted characters other
155 than whitespace or semicolon, single quote or double quote
156 characters or quoted characters of any kind (but see also
157 special case of expression - so called here-documents -
158 described below). Quoting means either preceding a single
159 character with a backslash or enclosing a part of the string
160 into single quotes '...' or double quotes "...". Quoting
161 characters are removed from the string so they must be quoted
162 themselves if they are a part of the expression: \\, \' or " '
163 ", \" or ' " '. Unquoted (sub)expressons and (sub)expressions
164 quoted with double-quotes are subject to variable, Perl, and
165 XPath expansions.
166  
167 Variable expansion replaces substrings of the form $id or
168 ${id} with the value of the variable named $id, unless the '$'
169 sign is quoted.
170  
171 Perl expansion evaluates every substring enclosed in between
172 `${{{' and `}}}' as a Perl expresson (in very much the same
173 way as the <perl> command) and replaces the whole thing with
174 the resulting value.
175  
176 XPath interpolation evaluates every substring enclosed in
177 between `${{' and `}}' as an XPath expression (in very much
178 the same way as the <count> command) and substitutes the whole
179 thing with the resul.
180  
181 For convenience, another kind XPath interpolation is performed
182 on expressions. It replaces any substring occuring between
183 `${(' and `)}' with a literal result of XPath evaluation of
184 the string. This means, that if the evaluation results in a
185 node-list, the textual content of its first node is
186 substituted rather than the number of nodes in the node-list
187 (as with `${{ ... }}').
188  
189 Example:
190 echo foo "bar" # prints: foo bar
191 echo foo"bar" # prints: foobar
192 echo foo'"bar"' # prints: foo"bar"
193 echo foo"'b\\a\"r'" # prints: foo'b\a"r'
194 $a="bar"
195 echo foo$a # prints: foobar
196 echo foo\$a # prints: foo$a
197 echo '$a' # prints: '$a'
198 echo "'$a'" # prints: 'bar'
199 echo "${{//middle-earth/creatures}}" # prints: 10
200 echo '${{//middle-earth/creatures}}' # prints: ${{//middle-earth/creatures}}
201 echo ${{//creature[1]/@name}} # !!! prints: 1
202 echo ${(//creature[1]/@name)} # prints: Bilbo
203 echo ${{{ join(",",split(//,$a)) }}} # prints: b,a,r
204  
205 There is one more special type of expressions, so called
206 ``here-documents'' following syntax of similar constructs in
207 Bash and Perl. Following a `<<' you specify a string to
208 terminate the quoted material, and all lines following the
209 current line down to the terminating string are the value of
210 the expression. The terminating string is either quoted or
211 unquoted identifier (a word). If quoted, the type of quotes
212 you use determines the treatment of the text, just as in
213 regular quoting, i.e. in case of double quotes, the material
214 contained in the here-document is subject to variable, Perl,
215 and XPath expansions. An unquoted identifier works just like
216 double quotes. There must be no space between the `<<' and the
217 identifier. The terminating string must appear by itself
218 (unquoted and with no surrounding whitespace) on the
219 terminating line.
220  
221 Example:
222 $a="bar"
223 echo foo <<END baz;
224 xx $a yy
225 END
226 # prints foo xx bar yy baz
227 echo foo <<"END" baz;
228 xx $a yy
229 END
230 # same as above
231 echo foo <<'END' baz;
232 xx $a yy
233 END
234 # prints foo xx $a yy baz
235  
236 END
237  
238  
239 4 $HELP{'enc-string'}=[<<'END'];
240 enc_string argument type
241  
242 description:
243 An <expression> which interpolates to a valid encoding string,
244 e.g. to utf-8, utf-16, iso-8859-1, iso-8859-2, windows-1250
245 etc.
246  
247 END
248  
249  
250 4 $HELP{'id'}=[<<'END'];
251 id argument type
252  
253 description:
254 An identifier, that is, a string beginning with a letter or
255 underscore, and containing letters, underscores, and digits.
256  
257 END
258  
259  
260 4 $HELP{'filename'}=[<<'END'];
261 Filename argument type
262  
263 description:
264 An <expression> which interpolates to a valid file name or
265 URL.
266  
267 END
268  
269  
270 4 $HELP{'xpath'}=[<<'END'];
271 XPath argument type
272  
273 description:
274 XSH supports arbitrary XPath expression as defined in W3C
275 recommendation at http://www.w3.org/TR/xpath. (Nice
276 interactive XPath tutorials and references can be found at
277 http://www.zvon.org.) In XSH, XPath expressoin may be
278 optionally preceded with a document identifier followed by
279 colon (<id>:xpath). If no document identifier is given, the
280 current document is used.
281  
282 As an extension, the following XPath extension functions are
283 defined in the XSH namespace:
284  
285 `xsh:doc(id-string)' - returns a nodelist consisting of the
286 document node associated in XSH with an identifier given in
287 `id-string'.
288  
289 `xsh:matches(match-string,regexp-string)' - returns `true' if
290 `match-string' matches the regular expression given in
291 `regexp-string'. Otherwise returns `false'.
292  
293 `xsh:grep(node-set, regexp-string)' - returns a node set
294 consisting of nodes of the given `node-set' whose content (as
295 returned by the built-in XPath function `string()') matches
296 the regular expression given in `regexp-string'.
297  
298 `xsh:same(node-set1, node-set2)' - returns `true' if the given
299 node sets both contain the same node (in XPath, this can also
300 be expressed as
301 `count(node-set1|node-set2)+count(node-set1)+count(node-set2)=
302 1').
303  
304 Example: Open a document and count all sections containing a subsection
305  
306 xsh scratch:/> open v = mydocument1.xml;
307 xsh v:/> open k = mydocument2.xml;
308 xsh k:/> count //section[subsection]; # searches k
309 xsh k:/> count v://section[subsection]; # searches v
310  
311 END
312  
313  
314 4 $HELP{'try'}=[<<'END'];
315 usage: try <command-block> catch [[local] $<id>] <command-block>
316
317 description:
318 Execute <command-block> following the `try' keyword. If an
319 error or exception occures during the evaluation, execute the
320 `catch' <command-block>. If a variable follows `catch' and the
321 `try' block fails, an error message of the exception occured
322 is stored to the variable before the `catch' block is
323 executed. Optionally, the variable name may be preceded with
324 the keyword `local' in order to make the assignment local to
325 the `catch' block (see <local>).
326  
327 The <throw> command and the equivalent perl construction `perl
328 { die "error message" }' allow user to throw custom
329 exceptions.
330  
331 Example: Handle parse errors
332  
333 try {
334 open XML doc=$input;
335 } catch {
336 try {
337 echo "XML parser failed, trying HTML";
338 open HTML doc=$input;
339 } catch local $error {
340 echo "Stopping due to errors: $error";
341 exit 1;
342 }
343 }
344  
345 see also: throw
346  
347 END
348  
349  
350 4 $HELP{'if'}=[<<'END'];
351 usage: if <xpath>|<perl-code> <command>
352 if <xpath>|<perl-code>
353 <command-block> [ elsif <command-block> ]* [ else <command-block> ]
354
355 description:
356 Execute <command-block> if a given <xpath> or <perl-code>
357 expression evaluates to a non-emtpty node-list, true
358 boolean-value, non-zero number or non-empty literal. If the
359 first test fails, check all possibly following `elsif'
360 conditions and execute the corresponding <command-block> for
361 the first one of them which is true. If none of them succeeds,
362 execute the `else' <command-block> (if any).
363  
364 Example: Display node type
365  
366 def node_type %n {
367 foreach (%n) {
368 if ( . = self::* ) { # XPath trick to check if . is an element
369 echo 'element';
370 } elsif ( . = ../@* ) { # XPath trick to check if . is an attribute
371 echo 'attribute';
372 } elsif ( . = ../processing-instruction() ) {
373 echo 'pi';
374 } elsif ( . = ../text() ) {
375 echo 'text';
376 } elsif ( . = ../comment() ) {
377 echo 'comment'
378 } else { # well, this should not happen, but anyway, ...
379 echo 'unknown-type';
380 }
381 }
382 }
383  
384 END
385  
386  
387 4 $HELP{'unless'}=[<<'END'];
388 usage: unless <xpath>|<perl-code>
389 <command>
390 unless <xpath>|<perl-code>
391 <command-block> [ else <command-block> ]
392
393 description:
394 Like if but negating the result of the expression.
395  
396 see also: if
397  
398 END
399  
400  
401 4 $HELP{'while'}=[<<'END'];
402 usage: while <xpath>|<perl-code> <command-block>
403
404 description:
405 Execute <command-block> as long as the given <xpath> or
406 <perl-code> expression evaluates to a non-emtpty node-list,
407 true boolean-value, non-zero number or non-empty literal.
408  
409 Example: The commands have the same results
410  
411 xsh> while /table/row remove /table/row[1];
412 xsh> remove /table/row;
413  
414 END
415  
416  
417 4 $HELP{'foreach'}=[<<'END'];
418 usage: foreach <xpath>|<perl-code>
419 <command>|<command-block>
420
421 aliases: for
422  
423 description:
424 If the first argument is an <xpath> expression, execute the
425 command-block for each node matching the expression making it
426 temporarily the current node, so that all relative XPath
427 expressions are evaluated in its context.
428  
429 If the first argument is a <perl-code>, it is evaluated and
430 the resulting perl-list is iterated setting the variable $__
431 (note that there are two underscores!) to be each element of
432 the list in turn. It works much like perl's foreach, except
433 that the variable used consists of two underscores.
434  
435 Example: Move all employee elements in a company element into a staff
436 subelement of the same company
437  
438 xsh> foreach //company xmove ./employee into ./staff;
439  
440 Example: List content of all XML files in current directory
441  
442 xsh> foreach { glob('*.xml') } { open f=$__; list f:/; }
443  
444 END
445  
446 4 $HELP{'for'}=$HELP{'foreach'};
447  
448 4 $HELP{'undef'}=[<<'END'];
449 usage: undef <expression>
450
451 aliases: undefine
452  
453 description:
454 This command can be used to undefine previously defined XSH
455 subroutines. The <expression> is evaluated as a Perl regular
456 expression. All subroutines whose names match are undefined.
457 Note, that like `def', all `undef' commands are processed
458 during the compilation of the source code, not at run-time, so
459 it doesn't matter how deep in the code is a `undef' command
460 nested.
461  
462 Example:
463 xsh> include my_defs.xsh
464 xsh> call my_sub1 //foo;
465 xsh> call my_sub2 //bar;
466 xsh> undefine 'my_sub.*'
467 xsh> # change/edit the definitions in my_defs.xsh and reload
468 xsh> include my_defs.xsh
469  
470 END
471  
472 4 $HELP{'undefine'}=$HELP{'undef'};
473  
474 4 $HELP{'def'}=[<<'END'];
475 usage: def <id> [$<id> | %<id>]* <command-block>
476 or
477 def <id> [$<id> | %<id>]*;
478
479 aliases: define
480  
481 description:
482 Define a new XSH subroutine named <id>. The subroutine may
483 require zero or more parameters of nodelist or string type.
484 These are declared as a whitespace-separated list of (so
485 called) parametric variables (of nodelist or string type). The
486 body of the subroutine is specified as a <command-block>.
487 Note, that all subroutine declarations are processed during
488 the parsing and not at run-time, so it does not matter where
489 the subroutine is defined.
490  
491 The routine can be later invoked using the <call> command
492 followed by the routine name and parameters. Nodelist
493 parameters must be given as an XPath expressions, and are
494 evaluated just before the subroutine's body is executed.
495 String parameters must be given as (string) <expression>s.
496 Resulting node-lists/strings are stored into the parametric
497 variables before the body is executed. These variables are
498 local to the subroutine's call tree (see also the <local>
499 command). If there is a global variable using the same name as
500 some parametric variable, the original value of the global
501 variable is replaced with the value of the parametric variable
502 for the time of the subroutine's run-time.
503  
504 Note that subroutine has to be declared before it is called
505 with <call>. If you cannot do so, e.g. if you want to call a
506 subroutine recursively, you have to pre-declare the subroutine
507 using a `def' with no <command-block>. There may be only one
508 full declaration (and possibly one pre-declaration) of a
509 subroutine for one <id> and the declaration and
510 pre-declaration has to define the same number of arguments and
511 their types must match.
512  
513 Example:
514 def l3 %v {
515 ls %v 3; # list given nodes upto depth 3
516 }
517 call l3 //chapter;
518  
519 Example: Commenting and un-commenting pieces of document
520  
521 def comment
522 %n # nodes to move to comments
523 $mark # maybe some handy mark to recognize such comments
524 {
525 foreach %n {
526 if ( . = ../@* ) {
527 echo "Warning: attribute nodes are not supported!";
528 } else {
529 echo "Commenting out:";
530 ls .;
531 local $node = "";
532 ls . |> $node;
533 add comment "$mark$node" replace .;
534 }
535 }
536 }
537
538 def uncomment %n $mark {
539 foreach %n {
540 if (. = ../comment()) { # is this node a comment node
541 local $string = substring-after(.,"$mark");
542 add chunk $string replace .;
543 } else {
544 echo "Warning: Ignoring non-comment node:";
545 ls . 0;
546 }
547 }
548 }
549
550
551 # comment out all chapters with no paragraphs
552 call comment //chapter[not(para)] "COMMENT-NOPARA";
553
554 # uncomment all comments (may not always be valid!)
555 $mark="COMMENT-NOPARA";
556 call uncomment //comment()[starts-with(.,"$mark")] $mark;
557  
558 see also: call return local
559  
560 END
561  
562 4 $HELP{'define'}=$HELP{'def'};
563  
564 4 $HELP{'assign'}=[<<'END'];
565 usage: assign $<id>=<xpath>
566 $<id>=<xpath>
567 assign %<id>=<xpath>
568 %<id>=<xpath>
569
570 description:
571 In the first two cases (where dollar sign appears) store the
572 result of evaluation of the <xpath> in a variable named $<id>.
573 In this case, <xpath> is evaluated in a simmilar way as in the
574 case of the <count>: if it results in a literal value this
575 value is used. If it results in a node-list, number of nodes
576 occuring in that node-list is used. Use the `string()' XPath
577 function to obtain a literal values in these cases.
578  
579 Example: String expressions
580  
581 xsh> $a=string(chapter/title)
582 xsh> $b="hallo world"
583  
584 Example: Arithmetic expressions
585  
586 xsh> $a=5*100
587 xsh> $a
588 $a=500
589 xsh> $a=(($a+5) div 10)
590 xsh> $a
591 $a=50.5
592  
593 Example: Counting nodes
594  
595 xsh> $a=//chapter
596 xsh> $a
597 $a=10
598 xsh> %chapters=//chapter
599 xsh> $a=%chapters
600 xsh> $a
601 $a=10
602  
603 Example: Some caveats of counting node-lists
604  
605 xsh> ls ./creature
606 <creature race='hobbit' name="Bilbo"/>
607
608 ## WRONG (@name results in a singleton node-list) !!!
609 xsh> $name=@name
610 xsh> $name
611 $name=1
612
613 ## CORRECT (use string() function)
614 xsh> $name=string(@name)
615 xsh> $name
616 $name=Bilbo
617  
618 In the other two cases (where percent sign appears) find all
619 nodes matching a given <xpath> and store the resulting
620 node-list in the variable named %<id>. The variable may be
621 later used instead of an XPath expression.
622  
623 see also: variables
624  
625 END
626  
627  
628 4 $HELP{'local'}=[<<'END'];
629 usage: local $<id> = <xpath>
630 local %<id> = <xpath>
631 local $<id>|%<id> [ $<id>|%<id> ... ]
632
633 description:
634 This command acts in a very similar way as <assign> does,
635 except that the variable assignment is done temporarily and
636 lasts only for the rest of the nearest enclosing
637 <command-block>. At the end of the enclosing block or
638 subroutine the original value is restored. This command may
639 also be used without the assignment part and assignments may
640 be done later using the usual <assign> command.
641  
642 Note, that the variable itself is not lexically is still
643 global in the sense that it is still visible to any subroutine
644 called subsequently from within the same block. A local just
645 gives temporary values to global (meaning package) variables.
646 Unlike Perl's `my' declarations it does not create a local
647 variable. This is known as dynamic scoping. Lexical scoping is
648 not implemented in XSH.
649  
650 To sum up for Perl programmers: `local' in XSH works exactly
651 the same as `local' in Perl.
652  
653 see also: assign def
654  
655 END
656  
657  
658 4 $HELP{'options'}=[<<'END'];
659 usage: options
660
661 aliases: flags
662  
663 description:
664 List current values of all XSH flags and options (such as
665 validation flag or query-encoding).
666  
667 Example: Store current settings in your .xshrc
668  
669 xsh> options | cat > ~/.xshrc
670  
671 END
672  
673 4 $HELP{'flags'}=$HELP{'options'};
674  
675 4 $HELP{'defs'}=[<<'END'];
676 usage: defs
677
678 description:
679 List names and parametric variables for all defined XSH
680 routines.
681  
682 see also: def variables
683  
684 END
685  
686  
687 4 $HELP{'ifinclude'}=[<<'END'];
688 usage: ifinclude <filename>
689
690 description:
691 Include a file named <filename> and execute all XSH commands
692 therein unless the file was already included using either
693 <include> of <ifinclude>.
694  
695 see also: include
696  
697 END
698  
699  
700 4 $HELP{'include'}=[<<'END'];
701 usage: include <filename>
702
703 aliases: .
704  
705 description:
706 Include a file named <filename> and execute all XSH commands
707 therein.
708  
709 see also: ifinclude
710  
711 END
712  
713 4 $HELP{'.'}=$HELP{'include'};
714  
715 4 $HELP{'call'}=[<<'END'];
716 usage: call <id> [<xpath> | <expression>]*
717
718 description:
719 Call an XSH subroutine named <id> previously created using
720 def. If the subroutine requires some paramters, these have to
721 be specified after the <id>. Node-list parameters are given by
722 means of <xpath> expressions. String parameters have to be
723 string <expression>s.
724  
725 see also: def return
726  
727 END
728  
729  
730 4 $HELP{'help'}=[<<'END'];
731 usage: help <command>|argument-type
732
733 aliases: ?
734  
735 description:
736 Print help on a given command or argument type.
737  
738 END
739  
740 4 $HELP{'?'}=$HELP{'help'};
741  
742 4 $HELP{'exec'}=[<<'END'];
743 usage: exec <expression> [<expression> ...]
744
745 aliases: system
746  
747 description:
748 execute the system command(s) in <expression>s.
749  
750 Example: Count words in "hallo wold" string, then print name of your
751 machine's operating system.
752  
753 exec echo hallo world; # prints hallo world
754 exec "echo hallo word" | wc; # counts words in hallo world
755 exec uname; # prints operating system name
756  
757 END
758  
759 4 $HELP{'system'}=$HELP{'exec'};
760  
761 4 $HELP{'xslt'}=[<<'END'];
762 usage: xslt <id> <filename> <id> [(params|parameters) name=<expression> [name=<expression> ...]]
763
764 aliases: transform xsl xsltproc process
765  
766 description:
767 Load an XSLT stylesheet from a file and use it to transform
768 the document of the first <id> into a new document named <id>.
769 Parameters may be passed to a stylesheet after params keyword
770 in the form of a list of name=value pairs where name is the
771 parameter name and value is an <expression> interpolating to
772 its value. The resulting value is interpretted by XSLT
773 processor as an XPath expression so e.g. quotes surrounding a
774 XPath string have to be quoted themselves to preveserve them
775 during the XSH expression interpolation.
776  
777 Example:
778 xslt src stylesheet.xsl rslt params font="'14pt'" color="'red'"
779  
780 END
781  
782 4 $HELP{'transform'}=$HELP{'xslt'};
783 4 $HELP{'xsl'}=$HELP{'xslt'};
784 4 $HELP{'xsltproc'}=$HELP{'xslt'};
785 4 $HELP{'process'}=$HELP{'xslt'};
786  
787 4 $HELP{'documents'}=[<<'END'];
788 usage: files
789
790 aliases: files docs
791  
792 description:
793 List open files and their identifiers.
794  
795 see also: open close
796  
797 END
798  
799 4 $HELP{'files'}=$HELP{'documents'};
800 4 $HELP{'docs'}=$HELP{'documents'};
801  
802 4 $HELP{'variables'}=[<<'END'];
803 usage: variables
804
805 aliases: vars var
806  
807 description:
808 List all defined variables and their values.
809  
810 see also: documents defs
811  
812 END
813  
814 4 $HELP{'vars'}=$HELP{'variables'};
815 4 $HELP{'var'}=$HELP{'variables'};
816  
817 4 $HELP{'copy'}=[<<'END'];
818 usage: copy <xpath> <location> <xpath>
819
820 aliases: cp
821  
822 description:
823 Copies nodes matching the first <xpath> to the destinations
824 determined by the <location> directive relative to the second
825 <xpath>. If more than one node matches the first <xpath> than
826 it is copied to the position relative to the corresponding
827 node matched by the second <xpath> according to the order in
828 which are nodes matched. Thus, the n'th node matching the
829 first <xpath> is copied to the location relative to the n'th
830 node matching the second <xpath>.
831  
832 The possible values for <location> are: after, before, into,
833 replace and cause copying the source nodes after, before, into
834 (as the last child-node). the destination nodes. If replace
835 <location> is used, the source node is copied before the
836 destination node and the destination node is removed.
837  
838 Some kind of type conversion is used when the types of the
839 source and destination nodes are not equal. Thus, text, cdata,
840 comment or processing instruction node data prepend, append or
841 replace value of a destination attribute when copied
842 before,after/into or instead (replace) an attribute, and vice
843 versa.
844  
845 Attributes may be copied after, before or into some other
846 attribute to append, prepend or replace the destination
847 attribute value. They may also replace the destination
848 attribute completely (both its name and value). To copy an
849 attribute from one element to another, simply copy the
850 attribute node into the destination element.
851  
852 Elements may be copied into other elements (which results in
853 appending the child-list of the destination element), or
854 before, after or instead (replace) other nodes of any type
855 except attributes.
856  
857 Example: Replace living-thing elements in the document b with the
858 coresponding creature elements of the document a.
859  
860 xsh> copy a://creature replace b://living-thing
861  
862 END
863  
864 4 $HELP{'cp'}=$HELP{'copy'};
865  
866 4 $HELP{'xcopy'}=[<<'END'];
867 usage: xcopy <xpath> <location> <xpath>
868
869 aliases: xcp
870  
871 description:
872 xcopy is similar to <copy>, but copies *all* nodes matching
873 the first <xpath> to *all* destinations determined by the
874 <location> directive relative to the second <xpath>. See
875 <copy> for detailed description of `xcopy' arguments.
876  
877 Example: Copy all middle-earth creatures within the document a into
878 every world of the document b.
879  
880 xsh> xcopy a:/middle-earth/creature into b://world
881  
882 END
883  
884 4 $HELP{'xcp'}=$HELP{'xcopy'};
885  
886 4 $HELP{'lcd'}=[<<'END'];
887 usage: lcd <expression>
888
889 aliases: chdir
890  
891 description:
892 Changes the filesystem working directory to <expression>, if
893 possible. If <expression> is omitted, changes to the directory
894 specified in HOME environment variable, if set; if not,
895 changes to the directory specified by LOGDIR environment
896 variable.
897  
898 END
899  
900 4 $HELP{'chdir'}=$HELP{'lcd'};
901  
902 4 $HELP{'insert'}=[<<'END'];
903 usage: insert <node-type> <expression> [namespace <expression>] <location> <xpath>
904
905 aliases: add
906  
907 description:
908 Works just like xadd, except that the new node is attached
909 only the first node matched.
910  
911 see also: xinsert move xmove
912  
913 END
914  
915 4 $HELP{'add'}=$HELP{'insert'};
916  
917 4 $HELP{'xinsert'}=[<<'END'];
918 usage: xinsert <node-type> <expression> [namespace <expression>] <location> <xpath>
919
920 aliases: xadd
921  
922 description:
923 Use the <expression> to create a new node of a given
924 <node-type> in the <location> relative to the given <xpath>.
925  
926 For element nodes, the format of the <expression> should look
927 like "<element-name att-name='attvalue' ...>". The `<' and `>'
928 characters are optional. If no attributes are used, the
929 expression may simply consist the element name. Note, that in
930 the first case, the quotes are required since the expression
931 contains spaces.
932  
933 Attribute nodes use the following syntax: "att-name='attvalue'
934 [...]".
935  
936 For the other types of nodes (text, cdata, comments) the
937 expression should contain the node's literal content. Again,
938 it is necessary to quote all whitespace and special characters
939 as in any expression argument.
940  
941 The <location> argument should be one of: `after', `before',
942 `into', `replace', `append' or `prepend'. See documentation of
943 the <location> argument type for more detail.
944  
945 The namespace <expression> is only valid for elements and
946 attributes and must evaluate to the namespace URI. In that
947 case, the element or attribute name must have a prefix. The
948 created node is associated with a given namespace.
949  
950 Example: Append a new Hobbit element to the list of middle-earth
951 creatures and name him Bilbo.
952  
953 xsh> xadd element "<creature race='hobbit' manner='good'>" \
954 into /middle-earth/creatures
955 xsh> xadd attribute "name='Bilbo'" \
956 into /middle-earth/creatures/creature[@race='hobbit'][last()]
957  
958 see also: insert move xmove
959  
960 END
961  
962 4 $HELP{'xadd'}=$HELP{'xinsert'};
963  
964 4 $HELP{'node-type'}=[<<'END'];
965 Node-type argument type
966  
967 description:
968 One of: element, attribute, text, cdata, comment, chunk and
969 (EXPERIMENTALLY!) entity_reference. A chunk is a character
970 string which forms a well-balanced peace of XML.
971  
972 Example:
973 add element hobbit into //middle-earth/creatures;
974 add attribute 'name="Bilbo"' into //middle-earth/creatures/hobbit[last()];
975 add chunk '<hobbit name="Frodo">A small guy from <place>Shire</place>.</hobbit>'
976 into //middle-earth/creatures;
977  
978 END
979  
980  
981 4 $HELP{'location'}=[<<'END'];
982 Location argument type
983  
984 description:
985 One of: `after', `before', `into', `append', `prepend',
986 `replace'.
987  
988 NOTE: XSH 1.6 introduces two new values for location argument:
989 `append' and `prepend' and slighlty changes behavior of
990 `after' and `before'!
991  
992 This argument is required by all commands that insert nodes to
993 a document in some way to a destination described by an XPath
994 expression. The meaning of the values listed above is supposed
995 be obvious in most cases, however the exact semantics for
996 location argument values depends on types of both the source
997 node and the target node.
998  
999 `after/before' place the node right after/before the
1000 destination node, except for when the destination node is a
1001 document node or one of the source nodes is an attribute: If
1002 the destination node is a document node, the source node is
1003 attached to the end/beginning of the document (remember: there
1004 is no "after/before a document"). If both the source and
1005 destination nodes are attributes, then the source node is
1006 simply attached to the element containing the destination node
1007 (remember: there is no order on attribute nodes). If the
1008 destination node is an attribute but the source node is of a
1009 different type, then the textual content of the source node is
1010 appended to the value of the destination attribute (i.e. in
1011 this case after/before act just as append/prepend).
1012  
1013 `append/prepend' appends/prepends the source node to the
1014 destination node. If the destination node can contain other
1015 nodes (i.e. it is an element or a document node) then the
1016 entire source node is attached to it. In case of other
1017 destination node types, the textual content of the source node
1018 is appended/prepended to the content of the destination node.
1019  
1020 `into' can also be used to place the source node to the end of
1021 an element (in the same way as `append'), to attach an
1022 attribute to an element, or, if the destination node is a text
1023 node, cdata section, processing-instruction, attribute or
1024 comment, to replace its textual content with the textual
1025 content of the source node.
1026  
1027 `replace' replaces the entire destination node with the source
1028 node except for the case when the destination node is an
1029 attribute and the source node is not. In such a case only the
1030 value of the destination attribute is replaced with the
1031 textual content of the source node. Note also that document
1032 node can never be replaced.
1033  
1034 END
1035  
1036  
1037 4 $HELP{'move'}=[<<'END'];
1038 usage: move <xpath> <location> <xpath>
1039
1040 aliases: mv
1041  
1042 description:
1043 `move' command acts exactly like <copy>, except that it
1044 removes the source nodes after a succesfull copy. Remember
1045 that the moved nodes are actually different nodes from the
1046 original ones (which may not be obvious when moving nodes
1047 within a single document into locations that do not require
1048 type conversion). So, after the move, the original nodes do
1049 not exist neither in the document itself nor any nodelist
1050 variable.
1051  
1052 See <copy> for more details on how the copies of the moved
1053 nodes are created.
1054  
1055 see also: copy xmove insert xinsert
1056  
1057 END
1058  
1059 4 $HELP{'mv'}=$HELP{'move'};
1060  
1061 4 $HELP{'xmove'}=[<<'END'];
1062 usage: xmove <xpath> <location> <xpath>
1063
1064 aliases: xmv
1065  
1066 description:
1067 Like <xcopy>, except that `xmove' removes the source nodes
1068 after a succesfull copy. Remember that the moved nodes are
1069 actually different nodes from the original ones (which may not
1070 be obvious when moving nodes within a single document into
1071 locations that do not require type conversion). So, after the
1072 move, the original nodes do not exist neither in the document
1073 itself nor any nodelist variable.
1074  
1075 See <xcopy> for more details on how the copies of the moved
1076 nodes are created.
1077  
1078 The following example demonstrates how `xcopy' can be used to
1079 get rid of HTML `<font>' elements while preserving their
1080 content. As an exercise, try to find out why simple `foreach
1081 //font { xmove node() replace . }' would not work here.
1082  
1083 Example: Get rid of all <font> tags
1084  
1085 while //font[1] {
1086 foreach //font[1] {
1087 xmove ./node() replace .;
1088 }
1089 }
1090  
1091 see also: xcopy move insert xinsert
1092  
1093 END
1094  
1095 4 $HELP{'xmv'}=$HELP{'xmove'};
1096  
1097 4 $HELP{'clone'}=[<<'END'];
1098 usage: clone <id>=<id>
1099
1100 aliases: dup
1101  
1102 description:
1103 Make a copy of the document identified by the <id> following
1104 the equal sign assigning it the identifier of the first <id>.
1105  
1106 see also: open close enc documents
1107  
1108 END
1109  
1110 4 $HELP{'dup'}=$HELP{'clone'};
1111  
1112 4 $HELP{'normalize'}=[<<'END'];
1113 usage: normalize <xpath>
1114
1115 description:
1116 `normalize' puts all text nodes in the full depth of the
1117 sub-tree underneath each node selected by a given <xpath>,
1118 into a "normal" form where only structure (e.g., elements,
1119 comments, processing instructions, CDATA sections, and entity
1120 references) separates text nodes, i.e., there are neither
1121 adjacent Text nodes nor empty Text nodes.
1122  
1123 END
1124  
1125  
1126 4 $HELP{'strip-whitespace'}=[<<'END'];
1127 usage: strip <xpath>
1128
1129 aliases: strip_whitespace
1130  
1131 description:
1132 `strip-whitespace' removes all leading and trailing whitespace
1133 from given nodes. If applied to an element node, it removes
1134 all leading and trailing child text nodes and CDATA sections
1135 that consist entirely of whitespace.
1136  
1137 END
1138  
1139 4 $HELP{'strip_whitespace'}=$HELP{'strip-whitespace'};
1140  
1141 4 $HELP{'ls'}=[<<'END'];
1142 usage: ls <xpath> [<expression>]
1143
1144 aliases: list
1145  
1146 description:
1147 List the XML representation of all nodes matching <xpath>. The
1148 optional <expression> argument may be provided to specify the
1149 depth of XML tree listing. If negative, the tree will be
1150 listed to unlimited depth. If the <expression> results in the
1151 word `fold', elements marked with the <fold> command are
1152 folded, i.e. listed only to a certain depth (this feature is
1153 still EXPERIMENTAL!).
1154  
1155 Unless in quiet mode, this command prints also number of nodes
1156 matched on stderr.
1157  
1158 If the <xpath> parameter is omitted, current context node is
1159 listed to the depth of 1.
1160  
1161 see also: count fold unfold
1162  
1163 END
1164  
1165 4 $HELP{'list'}=$HELP{'ls'};
1166  
1167 4 $HELP{'count'}=[<<'END'];
1168 usage: count <xpath>
1169
1170 aliases: print_value get
1171  
1172 description:
1173 Calculate a given <xpath> expression. If the result is a
1174 node-list, return number of nodes in the node-list. If the
1175 <xpath> results in a boolean, numeric or literal value, return
1176 the value.
1177  
1178 END
1179  
1180 4 $HELP{'print_value'}=$HELP{'count'};
1181 4 $HELP{'get'}=$HELP{'count'};
1182  
1183 4 $HELP{'perl-code'}=[<<'END'];
1184 Perl-code argument type
1185  
1186 description:
1187 A block of perl code enclosed in curly brackets or an
1188 expression which interpolates to a perl expression. Variables
1189 defined in XSH are visible in perl code as well. Since, in the
1190 interactive mode, XSH redirects output to the terminal, you
1191 cannot simply use perl print function for output if you want
1192 to filter the result with a shell command. Instead use the
1193 predefined perl routine `echo(...)' which is equivalent to
1194 Perl's `print $::OUT ...'. The `$::OUT' perl-variable stores
1195 the reference to the terminal file handle.
1196  
1197 For more information about embedded Perl code in XSH,
1198 predefined functions etc. see <Perl_shell>.
1199  
1200 Example:
1201 xsh> $i="foo";
1202 xsh> eval { echo "$i-bar\n"; } # prints foo-bar
1203 xsh> eval 'echo "\$i-bar\n";' # exactly the same as above
1204 xsh> eval 'echo "$i-bar\n";' # prints foo-bar too, but $i is
1205 # interpolated by XSH. Perl actually evaluates echo "foo-bar\n";
1206  
1207 END
1208  
1209  
1210 4 $HELP{'perl'}=[<<'END'];
1211 usage: eval <perl-code>
1212
1213 aliases: eval
1214  
1215 description:
1216 Evaluate a given perl expression.
1217  
1218 see also: count
1219  
1220 END
1221  
1222 4 $HELP{'eval'}=$HELP{'perl'};
1223  
1224 4 $HELP{'remove'}=[<<'END'];
1225 usage: remove <xpath>
1226
1227 aliases: rm prune delete del
1228  
1229 description:
1230 Remove all nodes matching <xpath>.
1231  
1232 Example: Get rid of all evil creatures.
1233  
1234 xsh> del //creature[@manner='evil']
1235  
1236 END
1237  
1238 4 $HELP{'rm'}=$HELP{'remove'};
1239 4 $HELP{'prune'}=$HELP{'remove'};
1240 4 $HELP{'delete'}=$HELP{'remove'};
1241 4 $HELP{'del'}=$HELP{'remove'};
1242  
1243 4 $HELP{'print'}=[<<'END'];
1244 usage: print <expression> [<expression> ...]
1245
1246 aliases: echo
1247  
1248 description:
1249 Interpolate and print a given expression(s).
1250  
1251 END
1252  
1253 4 $HELP{'echo'}=$HELP{'print'};
1254  
1255 4 $HELP{'sort'}=[<<'END'];
1256 usage: sort <xpath>|<perl-code> <perl-code> %<id>
1257
1258 description:
1259 EXPERIMENTAL! This command is not yet guaranteed to remain in
1260 the future releases.
1261  
1262 DOCUMENTATION OBSOLETE! Syntax changed!
1263  
1264 This command may be used to sort the node-list stored in the
1265 node-list variable <id>. First, for each node in the node-list
1266 %<id>, the first argument (either a <xpath> or <perl-code>
1267 expression), which serves as a sorting criterion, is evaluated
1268 in the context of the node and the obtained value is stored
1269 together with the node. (In case of <xpath> the result of
1270 whatever type is cast to a string). Then perl's sorting
1271 algorithm is used to sort the nodelist, consulting the second,
1272 <perl-code>, argument to compare nodes. Before the <perl-code>
1273 is evaluated, the values obtained from the previous evaluation
1274 of the sorting crierion argument on the two nodes being
1275 compared are stored into `$a' and `$b' variables in the
1276 respective order. The <perl-code> being consulted is supposed
1277 to return either -1 (the first node should come first), 0 (no
1278 order precedence), or 1 (the second node should come first).
1279 Note that Perl provides very convenient operators `cmp' and
1280 `<=>' for string and numeric comparison of this kind as shown
1281 in the examples below.
1282  
1283 Remember that `sort' (unlike <assign>, <if>, or <while>)
1284 evaluates the first <xpath> argument (the sorting criterion)
1285 in a way to obtain a string. Thus you need not to bother with
1286 wrapping node-queries with a `string()' function but you must
1287 remember to explicitly wrap the expression in `count()' if the
1288 number of the nodes is to be the sorting criterion.
1289  
1290 Example: Sort creatures by name (XPath-based sort) in ascending order
1291 using current locale settings
1292  
1293 xsh> local %c=/middle-earth[1]/creatures
1294 xsh> sort @name { use locale; lc($a) cmp lc($b) } %c
1295 xsh> xmove %c into /middle-earth[1]# replaces the creatures
1296  
1297 Example: Sort (descending order) a node-list by score (Perl-based sort)
1298  
1299 xsh> sort { $scores{ literal('@name') } } { $b <=> $a } %players
1300  
1301 END
1302  
1303  
1304 4 $HELP{'map'}=[<<'END'];
1305 usage: map <perl-code> <xpath>
1306
1307 aliases: sed
1308  
1309 description:
1310 This command provides an easy way to modify node's data
1311 (content) using arbitrary Perl code.
1312  
1313 Each of the nodes matching <xpath> is passes its data to the
1314 <perl-code> via the `$_' variable and receives the (possibly)
1315 modified data using the same variable.
1316  
1317 Since element nodes do not really have any proper content
1318 (they are only a storage for other nodes), node's name (tag)
1319 is used in case of elements. Note, however, that recent
1320 versions of XSH provide a special command <rename> with a very
1321 similar syntax to `map', that should be used for renaming
1322 element, attribute, and processing instruction nodes.
1323  
1324 Example: Capitalises all hobbit names
1325  
1326 xsh> map { $_=ucfirst($_) } //hobbit/@name
1327  
1328 Example: Changes goblins to orcs in all hobbit tales.
1329  
1330 xsh> map { s/goblin/orc/gi } //hobbit/tale/text()
1331  
1332 END
1333  
1334 4 $HELP{'sed'}=$HELP{'map'};
1335  
1336 4 $HELP{'rename'}=[<<'END'];
1337 usage: rename <perl-code> <xpath>
1338
1339 description:
1340 This command is very similar to the <map> command, except that
1341 it operates on nodes' names rather than their data/values. For
1342 every element, attribute or processing-instruction matched by
1343 the <xpath> expression the following procedure is used: 1) the
1344 name of the node is stored into Perl's `$_' variable, 2) the
1345 <perl-code> is evaluated, and 3) the (posibly changed) content
1346 of the `$_' variable is used as a new name for the node.
1347  
1348 Example: Renames all hobbits to halflings
1349  
1350 xsh> map $_='halfling' //hobbit
1351  
1352 Example: Make all elements and attributes uppercase
1353  
1354 xsh> map { $_=uc($_) } (//*|//@*)
1355  
1356 see also: map
1357  
1358 END
1359  
1360  
1361 4 $HELP{'close'}=[<<'END'];
1362 usage: close [<id>]
1363
1364 description:
1365 Close the document identified by <id>, removing its parse-tree
1366 from memory (note also that all nodes belonging to the
1367 document are removed from all nodelists they appear in). If
1368 <id> is omitted, the command closes the current document.
1369  
1370 END
1371  
1372  
1373 4 $HELP{'select'}=[<<'END'];
1374 usage: select <id>
1375
1376 description:
1377 Make <id> the document identifier to be used in the next xpath
1378 evaluation without identifier prefix.
1379  
1380 Example:
1381 xsh> a=mydoc1.xml # opens and selects a
1382 xsh> ls / # lists a
1383 xsh> b=mydoc2.xml # opens and selects b
1384 xsh> ls / # lists b
1385 xsh> ls a:/ # lists and selects a
1386 xsh> select b # does nothing except selecting b
1387 xsh> ls / # lists b
1388  
1389 END
1390  
1391  
1392 4 $HELP{'open'}=[<<'END'];
1393 usage: [open [HTML|XML|DOCBOOK] [FILE|PIPE|STRING]] <id>=<expression>
1394
1395 description:
1396 Load a new XML, HTML or SGML DOCBOOK document from the file
1397 (actually arbitrary URL), command output or string provided by
1398 the <expression>. In XSH the document is given a symbolic name
1399 <id>. To identify the documentin commands like close, save,
1400 validate, dtd or enc simply use <id>. In commands which work
1401 on document nodes, give <id>: prefix to XPath expressions to
1402 point the XPath to the document.
1403  
1404 Example:
1405 xsh> open x=mydoc.xml # open a document
1406
1407 # open a HTML document from the Internet
1408 xsh> open HTML h="http://www.google.com/?q=xsh"
1409 # quote file name if it contains whitespace
1410 xsh> open y="document with a long name with spaces.xml"
1411
1412 # you may omit the word open when loading an XML file/URI.
1413 xsh> z=mybook.xml
1414
1415 # use HTML or DOCBOOK keywords to load these types
1416 xsh> open HTML z=index.htm
1417
1418 # use PIPE keyword to read output of a command
1419 xsh> open HTML PIPE z='wget -O - xsh.sourceforge.net/index.html'
1420
1421 # use z: prefix to identify the document opened with the
1422 # previous comand in an XPath expression.
1423 xsh> ls z://chapter/title
1424  
1425 END
1426  
1427  
1428 4 $HELP{'create'}=[<<'END'];
1429 usage: create <id> <expression>
1430
1431 aliases: new
1432  
1433 description:
1434 Create a new document using <expression> to form the root
1435 element and associate it with a given identifier.
1436  
1437 Example:
1438 xsh> create t1 root
1439 xsh> ls /
1440 <?xml version="1.0" encoding="utf-8"?>
1441 <root/>
1442
1443 xsh> create t2 "<root id='r0'>Just a <b>test</b></root>"
1444 xsh> ls /
1445 <?xml version="1.0" encoding="utf-8"?>
1446 <root id='r0'>Just a <b>test</b></root>
1447 xsh> files
1448 scratch = new_document.xml
1449 t1 = new_document1.xml
1450 t2 = new_document2.xml
1451  
1452 see also: open clone
1453  
1454 END
1455  
1456 4 $HELP{'new'}=$HELP{'create'};
1457  
1458 4 $HELP{'save'}=[<<'END'];
1459 usage: save [HTML|XML|XInclude] [FILE|PIPE|STRING] <id> <expression> [encoding <enc-string>]
1460 or
1461 save <id>
1462 or
1463 save
1464
1465 description:
1466 Save the document identified by <id>. Using one of the `FILE',
1467 `PIPE', `STRING' keywords the user may choose to save the
1468 document to a file send it to a given command's input via a
1469 pipe or simply return its content as a string. If none of the
1470 keywords is used, it defaults to FILE. If saving to a PIPE,
1471 the <expression> argument must provide the coresponding
1472 command and all its parameters. If saving to a FILE, the
1473 <expression> argument may provide a filename; if omitted, it
1474 defaults to the original filename of the document. If saving
1475 to a STRING, the <expression> argument is ignored and may
1476 freely be omitted.
1477  
1478 The output format is controlled using one of the XML, HTML,
1479 XInclude keywords (see below). If the format keyword is
1480 ommited, save it defaults to XML.
1481  
1482 Note, that a document should be saved as HTML only if it
1483 actually is a HTML document. Note also, that the optional
1484 encoding parameter forces character conversion only; it is up
1485 to the user to declare the document encoding in the
1486 appropriate HTML <META> tag.
1487  
1488 The XInclude keyword automatically implies XML format and can
1489 be used to force XSH to save all already expanded XInclude
1490 sections back to their original files while replacing them
1491 with <xi:include> tags in the main XML file. Moreover, all
1492 material included within <include> elements from the
1493 http://www.w3.org/2001/XInclude namespace is saved to separate
1494 files too according to the `href' attribute, leaving only
1495 empty <include> element in the root file. This feature may be
1496 used to split the document to new XInclude fragments.
1497  
1498 The encoding keyword followed by a <enc-string> can be used to
1499 convert the document from its original encoding to a different
1500 encoding. In case of XML output, the <?xml?> declaration is
1501 changed accordingly. The new encoding is also set as the
1502 document encoding for the particular document.
1503  
1504 Example: Use save to preview a HTML document in Lynx
1505  
1506 save HTML PIPE mydoc 'lynx -stdin'
1507  
1508 see also: open close enc documents
1509  
1510 END
1511  
1512  
1513 4 $HELP{'dtd'}=[<<'END'];
1514 usage: dtd [<id>]
1515
1516 description:
1517 Print external or internal DTD for a given document. If no
1518 document identifier is given, the current document is used.
1519  
1520 see also: valid validate
1521  
1522 END
1523  
1524  
1525 4 $HELP{'set-enc'}=[<<'END'];
1526 usage: set-enc <enc-string> [<id>]
1527
1528 description:
1529 Changes character encoding of a given document. If no document
1530 <id> is given, the command applies to the current document.
1531 This has two effects: changing the XMLDecl encoding
1532 declaration in the document prolog to display the new encoding
1533 and making all future <save> operations on the document
1534 default to the given charset.
1535  
1536 Example:
1537 xsh> ls
1538 <?xml version="1.0" encoding="iso-8859-1"?>
1539 <foo>...</foo>
1540 xsh> set-enc "utf-8"
1541 xsh> ls
1542 <?xml version="1.0" encoding="utf-8"?>
1543 <foo>...</foo>
1544 xsh> save # saves the file in UTF-8 encoding
1545  
1546 see also: enc doc-info
1547  
1548 END
1549  
1550  
1551 4 $HELP{'set-standalone'}=[<<'END'];
1552 usage: set-standalone <expression> [<id>]
1553
1554 description:
1555 Changes the value of `standalone' declaration in the XMLDecl
1556 prolog of a document. The <expression> should evaluate to
1557 either 1 or 0 or `'yes'' or `'no''. The result of applying the
1558 command on other values is not specified. If no document <id>
1559 is given, the command applies to the current document.
1560  
1561 see also: doc-info
1562  
1563 END
1564  
1565  
1566 4 $HELP{'enc'}=[<<'END'];
1567 usage: enc [<id>]
1568
1569 description:
1570 Print the original document encoding string. If no document
1571 identifier is given, the current document is used.
1572  
1573 see also: set-enc
1574  
1575 END
1576  
1577  
1578 4 $HELP{'validate'}=[<<'END'];
1579 usage: validate [<id>]
1580 or
1581 validate DTD PUBLIC <expression> [SYSTEM <filename>] <id>
1582 or
1583 validate (DTD|RelaxNG|XSD) FILE <filename> [<id>]
1584 or
1585 validate (DTD|RelaxNG|XSD) STRING <filename> [<id>]
1586 or
1587 validate (RelaxNG|XSD) DOC <id> [<id>]
1588
1589 description:
1590 This command validates the document identified with <id>
1591 against a DTD, RelaxNG or XSD schema and report all validity
1592 errors. If no document identifier is given, the current
1593 document is used. A DTD can be specified either by its PUBLIC
1594 or SYSTEM identifier (or both), or as a STRING. RelaxNG and
1595 XSD schemas can be specified either as a filename or url (FILE
1596 <filename>), as a string (STRING <expression>), or as a XSH
1597 document (DOC <id>). If no schema is specified, validation is
1598 performed against the internal or external DTD subset of the
1599 document being validated.
1600  
1601 Example:
1602 open mydoc="test.xml"
1603 # in all examples below, mydoc can be ommited
1604 validate mydoc; # validate against the documet's DOCTYPE
1605 validate DTD PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" mydoc
1606 validate DTD SYSTEM "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" mydoc
1607 validate DTD FILE "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" mydoc
1608  
1609 Example:
1610 validate RelaxNG FILE "test.rng" mydoc
1611 validate RelaxNG STRING $relaxschema mydoc
1612 open rng="test.rng"
1613 validate RelaxNG DOC rng mydoc
1614  
1615 Example:
1616 validate XSD FILE "test.xsd" mydoc
1617 validate XSD STRING $xsdschema mydoc
1618 open xsd="test.xsd"
1619 validate XSD DOC xsd mydoc
1620  
1621 see also: valid dtd
1622  
1623 END
1624  
1625  
1626 4 $HELP{'valid'}=[<<'END'];
1627 usage: valid [validation-scheme] [<id>]
1628
1629 description:
1630 Check and report the validity of the document <id> with
1631 respect to a DTD, RelaxNG, or XSD schemas specified in
1632 `validation-scheme' (see <validate> for information, on how
1633 `validation-scheme' may be specified). Prints "yes" if the
1634 document is valid and "no" otherwise. If no document
1635 identifier is given, the current document is used. If no
1636 `validation-scheme' is specified, the validity against the DTD
1637 subset is checked.
1638  
1639 see also: validate dtd
1640  
1641 END
1642  
1643  
1644 4 $HELP{'exit'}=[<<'END'];
1645 usage: exit [<expression>]
1646
1647 aliases: quit
1648  
1649 description:
1650 Exit xsh immediately, optionally with the exit-code resulting
1651 from a given expression.
1652  
1653 WARNING: No files are saved on exit.
1654  
1655 END
1656  
1657 4 $HELP{'quit'}=$HELP{'exit'};
1658  
1659 4 $HELP{'process-xinclude'}=[<<'END'];
1660 usage: process_xinclude [<id>]
1661
1662 aliases: process_xinclude process-xincludes process_xincludes xinclude xincludes load_xincludes load-xincludes load_xinclude load-xinclude
1663  
1664 description:
1665 Process any xinclude tags in the document <id>.
1666  
1667 see also: parser-expands-xinclude
1668  
1669 END
1670  
1671 4 $HELP{'process_xinclude'}=$HELP{'process-xinclude'};
1672 4 $HELP{'process-xincludes'}=$HELP{'process-xinclude'};
1673 4 $HELP{'process_xincludes'}=$HELP{'process-xinclude'};
1674 4 $HELP{'xinclude'}=$HELP{'process-xinclude'};
1675 4 $HELP{'xincludes'}=$HELP{'process-xinclude'};
1676 4 $HELP{'load_xincludes'}=$HELP{'process-xinclude'};
1677 4 $HELP{'load-xincludes'}=$HELP{'process-xinclude'};
1678 4 $HELP{'load_xinclude'}=$HELP{'process-xinclude'};
1679 4 $HELP{'load-xinclude'}=$HELP{'process-xinclude'};
1680  
1681 4 $HELP{'cd'}=[<<'END'];
1682 usage: cd [<xpath>]
1683
1684 aliases: chxpath
1685  
1686 description:
1687 Change current context node (and current document) to the
1688 first node matching a given <xpath> argument.
1689  
1690 END
1691  
1692 4 $HELP{'chxpath'}=$HELP{'cd'};
1693  
1694 4 $HELP{'pwd'}=[<<'END'];
1695 usage: pwd
1696
1697 description:
1698 Print XPath leading to the current context node. This is
1699 equivalent to `locate .'.
1700  
1701 see also: locate
1702  
1703 END
1704  
1705  
1706 4 $HELP{'locate'}=[<<'END'];
1707 usage: locate <xpath>
1708
1709 description:
1710 Print canonical XPaths leading to nodes matched by a given
1711 <xpath>.
1712  
1713 see also: pwd
1714  
1715 END
1716  
1717  
1718 4 $HELP{'xupdate'}=[<<'END'];
1719 usage: xupdate <id> [<id>]
1720
1721 description:
1722 Modify the current document or the document specified by the
1723 second <id> argument according to XUpdate commands of the
1724 first <id> document. XUpdate is a XML Update Language which
1725 aims to be a language for updating XML documents.
1726  
1727 XUpdate langauge is described in XUpdate Working Draft at
1728 http://www.xmldb.org/xupdate/xupdate-wd.html.
1729  
1730 XUpdate output can be generated for example by Python xmldiff
1731 utility from http://www.logilab.org/xmldiff/. Unfortunatelly,
1732 there are few bugs (or, as I tend to say In case of Python,
1733 white-space problems) in their code, so its XUpdate output is
1734 not always correct.
1735  
1736 END
1737  
1738  
1739 4 $HELP{'verbose'}=[<<'END'];
1740 usage: verbose
1741
1742 description:
1743 Turn on verbose messages (default).
1744  
1745 This is equivalent to setting `$QUIET' variable to 0.
1746  
1747 see also: quiet
1748  
1749 END
1750  
1751  
1752 4 $HELP{'test-mode'}=[<<'END'];
1753 usage: test-mode
1754
1755 aliases: test_mode
1756  
1757 description:
1758 Switch into test mode in which no commands are actually
1759 executed and only command syntax is checked.
1760  
1761 This is equivalent to setting `$TEST_MODE' variable to 1.
1762  
1763 see also: run-mode
1764  
1765 END
1766  
1767 4 $HELP{'test_mode'}=$HELP{'test-mode'};
1768  
1769 4 $HELP{'run-mode'}=[<<'END'];
1770 usage: run-mode
1771
1772 aliases: run_mode
1773  
1774 description:
1775 Switch into normal XSH mode in which all commands are
1776 executed.
1777  
1778 This is equivalent to setting `$TEST_MODE' variable to 0.
1779  
1780 see also: test-mode
1781  
1782 END
1783  
1784 4 $HELP{'run_mode'}=$HELP{'run-mode'};
1785  
1786 4 $HELP{'debug'}=[<<'END'];
1787 usage: debug
1788
1789 description:
1790 Turn on debugging messages.
1791  
1792 This is equivalent to setting `$DEBUG' variable to 1.
1793  
1794 see also: nodebug
1795  
1796 END
1797  
1798  
1799 4 $HELP{'nodebug'}=[<<'END'];
1800 usage: nodebug
1801
1802 description:
1803 Turn off debugging messages.
1804  
1805 This is equivalent to setting `$DEBUG' variable to 0.
1806  
1807 see also: debug
1808  
1809 END
1810  
1811  
1812 4 $HELP{'version'}=[<<'END'];
1813 usage: version
1814
1815 description:
1816 Prints program version plus version numbers of the most
1817 important libraries used.
1818  
1819 END
1820  
1821  
1822 4 $HELP{'validation'}=[<<'END'];
1823 usage: validation <expression>
1824
1825 description:
1826 Turn on validation during the parse process if the
1827 <expression> is non-zero or off otherwise. In XSH version 1.6
1828 and later, defaults to off.
1829  
1830 This command is equivalent to setting the `$VALIDATION'
1831 variable.
1832  
1833 END
1834  
1835  
1836 4 $HELP{'recovering'}=[<<'END'];
1837 usage: recovering <expression>
1838
1839 description:
1840 Turn on recovering parser mode if the <expression> is non-zero
1841 or off otherwise. Defaults to off. Note, that the in the
1842 recovering mode, validation is not performed by the parser
1843 even if the validation flag is on and that recovering mode
1844 flag only influences parsing of XML documents (not HTML).
1845  
1846 The recover mode helps to efficiently recover documents that
1847 are almost well-formed. This for example includes documents
1848 without a close tag for the document element (or any other
1849 element inside the document).
1850  
1851 This command is equivalent to setting the `$RECOVERING'
1852 variable.
1853  
1854 END
1855  
1856  
1857 4 $HELP{'parser-expands-entities'}=[<<'END'];
1858 usage: parser_expands_entities <expression>
1859
1860 aliases: parser_expands_entities
1861  
1862 description:
1863 Enable the entity expansion during the parse process if the
1864 <expression> is non-zero, disable it otherwise. If entity
1865 expansion is off, any external parsed entities in the document
1866 are left as entities. Defaults to on.
1867  
1868 This command is equivalent to setting the
1869 `$PARSER_EXPANDS_ENTITIES' variable.
1870  
1871 END
1872  
1873 4 $HELP{'parser_expands_entities'}=$HELP{'parser-expands-entities'};
1874  
1875 4 $HELP{'keep-blanks'}=[<<'END'];
1876 usage: keep_blanks <expression>
1877
1878 aliases: keep_blanks
1879  
1880 description:
1881 Allows you to turn off XML::LibXML's default behaviour of
1882 maintaining whitespace in the document. Non-zero expression
1883 forces the XML parser to preserve all whitespace.
1884  
1885 This command is equivalent to setting the `$KEEP_BLANKS'
1886 variable.
1887  
1888 END
1889  
1890 4 $HELP{'keep_blanks'}=$HELP{'keep-blanks'};
1891  
1892 4 $HELP{'pedantic-parser'}=[<<'END'];
1893 usage: pedantic_parser <expression>
1894
1895 aliases: pedantic_parser
1896  
1897 description:
1898 If you wish, you can make XML::LibXML more pedantic by passing
1899 a non-zero <expression> to this command.
1900  
1901 This command is equivalent to setting the `$PEDANTIC_PARSER'
1902 variable.
1903  
1904 END
1905  
1906 4 $HELP{'pedantic_parser'}=$HELP{'pedantic-parser'};
1907  
1908 4 $HELP{'parser-completes-attributes'}=[<<'END'];
1909 usage: parser-completes-attributes <expression>
1910
1911 aliases: complete_attributes complete-attributes parser_completes_attributes
1912  
1913 description:
1914 If the expression is non-zero, this command allows XML parser
1915 to complete the elements attributes lists with the ones
1916 defaulted from the DTDs. By default, this option is enabled.
1917  
1918 This command is equivalent to setting the
1919 `$PARSER_COMPLETES_ATTRIBUTES' variable.
1920  
1921 END
1922  
1923 4 $HELP{'complete_attributes'}=$HELP{'parser-completes-attributes'};
1924 4 $HELP{'complete-attributes'}=$HELP{'parser-completes-attributes'};
1925 4 $HELP{'parser_completes_attributes'}=$HELP{'parser-completes-attributes'};
1926  
1927 4 $HELP{'indent'}=[<<'END'];
1928 usage: indent <expression>
1929
1930 description:
1931 If the value of <expression> is 1, format the XML output while
1932 saving a document by adding some nice ignorable whitespace. If
1933 the value is 2 (or higher), XSH will act as in case of 1, plus
1934 it will add a leading and a trailing linebreak to each text
1935 node.
1936  
1937 Note, that since the underlying C library (libxml2) uses a
1938 hardcoded indentation of 2 space characters per indentation
1939 level, the amount of whitespace used for indentation can not
1940 be altered on runtime.
1941  
1942 This command is equivalent to setting the `$INDENT' variable.
1943  
1944 END
1945  
1946  
1947 4 $HELP{'empty-tags'}=[<<'END'];
1948 usage: empty-tags <expression>
1949
1950 aliases: empty_tags
1951  
1952 description:
1953 If the value of <expression> is 1 (non-zero), empty tags are
1954 serialized as a start-tag/end-tag pair (`<foo></foo>'). This
1955 option affects both <ls> and <save> and possibly other
1956 commands. Otherwise, they are compacted into a short-tag form
1957 (`<foo/>'). Default value is `0'.
1958  
1959 This command is equivalent to setting the `$EMPTY_TAGS'
1960 variable.
1961  
1962 END
1963  
1964 4 $HELP{'empty_tags'}=$HELP{'empty-tags'};
1965  
1966 4 $HELP{'skip-dtd'}=[<<'END'];
1967 usage: skip-dtd <expression>
1968
1969 aliases: skip_dtd
1970  
1971 description:
1972 If the value of <expression> is 1 (non-zero), DTD DOCTYPE
1973 declaration is omitted from any serialization of XML documents
1974 (including <ls> and <save>). Default value is `0'.
1975  
1976 This command is equivalent to setting the `$SKIP_DTD'
1977 variable.
1978  
1979 END
1980  
1981 4 $HELP{'skip_dtd'}=$HELP{'skip-dtd'};
1982  
1983 4 $HELP{'parser-expands-xinclude'}=[<<'END'];
1984 usage: parser_expands_xinclude <expression>
1985
1986 aliases: parser_expands_xinclude
1987  
1988 description:
1989 If the <expression> is non-zero, the parser is allowed to
1990 expand XIinclude tags imidiatly while parsing the document.
1991  
1992 This command is equivalent to setting the
1993 `$PARSER_EXPANDS_XINCLUDE' variable.
1994  
1995 see also: process-xinclude
1996  
1997 END
1998  
1999 4 $HELP{'parser_expands_xinclude'}=$HELP{'parser-expands-xinclude'};
2000  
2001 4 $HELP{'load-ext-dtd'}=[<<'END'];
2002 usage: load_ext_dtd <expression>
2003
2004 aliases: load_ext_dtd
2005  
2006 description:
2007 If the expression is non-zero, XML parser loads external DTD
2008 subsets while parsing. By default, this option is enabled.
2009  
2010 This command is equivalent to setting the `$LOAD_EXT_DTD'
2011 variable.
2012  
2013 END
2014  
2015 4 $HELP{'load_ext_dtd'}=$HELP{'load-ext-dtd'};
2016  
2017 4 $HELP{'encoding'}=[<<'END'];
2018 usage: encoding <enc-string>
2019
2020 description:
2021 Set the default output character encoding.
2022  
2023 This command is equivalent to setting the `$ENCODING'
2024 variable.
2025  
2026 END
2027  
2028  
2029 4 $HELP{'query-encoding'}=[<<'END'];
2030 usage: query-encoding <enc-string>
2031
2032 aliases: query_encoding
2033  
2034 description:
2035 Set the default query character encoding.
2036  
2037 This command is equivalent to setting the `$QUERY_ENCODING'
2038 variable.
2039  
2040 END
2041  
2042 4 $HELP{'query_encoding'}=$HELP{'query-encoding'};
2043  
2044 4 $HELP{'quiet'}=[<<'END'];
2045 usage: quiet
2046
2047 description:
2048 Turn off verbose messages.
2049  
2050 This command is equivalent to setting the `$QUIET' variable.
2051  
2052 see also: verbose
2053  
2054 END
2055  
2056  
2057 4 $HELP{'switch-to-new-documents'}=[<<'END'];
2058 usage: switch-to-new-documents <expression>
2059
2060 aliases: switch_to_new_documents
2061  
2062 description:
2063 If non-zero, XSH changes current node to the document node of
2064 a newly open/created files every time a new document is opened
2065 or created with <open> or <create>. Default value for this
2066 option is 1.
2067  
2068 This command is equivalent to setting the
2069 `$SWITCH_TO_NEW_DOCUMENTS' variable.
2070  
2071 END
2072  
2073 4 $HELP{'switch_to_new_documents'}=$HELP{'switch-to-new-documents'};
2074  
2075 4 $HELP{'backups'}=[<<'END'];
2076 usage: backups
2077
2078 description:
2079 Enable creating backup files on save (default).
2080  
2081 This command is equivalent to setting the `$BACKUPS' variable
2082 to 1.
2083  
2084 see also: nobackups
2085  
2086 END
2087  
2088  
2089 4 $HELP{'nobackups'}=[<<'END'];
2090 usage: nobackups
2091
2092 description:
2093 Disable creating backup files on save.
2094  
2095 This command is equivalent to setting the `$BACKUPS' variable
2096 to 0.
2097  
2098 see also: nobackups
2099  
2100 END
2101  
2102  
2103 4 $HELP{'fold'}=[<<'END'];
2104 usage: fold <xpath> [<expression>]
2105
2106 description:
2107 This feature is still EXPERIMENTAL! Fold command may be used
2108 to mark elements matching the <xpath> with a `xsh:fold'
2109 attribute from the `http://xsh.sourceforge.net/xsh/'
2110 namespace. When listing the DOM tree using `<ls> <xpath>
2111 fold', elements marked in this way are folded to the depth
2112 given by the <expression> (default depth is 0 = fold
2113 immediately).
2114  
2115 Example:
2116 xsh> fold //chapter 1
2117 xsh> ls //chapter[1] fold
2118 <chapter id="intro" xsh:fold="1">
2119 <title>...</title>
2120 <para>...</para>
2121 <para>...</para>
2122 </chapter>
2123  
2124 see also: unfold ls
2125  
2126 END
2127  
2128  
2129 4 $HELP{'unfold'}=[<<'END'];
2130 usage: unfold <xpath>
2131
2132 description:
2133 This feature is still EXPERIMENTAL! Unfold command removes
2134 `xsh:fold' attributes from all elements matching a given
2135 <xpath> created by previous usage of <fold>. Be aware, that
2136 `xmlns:xsh' namespace declaration may still be present in the
2137 document even when all elements are unfolded.
2138  
2139 see also: fold ls
2140  
2141 END
2142  
2143  
2144 4 $HELP{'redo'}=[<<'END'];
2145 usage: redo [<expression>]
2146
2147 description:
2148 The redo command restarts a loop block without evaluating the
2149 conditional again. The optional <expression> argument may
2150 evaluate to a positive integer number that indicates which
2151 level of the nested loops should be restarted. If omitted, it
2152 defaults to 1, i.e. the innermost loop.
2153  
2154 Using this command outside a loop causes an immediate run-time
2155 error.
2156  
2157 Example: Restart a higher level loop from an inner one
2158  
2159 while ($i<100) {
2160 # ...
2161 foreach //para {
2162 # some code
2163 if $param {
2164 redo; # redo foreach loop
2165 } else {
2166 redo 2; # redo while loop
2167 }
2168 }
2169 }
2170  
2171 see also: foreach while iterate next last
2172  
2173 END
2174  
2175  
2176 4 $HELP{'next'}=[<<'END'];
2177 usage: next [<expression>]
2178
2179 description:
2180 The next command is like the continue statement in C; it
2181 starts the next iteration of an enclosing loop. The optional
2182 <expression> argument may evaluate to a positive integer
2183 number that indicates which level of the nested loops should
2184 be restarted. If omitted, it defaults to 1, i.e. the innermost
2185 loop.
2186  
2187 Using this command outside a loop causes an immediate run-time
2188 error.
2189  
2190 see also: foreach while iterate redo last prev
2191  
2192 END
2193  
2194  
2195 4 $HELP{'prev'}=[<<'END'];
2196 usage: prev [<expression>]
2197
2198 description:
2199 This command is only allowed inside an `iterate' loop. It
2200 returns the iteration one step back, to the previous node on
2201 the iterated axis. The optional <expression> argument may be
2202 used to indicate to which level of nested loops the command
2203 applies to.
2204  
2205 see also: iterate redo last next
2206  
2207 END
2208  
2209  
2210 4 $HELP{'last'}=[<<'END'];
2211 usage: last [<expression>]
2212
2213 description:
2214 The last command is like the break statement in C (as used in
2215 loops); it immediately exits an enclosing loop. The optional
2216 <expression> argument may evaluate to a positive integer
2217 number that indicates which level of the nested loops to quit.
2218 If this argument is omitted, it defaults to 1, i.e. the
2219 innermost loop.
2220  
2221 Using this command outside a subroutine causes an immediate
2222 run-time error.
2223  
2224 see also: foreach while iterate next last
2225  
2226 END
2227  
2228  
2229 4 $HELP{'return'}=[<<'END'];
2230 usage: return
2231
2232 description:
2233 This command immediatelly stops the execution of a procedure
2234 it occurs in and returns the execution to the place of the
2235 script from which the subroutine was called.
2236  
2237 Using this command outside a subroutine causes an immediate
2238 run-time error.
2239  
2240 see also: def call
2241  
2242 END
2243  
2244  
2245 4 $HELP{'throw'}=[<<'END'];
2246 usage: throw <expression>
2247
2248 description:
2249 This command throws and exception containing error message
2250 given by the obligatory <expression> argument. If the
2251 exception is not handled by some surrounding <try> block, the
2252 execution is stopped immediatelly and the error message is
2253 printed.
2254  
2255 Note: There is a special class of internal exceptions with
2256 error message starting with a word 'UNCATCHABLE'. Such
2257 exceptions are not trapped by <try> constructions and should
2258 be avoided in ordinary XSH scripts.
2259  
2260 see also: try
2261  
2262 END
2263  
2264  
2265 4 $HELP{'catalog'}=[<<'END'];
2266 usage: catalog <expression>
2267
2268 description:
2269 Will use a given catalog file as a catalog during all parsing
2270 processes. Using a catalog will significantly speed up parsing
2271 processes if many external ressources are loaded into the
2272 parsed documents (such as DTDs or XIncludes)
2273  
2274 END
2275  
2276  
2277 4 $HELP{'iterate'}=[<<'END'];
2278 usage: iterate <xpath> <command-block>
2279
2280 description:
2281 Iterate works very much like the XPath variant of <foreach>,
2282 except that `iterate' evaluates the <command-block> as soon as
2283 a new node matching a given <xpath> is found. As a limitation,
2284 the <xpath> expresion used with `iterate' may only consist of
2285 one XPath step, i.e. it cannot contain an XPath step separator
2286 `/'.
2287  
2288 What are the benefits of `iterate' over a <foreach> loop,
2289 then? Well, under some circumstances it is efficiency, under
2290 other there are none. To clarify this, we have to dive a bit
2291 deeper into the details of XPath implementation. By
2292 definition, the node-list resulting from evaluation of an
2293 XPath has to be ordered in the canonical document order. That
2294 means that an XPath implementation must contain some kind of a
2295 sorting algorithm. This would not itself be much trouble if a
2296 relative document order of two nodes of a DOM tree could be
2297 determined in a constant time. Unfortunately, the libxml2
2298 library, used behind XSH, does not implement mechanisms that
2299 would allow this complexity restriction (which is, however,
2300 quite natural and reasonable approach if all the consequences
2301 are considered). Thus, when comparing two nodes, libxml2
2302 traverses the tree to find their nearest common ancestor and
2303 at that point determines the relative order of the two
2304 subtrees by trying to seek one of them in a list of right
2305 siblings of the other. This of course cannot be handled in a
2306 constant time. As a result, the sorting algorithm, reasonably
2307 efficient for a constant time comparison (polynomial of a
2308 degree < 1.5) or small node-lists, becomes rather unusable for
2309 huge node-lists with linear time comparison (still polynomial
2310 but of a degree > 2).
2311  
2312 The `iterate' command provides a way to avoid sorting the
2313 resulting nodelist by limiting allowed XPath expression to one
2314 step (and thus one axis) at a time. On the other hand, since
2315 `iterate' is implemented in Perl, a proxy object glueing the C
2316 and Perl layers has to be created for every node the iterator
2317 passes by. This (plus some extra subroutine calls) makes it
2318 about two to three times slower compared to a similar
2319 tree-traversing algorithm used by libxml2 itself during XPath
2320 evaluation.
2321  
2322 Our experience shows that `iterate' beats <foreach> in
2323 performance on large node-lists (>=1500 nodes, but your milage
2324 may vary) while <foreach> wins on smaller node-lists.
2325  
2326 The following two examples give equivallent results. However,
2327 the one using iterate may be faster esp. if the number of
2328 nodes being counted is very large.
2329  
2330 Example: Count inhabitants of the kingdom of Rohan in productive age
2331  
2332 cd rohan/inhabitants;
2333 iterate child::*[@age>=18 and @age<60] { perl $productive++ };
2334 echo "$productive inhabitants in productive age";
2335  
2336 Example: Using XPath
2337  
2338 $productive=count(rohan/inhabitants/*[@age>=18 and @age<60]);
2339 echo "$productive inhabitants in productive age";
2340  
2341 Use e.g. `| time cut' pipe-line redirection to benchmark a XSH
2342 command on a UNIX system.
2343  
2344 see also: foreach next prev last
2345  
2346 END
2347  
2348  
2349 4 $HELP{'register-namespace'}=[<<'END'];
2350 usage: register-namespace <expression> <expression>
2351
2352 aliases: regns
2353  
2354 description:
2355 Registers the first argument as a prefix for the namespace
2356 given in the second argument. The prefix can later be used in
2357 XPath expressions.
2358  
2359 END
2360  
2361 4 $HELP{'regns'}=$HELP{'register-namespace'};
2362  
2363 4 $HELP{'unregister-namespace'}=[<<'END'];
2364 usage: unregister-namespace <expression>
2365
2366 aliases: unregns
2367  
2368 description:
2369 Unregisters given namespace prefix previously registered using
2370 <register-namespace>. The prefix can no longer be used in
2371 XPath expressions unless declared within the current scope of
2372 the queried document.
2373  
2374 END
2375  
2376 4 $HELP{'unregns'}=$HELP{'unregister-namespace'};
2377  
2378 4 $HELP{'register-xhtml-namespace'}=[<<'END'];
2379 usage: register-xhtml-namespace <expression>
2380
2381 aliases: regns-xhtml
2382  
2383 description:
2384 Registers a prefix for the XHTML namespace. The prefix can
2385 later be used in XPath expressions.
2386  
2387 END
2388  
2389 4 $HELP{'regns-xhtml'}=$HELP{'register-xhtml-namespace'};
2390  
2391 4 $HELP{'register-xsh-namespace'}=[<<'END'];
2392 usage: register-xsh-namespace <expression>
2393
2394 aliases: regns-xsh
2395  
2396 description:
2397 Registers a new prefix for the XSH namespace. The prefix can
2398 later be used in XPath expressions. Note, that XSH namespace
2399 is by default registered with `xsh' prefix. This command is
2400 thus, in general, useful only when some document uses `xsh'
2401 prefix for a different namespace.
2402  
2403 END
2404  
2405 4 $HELP{'regns-xsh'}=$HELP{'register-xsh-namespace'};
2406  
2407 4 $HELP{'register-function'}=[<<'END'];
2408 usage: register-function <expression> <perl-code>
2409
2410 aliases: regfunc
2411  
2412 description:
2413 EXPERIMENTAL! Register given perl code as a new XPath
2414 extension function under a name provided in the first argument
2415 (<expression>). XML::LibXML DOM API may be used in the perl
2416 code for object processing. If the name contains a colon, then
2417 the first part before the colon must be a registered namespace
2418 prefix (see <register-namespace>) and the function is
2419 registered within the corresponding namespace.
2420  
2421 END
2422  
2423 4 $HELP{'regfunc'}=$HELP{'register-function'};
2424  
2425 4 $HELP{'unregister-function'}=[<<'END'];
2426 usage: unregister-function <expression>
2427
2428 aliases: unregfunc
2429  
2430 description:
2431 EXPERIMENTAL! Unregiseter XPath extension function of a given
2432 name previously registered using <register-function>.
2433  
2434 END
2435  
2436 4 $HELP{'unregfunc'}=$HELP{'unregister-function'};
2437  
2438 4 $HELP{'stream'}=[<<'END'];
2439 usage: stream input [FILE|PIPE|STRING] <expression>
2440 output [FILE|PIPE|STRING] <expression>
2441 select <xpath> <command-block>
2442 [ select <xpath> <command-block> ... ]
2443
2444 description:
2445 EXPERIMENTAL! This command provides a memory efficient (though
2446 slower) way to process selected parts of an XML document with
2447 XSH. A streaming XML parser (SAX parser) is used to parse the
2448 input. The parser has two states which will be refered to as A
2449 and B below. The initial state of the parser is A.
2450  
2451 In the state A, only a limited vertical portion of the DOM
2452 tree is built. All XML data comming from the input stream
2453 other than start-tags are immediatelly copied to the output
2454 stream. If a new start-tag of an element arrives, a new node
2455 is created in the tree. All siblings of the newly created node
2456 are removed. Thus, in the state A, there is exactly one node
2457 on every level of the tree. After a node is added to the tree,
2458 all the <xpath> expressions following the `select' keyword are
2459 checked. If none matches, the parser remains in state A and
2460 copies the start-tag to the output stream. Otherwise, the
2461 first expression that matches is remembered and the parser
2462 changes its state to B.
2463  
2464 In state B the parser builds a complete DOM subtree of the
2465 element that was last added to the tree before the parser
2466 changed its state from A to B. No data are sent to the output
2467 at this stage. When the subtree is complete (i.e. the
2468 corresponding end-tag for its topmost element is encountered),
2469 the <command-block> of instructions following the <xpath>
2470 expression that matched is invoked with the root element of
2471 the subtree as the current context node. The commands in
2472 <command-block> are allowed to transform the whole element
2473 subtree or even to replace it with a different DOM subtree or
2474 subtrees. They must, however, preserve the element's parent as
2475 well as all its ancestor nodes intact. Failing to do so can
2476 result in an error or unpredictable results.
2477  
2478 After the subtree processing <command-block> returns, all
2479 subtrees that now appear in the DOM tree in the place of the
2480 original subtree are serialized to the output stream. After
2481 that, they are deleted and the parser returns to state A.
2482  
2483 Note that this type of processing highly limits the amount of
2484 information the XPath expressions can use. First notable fact
2485 is that elements can not be selected by their content. The
2486 only information present in the tree at the time of the XPath
2487 evaluation is the element's name and attributes plus the same
2488 information for all its ancestors. There is nothing known yet
2489 about possible child nodes of the element as well as of the
2490 node's position within its siblings.
2491  
2492 END
2493  
2494  
2495 4 $HELP{'namespaces'}=[<<'END'];
2496 usage: namespaces [<xpath>]
2497
2498 description:
2499 For each node matching given <xpath> lists all namespaces that
2500 are valid in its scope in the form of `xmlns:prefix="uri"'
2501 declarations. If no <xpath> is given, lists namespaces in the
2502 scope of the current node.
2503  
2504 END
2505  
2506  
2507 4 $HELP{'xpath-completion'}=[<<'END'];
2508 usage: xpath_completion <expression>
2509
2510 aliases: xpath_completion
2511  
2512 description:
2513 If the <expression> is non-zero, enable the TAB completion for
2514 <xpath> expansions in the interactive shell mode, disable it
2515 otherwise. Defaults to on.
2516  
2517 This command is equivalent to setting the `$XPATH_COMPLETION'
2518 variable.
2519  
2520 END
2521  
2522 4 $HELP{'xpath_completion'}=$HELP{'xpath-completion'};
2523  
2524 4 $HELP{'xpath-axis-completion'}=[<<'END'];
2525 usage: xpath-axis-completion <expression>
2526
2527 aliases: xpath_axis_completion
2528  
2529 description:
2530 The following values are allowed: `always', `never',
2531 `when-empty'. Note, that all other values (including 1) work
2532 as `never'!
2533  
2534 If the <expression> evaluates to `always', TAB completion for
2535 XPath expressions always includes axis names.
2536  
2537 If the <expression> evaluates to `when-empty', the TAB
2538 completion list for XPath expressions includes axis names only
2539 if no element name matches the completion.
2540  
2541 If the <expression> evaluates to `never', the TAB completion
2542 list for XPath expressions never includes axis names.
2543  
2544 The default value for this optio is `always'.
2545  
2546 This command is equivalent to setting the
2547 `$XPATH_AXIS_COMPLETION' variable.
2548  
2549 END
2550  
2551 4 $HELP{'xpath_axis_completion'}=$HELP{'xpath-axis-completion'};
2552  
2553 4 $HELP{'doc-info'}=[<<'END'];
2554 usage: doc-info [<expression>]
2555
2556 aliases: doc_info
2557  
2558 description:
2559 In the present implementation, this command displays
2560 information provided in the `<?xml ...?>' declaration of a
2561 document: `version', `encoding', `standalone', plus
2562 information about level of `gzip' compression of the original
2563 XML file.
2564  
2565 see also: set-enc set-standalone
2566  
2567 END
2568  
2569 4 $HELP{'doc_info'}=$HELP{'doc-info'};
2570  
2571 4 $HELP{'Documents'}=[<<'END'];
2572 Files/Documents
2573 ---------------
2574  
2575 XSH is intended to query and manipulate XML and HTML documents. Use one
2576 of the `open/open-*/create' commands to load an XML or HTML document from
2577 a local file, external URL (such as http:// or ftp://), string or pipe.
2578 While loading, XSH parses and optionally validates (see <validation> and
2579 <load-ext-dtd>) the document. Parsed documents are stored in memory as
2580 DOM trees, that can be <navigated>navigated and <manipulated>manipulated
2581 quite similarly to a local filesystem.
2582  
2583 Every opened document is associated with an identifier (<id>), that is a
2584 symbolic name for the document in XSH and can be used for example as a
2585 prefix of <xpath>.
2586  
2587 In the current version, XSH is only able to save documents locally. To
2588 store a document on any other location, use <ls> command and pipe
2589 redirection to feed the XML representation of the document to any
2590 external program that is able to store it on a remote location.
2591  
2592 Example: Store XSH document DOC on a remote machine using Secure Shell
2593  
2594 xsh> ls DOC:/ | ssh my.remote.org 'cat > test.xml'
2595  
2596  
2597 Related commands:
2598 backups, catalog, clone, close, create, documents, nobackups, open,
2599 process-xinclude, save, select, stream, switch-to-new-documents
2600  
2601 END
2602  
2603 4 $HELP{'Navigation'}=[<<'END'];
2604 Tree navigation
2605 ---------------
2606  
2607 With XSH, it is possible to browse <document trees>document trees as if
2608 they were a local filesystem, except that <XPath>XPath expressions are
2609 used instead of ordinary UNIX paths.
2610  
2611 Current position in the document tree is called the current node. Current
2612 node's XPath may be queried with <pwd> command. In the interactive shell,
2613 current node is also displayed in the command line prompt. Remember, that
2614 beside <cd> command, current node (and document) is silently changed by
2615 all variant of <open> command, <create> command and temporarily also by
2616 the node-list variant of the <foreach> statement.
2617  
2618 Documents are specified in a similar way as harddrives on DOS/Windows(TM)
2619 systems (except that their names are not limitted to one letter in XSH),
2620 i.e. by a prefix of the form doc: where doc is the <id> associated with
2621 the document.
2622  
2623 To mimic the filesystem navigation as closely as possible, XSH contains
2624 several commands named by analogy of UNIX filesystem commands, such as
2625 <cd>, <ls> and <pwd>.
2626  
2627 Example:
2628 xsh scratch:/> open docA="testA.xml"
2629 xsh docB:/> open docB="testB.xml"
2630 xsh> pwd
2631 docB:/
2632 xsh docB:/> cd docA:/article/chapter[title='Conclusion']
2633 xsh docA:/article/chapter[5]> pwd
2634 docA:/article/chapter[5]
2635 xsh docA:/article/chapter[5]> cd previous-sibling::chapter
2636 xsh docA:/article/chapter[4]> cd ..
2637 xsh docA:/article> select docB
2638 xsh docB:/>
2639  
2640  
2641 Related commands:
2642 cd, fold, locate, ls, pwd, register-function, register-namespace,
2643 register-xhtml-namespace, register-xsh-namespace, select, unfold,
2644 unregister-function, unregister-namespace
2645  
2646 END
2647  
2648 4 $HELP{'Manipulation'}=[<<'END'];
2649 Tree modification
2650 -----------------
2651  
2652 XSH provides mechanisms not only to browse and inspect the DOM tree but
2653 also to modify its content by providing commands for copying, moving, and
2654 deleting its nodes as well as adding completely new nodes or XML
2655 fragments to it. It is quite easy to learn these commands since their
2656 names or aliases mimic their well-known filesystem analogies. On the
2657 other hand, many of these commands have two versions one of which is
2658 prefixed with a letter "x". This "x" stands for "cross", thus e.g.
2659 <xcopy> should be read as "cross copy". Let's explain the difference on
2660 the example of <xcopy>.
2661  
2662 When you copy, you have to specify what are you copying and where are you
2663 copying to, so you have to specify the source and the target. XSH is very
2664 much XPath-based so, XPath is used here to specify both of them. However,
2665 there might be more than one node that satisfies an XPath expression. So,
2666 the rule of thumb is that the "cross" variant of a command places one and
2667 every of the source nodes to the location of one and every destination
2668 node, while the plain variant works one-by-one, placing the first source
2669 node to the first destination, the second source node to the second
2670 destination, and so on (as long as there are both source nodes and
2671 destinations left).
2672  
2673 Example:
2674 xsh> create a "<X><A/><Y/><A/></X>";
2675 xsh> create b "<X><B/><C/><B/><C/><B/></X>";
2676 xsh> xcopy a://A replace b://B;
2677 xsh> copy b://C before a://A;
2678 xsh> ls a:/;
2679 <?xml version="1.0" encoding="utf-8"?>
2680 <X><C/><A/><Y/><C/><A/></X>
2681
2682 xsh> ls b:/;
2683 <?xml version="1.0" encoding="utf-8"?>
2684 <X><A/><A/><C/><A/><A/><C/><A/><A/></X>
2685  
2686 As already indicated by the example, another issue of tree modification
2687 is the way in which the destination node determines the target location.
2688 Should the source node be placed before, after, or into the resulting
2689 node? Should it replace it completely? This information has to be given
2690 in the <location> argument that usually precedes the destination XPath.
2691  
2692 Now, what happens if source and destination nodes are of incompatible
2693 types? XSH tries to avoid this by implicitly converting between node
2694 types when necessary. For example, if a text, comment, and attribute node
2695 is copied into, before or after an attribute node, the original value of
2696 the attribute is replaced, prepended or appended respectively with the
2697 textual content of the source node. Note however, that element nodes are
2698 never converted into text, attribute or any other textual node. There are
2699 many combinations here, so try yourself and see the results.
2700  
2701 You may even use some more sofisticated way to convert between node
2702 types, as shown in the following example, where an element is first
2703 commented out and than again uncommented. Note, that the particular
2704 approach used for resurrecting the commented XML material works only for
2705 well-balanced chunks of XML.
2706  
2707 Example: Using string variables to convert between different types of nodes
2708  
2709 create doc <<EOF;
2710 <?xml version='1.0'?>
2711 <book>
2712 <chapter>
2713 <title>Intro</title>
2714 </chapter>
2715 <chapter>
2716 <title>Rest</title>
2717 </chapter>
2718 </book>
2719 EOF
2720
2721 # comment out the first chapter
2722 ls //chapter[1] |> $chapter_xml;
2723 add comment $chapter_xml replace //chapter[1];
2724 ls / 0;
2725 # OUTPUT:
2726 <?xml version="1.0"?>
2727 <book>
2728 <!-- <chapter>
2729 <title>Intro</title>
2730 </chapter>
2731 -->
2732 <chapter>
2733 <title>Rest</title>
2734 </chapter>
2735 </book>
2736
2737
2738 # un-comment the chapter
2739 $comment = string(//comment()[1]);
2740 add chunk $comment replace //comment()[1];
2741 ls / 0;
2742 # OUTPUT:
2743 <?xml version="1.0"?>
2744 <book>
2745 <chapter>
2746 <title>Intro</title>
2747 </chapter>
2748
2749 <chapter>
2750 <title>Rest</title>
2751 </chapter>
2752 </book>
2753  
2754  
2755 Related commands:
2756 clone, copy, insert, map, move, normalize, process-xinclude, remove,
2757 rename, set-enc, set-standalone, strip-whitespace, xcopy, xinsert, xmove,
2758 xslt, xupdate
2759  
2760 END
2761  
2762 4 $HELP{'Flow'}=[<<'END'];
2763 Flow control
2764 ------------
2765  
2766 What a scripting language XSH would be had it not some kind of
2767 conditional statements, loops and other stuff that influences the way in
2768 which XSH commands are processed.
2769  
2770 Most notable XSH's feature in this area is that some of the basic flow
2771 control statements, namely <if>, <unless>, <while> and <foreach> have two
2772 variants, an XPath-based one and a Perl-based one. The XPath-based
2773 variant uses <xpath> expressions to specify the condition or node-lists
2774 to iterate, while the other one utilizes <perl-code> for this purpose.
2775 See descriptions of the individual statements for more detail.
2776  
2777  
2778 Related commands:
2779 call, def, exit, foreach, if, ifinclude, include, iterate, last, next,
2780 prev, redo, return, run-mode, stream, test-mode, throw, try, undef,
2781 unless, while
2782  
2783 END
2784  
2785 4 $HELP{'Information'}=[<<'END'];
2786 Retrieving more information
2787 ---------------------------
2788  
2789 Beside the possibility to browse the DOM tree and list some parts of it
2790 (as described in <Navigation>), XSH provides commands to obtain other
2791 information related to open documents as well as the XSH interpreter
2792 itself. These commands are listed bellow.
2793  
2794  
2795 Related commands:
2796 count, defs, doc-info, documents, dtd, enc, help, locate, ls, namespaces,
2797 options, print, pwd, valid, validate, variables, version
2798  
2799 END
2800  
2801 4 $HELP{'Argtypes'}=[<<'END'];
2802 Argument Types
2803 --------------
2804  
2805 XSH commands accept different types of arguments, such as usual strings
2806 (<expression>) or <XPath expressions>XPath expressions. Notably, these
2807 two types and types based on them support string variable interpolation.
2808 See documentation of the individual types for more information.
2809  
2810 END
2811  
2812 4 $HELP{'Variables'}=[<<'END'];
2813 Variables
2814 ---------
2815  
2816 In the current version, XSH supports two types of variables: string
2817 (scalar) variables and node-list variables. Perl programmers that might
2818 miss some other kinds of variables (arrays or hashes) may use the support
2819 for <interacting with Perl>interacting with Perl to access these types
2820 (see some examples below).
2821  
2822 These two kinds of variables differ syntactically in the prefix: string
2823 variables are prefixed with a dollar sign (`$') while node-list variables
2824 are prefixed with a percent sign (`%').
2825  
2826 String Variables
2827 ----------------
2828  
2829 Every string variable name consists of a dollar sign (`$') prefix and
2830 an <id>, that has to be unique among other scalar variables, e.g.
2831 `$variable'. Values are assigned to variables either by simple
2832 <assignments>assignments of the form `$variable = <xpath>' or by
2833 capturing the output of some command with a variable redirection of the
2834 form `command |> $variable'.
2835  
2836 String variables may be used in <string expressions>string expressions,
2837 <XPath expressions>XPath expressions, or even in perl-code as $<id> or
2838 ${<id>}. In the first two cases, variables act as macros in the sense
2839 that all variables occurences are replaced by the corresponding values
2840 before the expression itself is evaluated.
2841  
2842 To display current value of a variable, use the <print> command,
2843 <variables> command or simply the variable name:
2844  
2845 Example:
2846 xsh> $b="chapter";
2847 xsh> $file="${b}s.xml";
2848 xsh> open f=$file;
2849 xsh> ls //$b[count(descendant::para)>10]
2850 xsh> print $b
2851 chapter
2852 xsh> $b
2853 $b='chapter';
2854 xsh> variables
2855 $a='chapters.xml';
2856 $b='chapter';
2857  
2858 Node-list Variables
2859 -------------------
2860  
2861 Every string variable name consists of a percent sign (`%') prefix and
2862 an <id>, that has to be unique among other node-list variables, e.g.
2863 `%variable'.
2864  
2865 Node-list variables can be used to store lists of nodes that result
2866 from evaluating an XPath. This is especially useful when several
2867 changes are performed on some set of nodes and evaluating the XPath
2868 expression repeatedly would take too long. Other important use is to
2869 remember a node that would otherwise be extremely hard or even
2870 impossible to locate by XPath expressions after some changes to the
2871 tree structure are made, since such an XPath cannot be predicted in
2872 advance.
2873  
2874 Although node-list variables act just like XPath expressions that would
2875 result in the same node-list, for implementation reasons it is not
2876 possible to use node-list variables as parts of complex XPath
2877 expressions except for one case. They may be only used at the very
2878 beginning of an XPath expression. So while constructions such as
2879 `%creatures[4]', `%creatures[@race='elf']', or
2880 `%creatures/parents/father' do work as expected,
2881 `string(%creatures[2]/@name)' `//creature[%creatures[2]/@name=@name]',
2882 or `%creatures[@race='elf'][2]' do not. In the first two cases it is
2883 because node-list variables cannot be evaluated in the middle of an
2884 XPath expression. The third case fails because this construction
2885 actually translates into a sequence of evaluations of
2886 `self::*[@race='elf'][2]' for each node in the `%creatures' node-list,
2887 which is not equivallent to the intended expression as the `[2]' filter
2888 does not apply to the whole result of `%creatures[@race='elf']' at once
2889 but rather to the partial results.
2890  
2891 Fortunatelly, it is usually possible to work around these unsupported
2892 constructions quite easily. This is typically done by introducing some
2893 more variables as well as using the <foreach> statement. The following
2894 example should provide some idea on how to do this:
2895  
2896 Example:
2897 # work around for $name=string(%creatures[2]/@name)
2898 xsh> foreach %creatures[2] $name=string(@name)
2899 # work around for ls //creature[%creatures[2]/@name=@name]
2900 xsh> ls //creature[$name=@name]
2901 # work around for ls %creatures[@race='elf'][2]
2902 xsh> %elves = %creatures[@race='elf']
2903 xsh> ls %elves[2]
2904  
2905 Remember, that when a node is deleted from a tree it is at the same
2906 time removed from all node-lists it occurs in. Note also, that unlike
2907 string variables, node-list variables can not be (and are not intended
2908 to be) directly accessed from Perl code.
2909  
2910 Accessing Perl Variables
2911 ------------------------
2912  
2913 All XSH string variables are usual Perl scalar variables from the
2914 `XML::XSH::Map' namespace, which is the default namespace for any Perl
2915 code evaluated from XSH. Thus it is possible to arbitrarily intermix
2916 XSH and Perl assignments:
2917  
2918 Example:
2919 xsh> ls //chapter[1]/title
2920 <title>Introduction</title>
2921 xsh> $a=string(//chapter[1]/title)
2922 xsh> eval { $b="CHAPTER 1: ".uc($a); }
2923 xsh> print $b
2924 CHAPTER 1: INTRODUCTION
2925  
2926 If needed, it is, however, possible to use any other type of Perl
2927 variables by means of evaluating a corresponding perl code. The
2928 following example demonstrates using Perl hashes to collect and print
2929 some simple racial statistics about the population of Middle-Earth:
2930  
2931 Example:
2932 foreach a:/middle-earth/creature {
2933 $race=string(@race);
2934 eval { $races{$race}++ };
2935 }
2936 print "Middle-Earth Population (race/number of creatures)"
2937 eval {
2938 echo map "$_/$races{$_}\n",
2939 sort ($a cmp $b), keys(%races);
2940 };
2941  
2942  
2943 Related commands:
2944 assign, local
2945  
2946 END
2947  
2948 4 $HELP{'Configuration'}=[<<'END'];
2949 Options
2950 -------
2951  
2952 The following commands are used to modify the default behaviour of the
2953 XML parser or XSH itself. Some of the commands are switch between two
2954 different modes according to a given expression (which is expected to
2955 result either in zero or non-zero value). Other commands also working as
2956 a flip-flop have their own explicit counterpart (e.g. <verbose> and
2957 <quiet> or <debug> and <nodebug>). This misconsistency is due to
2958 historical reasons.
2959  
2960 The <encoding> and <query-encoding> options allow to specify character
2961 encoding that should be expected from user as well as the encoding to be
2962 used by XSH on output. This is particularly useful when you work with
2963 UTF-8 encoded documents on a console which supports only 8-bit
2964 characters.
2965  
2966 The <options> command displays current settings by means of XSH commands.
2967 Thus it can not only be used to review current values, but also to store
2968 them future use, e.g. in ~/.xshrc file.
2969  
2970 Example:
2971 xsh> options | cat > ~/.xshrc
2972  
2973  
2974 Related commands:
2975 backups, debug, empty-tags, encoding, indent, keep-blanks, load-ext-dtd,
2976 nobackups, nodebug, options, parser-completes-attributes,
2977 parser-expands-entities, parser-expands-xinclude, pedantic-parser,
2978 query-encoding, quiet, recovering, register-function, register-namespace,
2979 register-xhtml-namespace, register-xsh-namespace, run-mode, skip-dtd,
2980 switch-to-new-documents, test-mode, unregister-function,
2981 unregister-namespace, validation, verbose, xpath-axis-completion,
2982 xpath-completion
2983  
2984 END
2985  
2986 4 $HELP{'Perl_shell'}=[<<'END'];
2987 Interacting with Perl and Shell
2988 -------------------------------
2989  
2990 To allow more complex tasks to be achieved, XSH provides ways for
2991 interaction with the Perl programming language and the system shell.
2992  
2993 Calling Perl
2994 ------------
2995  
2996 Perl is a language optimized for scanning arbitrary text files,
2997 extracting information from those text files, and printing reports
2998 based on that information. It's also a good language for many system
2999 management tasks. The language is intended to be practical (easy to
3000 use, efficient, and complete). XSH itself is written in Perl, so it is
3001 extremely easy to support this language as an extension to XSH.
3002  
3003 Perl <expressions or blocks of code>expressions or blocks of code can
3004 either be simply evaluated with the <perl> command, used to do quick
3005 changes to nodes of the DOM tree (see <map> command), used to provide
3006 list of strings to iterate over in a <foreach> loop, or to specify more
3007 complex conditions for <if>, <unless>, and <while> statements.
3008  
3009 To prevent conflict between XSH internals and the evaluated perl code,
3010 XSH runs such code in the context of a special namespace
3011 `XML::XSH::Map'. As described in the section <Variables>, XSH string
3012 variables may be accessed and possibly assigned from Perl code in the
3013 most obvious way, since they actually are Perl variables defined in the
3014 `XML::XSH::Map' namespace.
3015  
3016 The interaction between XSH and Perl actually works also the other way
3017 round, so that you may call back XSH from the evaluated Perl code. For
3018 this, Perl function `xsh' is defined in the `XML::XSH::Map' namespace.
3019 All parameters passed to this function are interpreted as XSH commands.
3020 To simplify evaluation of XPath expressions, another three functions:
3021 The first one, named `count', returns the same value as would be
3022 printed by <count> command in XSH on the same XPath expression. The
3023 second function, named `literal', returns the result of XPath
3024 evaluation as if the whole expression was wrapped with the XPath
3025 `string()' function. In other words, `literal('doc:expression')'
3026 returns the same value as `count('doc:string(expression)')'. The third
3027 function, named `xml_list', returns the result of the XPath search as a
3028 XML string which is equivallent to the output of a <ls> on the same
3029 XPath expression (without indentation and without folding or any other
3030 limitation on the depth of the listing).
3031  
3032 In the following examples we use Perl to populate the Middle-Earth with
3033 Hobbits whose names are read from a text file called `hobbits.txt',
3034 unless there are some Hobbits in Middle-Earth already.
3035  
3036 Example: Use Perl to read text files
3037  
3038 unless (//creature[@race='hobbit']) {
3039 perl 'open $file, "hobbits.txt"';
3040 perl '@hobbits=<$file>';
3041 perl 'close $file';
3042 foreach { @hobbits } {
3043 insert element "<creature name='$__' race='hobbit'>"
3044 into m:/middle-earth/creatures;
3045 }
3046 }
3047  
3048 Example: The same code as a single Perl block
3049  
3050 perl {
3051 unless (count(//creature[@race='hobbit'])) {
3052 open my $file, "hobbits.txt";
3053 foreach (<$file>) {
3054 xsh(qq{insert element "<creature name='$_' race='hobbit'>"
3055 into m:/middle-earth/creatures});
3056 }
3057 close $file;
3058 }
3059 };
3060  
3061 Writing your own XPath extension functions in Perl
3062 --------------------------------------------------
3063  
3064 XSH allows the user to extend the set of XPath functions by providing
3065 an extension function written in Perl. This can be achieved using the
3066 <register-function> command. The perl code implementing an extension
3067 function works as a usual perl routine accepting its arguments in `@_'
3068 and returning the result. The following conventions are used:
3069  
3070 The arguments passed to the perl implementation by the XPath engine are
3071 either simple scalars or `XML::LibXML::NodeList' objects, depending on
3072 the types of the XPath arguments. The implementation is responsible for
3073 checking the argument number and types. The implementation may use
3074 arbitrary `XML::LibXML' methods to process the arguments and return the
3075 result. (`XML::LibXML' perl module documentation can be found for
3076 example at
3077 http://search.cpan.org/author/PHISH/XML-LibXML-1.54/LibXML.pm).
3078  
3079 The implementation SHOULD NOT, however, MODIFY the document. Doing so
3080 could not only confuse the XPath engine but result in an critical error
3081 (such as segmentation fault).
3082  
3083 Calling XSH commands from extension function implementations is not
3084 currently allowed.
3085  
3086 The perl code must return a single value, which can be of one of the
3087 following types: a simple scalar (a number or string),
3088 `XML::LibXML::Boolean' object reference (result is a boolean value),
3089 `XML::LibXML::Literal' object reference (result is a string),
3090 `XML::LibXML::Number' object reference (resulat is a float),
3091 `XML::LibXML::Node' (or derived) object reference (result is a nodeset
3092 consisting of a single node), or `XML::LibXML::NodeList' (result is a
3093 nodeset). For convenience, simple (non-blessed) array references
3094 consisting of `XML::LibXML::Node' objects can also be used for a
3095 nodeset result instead of a `XML::LibXML::NodeList'.
3096  
3097 Calling the System Shell
3098 ------------------------
3099  
3100 In the interactive mode, XSH interprets all lines starting with a
3101 exclamation mark (`!') as shell commands and invokes the system shell
3102 to interpret them (this is to mimic FTP command-line interpreters).
3103  
3104 Example:
3105 xsh> !ls -l
3106 -rw-rw-r-- 1 pajas pajas 6355 Mar 14 17:08 Artistic
3107 drwxrwxr-x 2 pajas users 128 Sep 1 10:09 CVS
3108 -rw-r--r-- 1 pajas pajas 14859 Aug 26 15:19 ChangeLog
3109 -rw-r--r-- 1 pajas pajas 2220 Mar 14 17:03 INSTALL
3110 -rw-r--r-- 1 pajas pajas 18009 Jul 15 17:35 LICENSE
3111 -rw-rw-r-- 1 pajas pajas 417 May 9 15:16 MANIFEST
3112 -rw-rw-r-- 1 pajas pajas 126 May 9 15:16 MANIFEST.SKIP
3113 -rw-r--r-- 1 pajas pajas 20424 Sep 1 11:04 Makefile
3114 -rw-r--r-- 1 pajas pajas 914 Aug 26 14:32 Makefile.PL
3115 -rw-r--r-- 1 pajas pajas 1910 Mar 14 17:17 README
3116 -rw-r--r-- 1 pajas pajas 438 Aug 27 13:51 TODO
3117 drwxrwxr-x 5 pajas users 120 Jun 15 10:35 blib
3118 drwxrwxr-x 3 pajas users 1160 Sep 1 10:09 examples
3119 drwxrwxr-x 4 pajas users 96 Jun 15 10:35 lib
3120 -rw-rw-r-- 1 pajas pajas 0 Sep 1 16:23 pm_to_blib
3121 drwxrwxr-x 4 pajas users 584 Sep 1 21:18 src
3122 drwxrwxr-x 3 pajas users 136 Sep 1 10:09 t
3123 -rw-rw-r-- 1 pajas pajas 50 Jun 16 00:06 test
3124 drwxrwxr-x 3 pajas users 496 Sep 1 20:18 tools
3125 -rwxr-xr-x 1 pajas pajas 5104 Aug 30 17:08 xsh
3126  
3127 To invoke a system shell command or program from the non-interactive
3128 mode or from a complex XSH construction, use the <exec> command.
3129  
3130 Since UNIX shell commands are very powerful tool for processing textual
3131 data, XSH supports direct redirection of XSH commands output to system
3132 shell command. This is very similarly to the redirection known from
3133 UNIX shells, except that here, of course, the first command in the
3134 pipe-line colone is an XSH command. Since semicolon (`;') is used in
3135 XSH to separate commands, it has to be prefixed with a backslash if it
3136 should be used for other purposes.
3137  
3138 Example: Use grep and less to display context of `funny'
3139  
3140 xsh> ls //chapter[5]/para | grep funny | less
3141  
3142 Example: The same on Windows 2000/XP systems
3143  
3144 xsh> ls //chapter[5]/para | find "funny" | more
3145  
3146  
3147 Related commands:
3148 exec, lcd, map, perl, rename
3149  
3150 END
3151  
3152 4 $HELP{'commands'}=$HELP{'command'};
3153  
3154 4 1;
3155 __END__
« XML/XSH/CompletionList.pm ↑ Summary