Debian Codegraph @

doc/xmlstarlet-ug.xml

raw
1 <?xml version="1.0" encoding="UTF-8"?>
2 <book xmlns="http://docbook.org/ns/docbook"
3 xmlns:xlink="http://www.w3.org/1999/xlink"
4 xmlns:db="http://docbook.org/ns/docbook"
5 version="5.0"
6 xml:lang="en">
7 <title>XmlStarlet Command Line XML Toolkit User's Guide</title>
8
9 <info>
10 <date>June 17, 2012</date>
11
12 <author>
13 <personname>
14 <firstname>Mikhail</firstname>
15 <surname>Grushinskiy</surname>
16 </personname>
17 </author>
18 </info>
19
20 <chapter>
21 <title xml:id="s.1">Introduction</title>
22
23 <sect1>
24 <title xml:id="s.1.1">About XmlStarlet</title>
25
26 <para><link xlink:href="http://xmlstar.sourceforge.net/">XMLStarlet</link> is
27 a set of command line utilities (tools) which can be used to transform,
28 query, validate, and edit XML documents and files using simple set of
29 shell commands in similar way it is done for plain text files using UNIX
30 grep, sed, awk, diff, patch, join, etc commands.</para>
31
32 <para>This set of command line utilities can be used by those who deal
33 with many XML documents on UNIX shell command prompt as well as for
34 automated XML processing with shell scripts.</para>
35
36 <para>XMLStarlet command line utility is written in C and uses libxml2
37 and libxslt from <link
38 xlink:href="http://xmlsoft.org/">http://xmlsoft.org/</link>. Implementation of
39 extensive choice of options for XMLStarlet utility was only possible
40 because of rich feature set of libxml2 and libxslt (many thanks to the
41 developers of those libraries for great work).</para>
42
43 <para>'diff' and 'patch' options are not currently implemented. Other
44 features need some work too. Please, send an email to the project
45 administrator (see <link
46 xlink:href="http://sourceforge.net/projects/xmlstar/">http://sourceforge.net/projects/xmlstar/</link>)
47 if you wish to help.</para>
48
49 <para>XMLStarlet is linked statically to both libxml2 and libxslt, so
50 generally all you need to process XML documents is one executable file.
51 To run XmlStarlet utility you can simple type '<phrase role="PROG"/>' on command line and
52 see list of options available.</para>
53
54 <para>XMLStarlet is open source freeware under MIT license which allows
55 free use and distribution for both commercial and non-commercial
56 projects.</para>
57
58 <para>We welcome any user's feedback on this project which would greatly
59 help us to improve its quality. Comments, suggestions, feature requests,
60 bug reports can be done via SourceForge project web site (see <link
61 xlink:href="http://sourceforge.net/p/xmlstar/discussion/">XMLStarlet
62 Sourceforge forums</link>, or <link
63 xlink:href="http://sourceforge.net/p/xmlstar/mailman/">XMLStarlet
64 mailing list</link>)</para>
65 </sect1>
66
67 <sect1>
68 <title xml:id="s.1.2">Main Features</title>
69
70 <para>The toolkit's feature set includes options to:</para>
71
72 <itemizedlist>
73 <listitem>
74 <para>Check or validate XML files (simple well-formedness check,
75 DTD, XSD, RelaxNG)</para>
76 </listitem>
77
78 <listitem>
79 <para>Calculate values of XPath expressions on XML files (such as
80 running sums, etc)</para>
81 </listitem>
82
83 <listitem>
84 <para>Search XML files for matches to given XPath expressions</para>
85 </listitem>
86
87 <listitem>
88 <para>Apply XSLT stylesheets to XML documents (including EXSLT
89 support, and passing parameters to stylesheets)</para>
90 </listitem>
91
92 <listitem>
93 <para>Query XML documents (ex. query for value of some elements of
94 attributes, sorting, etc)</para>
95 </listitem>
96
97 <listitem>
98 <para>Modify or edit XML documents (ex. delete some elements)</para>
99 </listitem>
100
101 <listitem>
102 <para>Format or "beautify" XML documents (as changing indentation,
103 etc)</para>
104 </listitem>
105
106 <listitem>
107 <para>Fetch XML documents using http:// or ftp:// URLs</para>
108 </listitem>
109
110 <listitem>
111 <para>Browse tree structure of XML documents (in similar way to 'ls'
112 command for directories)</para>
113 </listitem>
114
115 <listitem>
116 <para>Include one XML document into another using XInclude</para>
117 </listitem>
118
119 <listitem>
120 <para>XML c14n canonicalization</para>
121 </listitem>
122
123 <listitem>
124 <para>Escape/unescape special XML characters in input text</para>
125 </listitem>
126
127 <listitem>
128 <para>Print directory as XML document</para>
129 </listitem>
130
131 <listitem>
132 <para>Convert XML into PYX format (based on ESIS - ISO 8879), and
133 vice versa</para>
134 </listitem>
135 </itemizedlist>
136
137 <para></para>
138 </sect1>
139
140 <sect1>
141 <title xml:id="s.1.3">Supported Platforms</title>
142
143 <para>Here is a list of platforms on which XmlStarlet is known to
144 work.</para>
145
146 <itemizedlist>
147 <listitem>
148 <para>Linux</para>
149 </listitem>
150 </itemizedlist>
151
152 <itemizedlist>
153 <listitem>
154 <para>Solaris</para>
155 </listitem>
156 </itemizedlist>
157
158 <itemizedlist>
159 <listitem>
160 <para>Windows</para>
161 </listitem>
162 </itemizedlist>
163
164 <itemizedlist>
165 <listitem>
166 <para>MacOS X</para>
167 </listitem>
168 </itemizedlist>
169
170 <itemizedlist>
171 <listitem>
172 <para>FreeBSD/NetBSD</para>
173 </listitem>
174 </itemizedlist>
175
176 <itemizedlist>
177 <listitem>
178 <para>HP-UX</para>
179 </listitem>
180 </itemizedlist>
181
182 <itemizedlist>
183 <listitem>
184 <para>AIX</para>
185 </listitem>
186 </itemizedlist>
187
188 <para>You might be able to compile and make it on others too.</para>
189 </sect1>
190
191 <sect1>
192 <title xml:id="s.1.4">Finding binary packages</title>
193
194 <para>Here is a list of sites where you can also find XmlStarlet binary
195 packages.</para>
196
197 <itemizedlist>
198 <listitem>
199 <para><link
200 xlink:href="http://www.suse.com/us/private/products/suse_linux/prof/packages_professional/xmlstarlet.html">SuSE
201 Packages</link></para>
202 </listitem>
203 </itemizedlist>
204
205 <itemizedlist>
206 <listitem>
207 <para><link
208 xlink:href="http://linux01.gwdg.de/~pbleser/rpm-navigation.php?cat=%2FUtilities%2Fxmlstarlet/">SuSE
209 Guru's RPM Site</link></para>
210 </listitem>
211 </itemizedlist>
212
213 <itemizedlist>
214 <listitem>
215 <para><link
216 xlink:href="http://www.freebsd.org/cgi/ports.cgi?query=xmlstarlet&amp;stype=all">FreeBSD
217 Ports</link></para>
218 </listitem>
219 </itemizedlist>
220
221 <itemizedlist>
222 <listitem>
223 <para><link
224 xlink:href="http://www.freshports.org/textproc/xmlstarlet/">FreeBSD Fresh
225 Ports</link></para>
226 </listitem>
227 </itemizedlist>
228
229 <itemizedlist>
230 <listitem>
231 <para><link
232 xlink:href="http://fink.sourceforge.net/pdb/package.php/xmlstarlet">Mac OS
233 Fink</link></para>
234 </listitem>
235 </itemizedlist>
236
237 <itemizedlist>
238 <listitem>
239 <para><link
240 xlink:href="http://rpms.mandrakeclub.com/linux/rpm2html/search.php?query=xmlstarlet">Mandrake
241 RPMs</link></para>
242 </listitem>
243 </itemizedlist>
244
245 <itemizedlist>
246 <listitem>
247 <para><link
248 xlink:href="http://gentoo-portage.com/app-text/xmlstarlet">Gentoo
249 Portage</link></para>
250 </listitem>
251 </itemizedlist>
252
253 <itemizedlist>
254 <listitem>
255 <para><link
256 xlink:href="http://packages.debian.org/stable/text/xmlstarlet">Debian</link></para>
257 </listitem>
258 </itemizedlist>
259 </sect1>
260 </chapter>
261
262 <chapter>
263 <title xml:id="s.2">Installation</title>
264
265 <sect1>
266 <title xml:id="s.2.1">Installation on Linux</title>
267
268 <para>Execute the following command as root<programlisting>rpm -i xmlstarlet-x.x.x-1.i386.rpm</programlisting></para>
269
270 <para>where x.x.x indicates package version.</para>
271
272 <para>You can use <link
273 xlink:href="http://fr2.rpmfind.net/linux/rpm2html/search.php?query=xmlstarlet&amp;system=&amp;arch=">http://rpmfind.net</link>
274 to search for RPM appropriate for your distribution.</para>
275 </sect1>
276
277 <sect1>
278 <title xml:id="s.2.2">Installation on Solaris</title>
279
280 <para>Execute the following commands as root<programlisting>gunzip xmlstarlet-x.x.x-sol8-sparc-local.gz
281 pkgadd -d xmlstarlet-x.x.x-sol8-sparc-local all</programlisting></para>
282 </sect1>
283
284 <sect1>
285 <title xml:id="s.2.3">Installation on MacOS X</title>
286
287 <para>XmlStarlet is available on MacOS in Fink. <link
288 xlink:href="http://fink.sourceforge.net/pdb/package.php/xmlstarlet">See
289 fink.sourceforge.net</link></para>
290 </sect1>
291
292 <sect1>
293 <title xml:id="s.2.4">Installation on Windows</title>
294
295 <para>Unzip the file xmlstarlet-x.x.x-win32.zip to some directory. To
296 take advantage of UNIX shell scripting you might want to run XmlStarlet
297 from Cygwin. Consider installing <link
298 xlink:href="http://www.cygwin.com/">Cygwin</link> on your Windows
299 machine.</para>
300 </sect1>
301 </chapter>
302
303 <chapter>
304 <title xml:id="s.3">Getting Started</title>
305
306 <sect1>
307 <title xml:id="s.3.1">Basic Command-Line Options</title>
308
309 <para>Basic command line syntax: <programlisting>bash-2.03$ <phrase role="PROG"/>
310 XMLStarlet Toolkit: Command line utilities for XML
311 Usage: <phrase role="PROG"/> [&lt;options&gt;] &lt;command&gt; [&lt;cmd-options&gt;]
312 where &lt;command&gt; is one of:
313 ed (or edit) - Edit/Update XML document(s)
314 sel (or select) - Select data or query XML document(s) (XPATH, etc)
315 tr (or transform) - Transform XML document(s) using XSLT
316 val (or validate) - Validate XML document(s) (well-formed/DTD/XSD/RelaxNG)
317 fo (or format) - Format XML document(s)
318 el (or elements) - Display element structure of XML document
319 c14n (or canonic) - XML canonicalization
320 ls (or list) - List directory as XML
321 esc (or escape) - Escape special XML characters
322 unesc (or unescape) - Unescape special XML characters
323 pyx (or xmln) - Convert XML into PYX format (based on ESIS - ISO 8879)
324 p2x (or depyx) - Convert PYX into XML
325 &lt;options&gt; are:
326 -q or --quiet - no error output
327 --doc-namespace - extract namespace bindings from input doc (default)
328 --no-doc-namespace - don't extract namespace bindings from input doc
329 --version - show version
330 --help - show help
331 Wherever file name mentioned in command help it is assumed
332 that URL can be used instead as well.
333
334 Type: <phrase role="PROG"/> &lt;command&gt; --help &lt;ENTER&gt; for command help
335
336 XMLStarlet is a command line toolkit to query/edit/check/transform
337 XML documents (for more information see http://xmlstar.sourceforge.net/)</programlisting></para>
338 </sect1>
339
340 <sect1>
341 <title xml:id="s.3.2">Studying Structure of XML Document</title>
342
343 <para>Before you do anything with your XML document you probably would
344 like to know its structure at first. 'el' option could be used for this
345 purpose.</para>
346
347 <para>Let's say you have the following XML document (table.xml)</para>
348
349 <programlisting>&lt;xml&gt;
350 &lt;table&gt;
351 &lt;rec id="1"&gt;
352 &lt;numField&gt;123&lt;/numField&gt;
353 &lt;stringField&gt;String Value&lt;/stringField&gt;
354 &lt;/rec&gt;
355 &lt;rec id="2"&gt;
356 &lt;numField&gt;346&lt;/numField&gt;
357 &lt;stringField&gt;Text Value&lt;/stringField&gt;
358 &lt;/rec&gt;
359 &lt;rec id="3"&gt;
360 &lt;numField&gt;-23&lt;/numField&gt;
361 &lt;stringField&gt;stringValue&lt;/stringField&gt;
362 &lt;/rec&gt;
363 &lt;/table&gt;
364 &lt;/xml&gt;</programlisting>
365
366 <programlisting><phrase role="PROG"/> el table.xml</programlisting>
367
368 <para>would produce the following output.</para>
369
370 <programlisting>xml
371 xml/table
372 xml/table/rec
373 xml/table/rec/numField
374 xml/table/rec/stringField
375 xml/table/rec
376 xml/table/rec/numField
377 xml/table/rec/stringField
378 xml/table/rec
379 xml/table/rec/numField
380 xml/table/rec/stringField</programlisting>
381
382 <para>Every line in this output is an XPath expression which indicates a
383 'path' to elements in XML document. You would use these XPath
384 expressions to navigate through your XML documents in other XmlStarlet
385 options.</para>
386
387 <para>XML documents can be pretty large but with a very simple
388 structure. (This is espesially true for data driven XML documents ex:
389 XML formatted result of select from SQL table). If you just interested
390 in structure but not order of the elements you can use -u switch
391 combined with 'el' option.</para>
392
393 <para>EXAMPLE:</para>
394
395 <programlisting><phrase role="PROG"/> el -u table.xml</programlisting>
396
397 <para>Output:</para>
398
399 <programlisting>xml
400 xml/table
401 xml/table/rec
402 xml/table/rec/numField
403 xml/table/rec/stringField</programlisting>
404
405 <para>If you are interested not just in elements of your XML document,
406 but you want to see attributes as well you can use -a switch with 'el'
407 option. And every line of the output will still be a valid XPath
408 expression.</para>
409
410 <para>EXAMPLE:</para>
411
412 <programlisting><phrase role="PROG"/> el -a table.xml</programlisting>
413
414 <para>Output:</para>
415
416 <programlisting>xml
417 xml/table
418 xml/table/rec
419 xml/table/rec/@id
420 xml/table/rec/numField
421 xml/table/rec/stringField
422 xml/table/rec
423 xml/table/rec/@id
424 xml/table/rec/numField
425 xml/table/rec/stringField
426 xml/table/rec
427 xml/table/rec/@id
428 xml/table/rec/numField
429 xml/table/rec/stringField</programlisting>
430
431 <para>If you are looking for attribute values as well use -v switch of
432 'el' option. And again - every line of output is a valid XPath
433 expression.</para>
434
435 <para>EXAMPLE:</para>
436
437 <programlisting><phrase role="PROG"/> el -v table.xml</programlisting>
438
439 <para>Output:</para>
440
441 <programlisting>xml
442 xml/table
443 xml/table/rec[@id='1']
444 xml/table/rec/numField
445 xml/table/rec/stringField
446 xml/table/rec[@id='2']
447 xml/table/rec/numField
448 xml/table/rec/stringField
449 xml/table/rec[@id='3']
450 xml/table/rec/numField
451 xml/table/rec/stringField</programlisting>
452 </sect1>
453 </chapter>
454
455 <chapter>
456 <title>XmlStarlet Reference</title>
457
458 <para></para>
459
460 <sect1>
461 <title>Querying XML documents</title>
462
463 <para>XmlStarlet 'select' or 'sel' option can be used to query or search
464 XML documents. Here is synopsis for '<phrase role="PROG"/> sel' command:</para>
465
466 <programlisting>XMLStarlet Toolkit: Select from XML document(s)
467 Usage: <phrase role="PROG"/><![CDATA[ sel <global-options> {<template>} [ <xml-file> ... ]
468 where
469 <global-options> - global options for selecting
470 <xml-file> - input XML document file name/uri (stdin is used if missing)
471 <template> - template for querying XML document with following syntax:
472
473 <global-options> are:
474 -Q or --quiet - do not write anything to standard output.
475 -C or --comp - display generated XSLT
476 -R or --root - print root element <xsl-select>
477 -T or --text - output is text (default is XML)
478 -I or --indent - indent output
479 -D or --xml-decl - do not omit xml declaration line
480 -B or --noblanks - remove insignificant spaces from XML tree
481 -E or --encode <encoding> - output in the given encoding (utf-8, unicode...)
482 -N <name>=<value> - predefine namespaces (name without 'xmlns:')
483 ex: xsql=urn:oracle-xsql
484 Multiple -N options are allowed.
485 --net - allow fetch DTDs or entities over network
486 --help - display help
487
488 Syntax for templates: -t|--template <options>
489 where <options>
490 -c or --copy-of <xpath> - print copy of XPATH expression
491 -v or --value-of <xpath> - print value of XPATH expression
492 -o or --output <string> - output string literal
493 -n or --nl - print new line
494 -f or --inp-name - print input file name (or URL)
495 -m or --match <xpath> - match XPATH expression
496 --var <name> <value> --break or
497 --var <name>=<value> - declare a variable (referenced by $name)
498 -i or --if <test-xpath> - check condition <xsl:if test="test-xpath">
499 --elif <test-xpath> - check condition if previous conditions failed
500 --else - check if previous conditions failed
501 -e or --elem <name> - print out element <xsl:element name="name">
502 -a or --attr <name> - add attribute <xsl:attribute name="name">
503 -b or --break - break nesting
504 -s or --sort op xpath - sort in order (used after -m) where
505 op is X:Y:Z,
506 X is A - for order="ascending"
507 X is D - for order="descending"
508 Y is N - for data-type="numeric"
509 Y is T - for data-type="text"
510 Z is U - for case-order="upper-first"
511 Z is L - for case-order="lower-first"
512
513 There can be multiple --match, --copy-of, --value-of, etc options
514 in a single template. The effect of applying command line templates
515 can be illustrated with the following XSLT analogue
516
517 ]]><phrase role="PROG"/><![CDATA[ sel -t -c "xpath0" -m "xpath1" -m "xpath2" -v "xpath3" \
518 -t -m "xpath4" -c "xpath5"
519
520 is equivalent to applying the following XSLT
521
522 <?xml version="1.0"?>
523 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
524 <xsl:template match="/">
525 <xsl:call-template name="t1"/>
526 <xsl:call-template name="t2"/>
527 </xsl:template>
528 <xsl:template name="t1">
529 <xsl:copy-of select="xpath0"/>
530 <xsl:for-each select="xpath1">
531 <xsl:for-each select="xpath2">
532 <xsl:value-of select="xpath3"/>
533 </xsl:for-each>
534 </xsl:for-each>
535 </xsl:template>
536 <xsl:template name="t2">
537 <xsl:for-each select="xpath4">
538 <xsl:copy-of select="xpath5"/>
539 </xsl:for-each>
540 </xsl:template>
541 </xsl:stylesheet>
542
543 ]]></programlisting>
544
545 <para>'select' option allows you basically avoid writting XSLT
546 stylesheet to perform some queries on XML documents. I.e. various
547 combinations of command line parameters will let you to generate XSLT
548 stylesheet and apply in to XML documents with a single command line.
549 Very often you do not really care what XSLT was created for you 'select'
550 command, but in those cases when you do; you can always use -C or --comp
551 switch which will let you see exactly which XSLT is applied to your
552 input.</para>
553
554 <para>'select' option supports many EXSLT functions in XPath
555 expressions.</para>
556
557 <para>Here are few examples which will help to understand how '<phrase role="PROG"/>
558 select' works:</para>
559
560 <para>EXAMPLE:</para>
561
562 <para>Count elements matching XPath expression:</para>
563
564 <para></para>
565
566 <programlisting><phrase role="PROG"/> sel -t -v "count(/xml/table/rec/numField)" table.xml</programlisting>
567
568 <para>Input (table.xml):</para>
569
570 <programlisting>&lt;xml&gt;
571 &lt;table&gt;
572 &lt;rec id="1"&gt;
573 &lt;numField&gt;123&lt;/numField&gt;
574 &lt;stringField&gt;String Value&lt;/stringField&gt;
575 &lt;/rec&gt;
576 &lt;rec id="2"&gt;
577 &lt;numField&gt;346&lt;/numField&gt;
578 &lt;stringField&gt;Text Value&lt;/stringField&gt;
579 &lt;/rec&gt;
580 &lt;rec id="3"&gt;
581 &lt;numField&gt;-23&lt;/numField&gt;
582 &lt;stringField&gt;stringValue&lt;/stringField&gt;
583 &lt;/rec&gt;
584 &lt;/table&gt;
585 &lt;/xml&gt;</programlisting>
586
587 <para>Output:</para>
588
589 <programlisting>3
590 </programlisting>
591
592 <para>Let's take a close look what it did internally. For that we will
593 use '-C' option</para>
594
595 <programlisting>$ <phrase role="PROG"/> sel -C -t -v "count(/xml/table/rec/numField)"
596 &lt;?xml version="1.0"?&gt;
597 &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
598 xmlns:exslt="http://exslt.org/common"
599 xmlns:math="http://exslt.org/math"
600 xmlns:date="http://exslt.org/dates-and-times"
601 xmlns:func="http://exslt.org/functions"
602 xmlns:set="http://exslt.org/sets"
603 xmlns:str="http://exslt.org/strings"
604 xmlns:dyn="http://exslt.org/dynamic"
605 xmlns:saxon="http://icl.com/saxon"
606 xmlns:xalanredirect="org.apache.xalan.xslt.extensions.Redirect"
607 xmlns:xt="http://www.jclark.com/xt"
608 xmlns:libxslt="http://xmlsoft.org/XSLT/namespace"
609 xmlns:test="http://xmlsoft.org/XSLT/"
610 extension-element-prefixes=
611 "exslt math date func set str dyn saxon xalanredirect xt libxslt test"
612 exclude-result-prefixes="math str"&gt;
613 &lt;xsl:output omit-xml-declaration="yes" indent="no"/&gt;
614 &lt;xsl:param name="inputFile"&gt;-&lt;/xsl:param&gt;
615 &lt;xsl:template match="/"&gt;
616 &lt;xsl:call-template name="t1"/&gt;
617 &lt;/xsl:template&gt;
618 &lt;xsl:template name="t1"&gt;
619 &lt;xsl:value-of select="count(/xml/table/rec/numField)"/&gt;
620 &lt;/xsl:template&gt;
621 &lt;/xsl:stylesheet&gt;</programlisting>
622
623 <para>Ignoring some XSLT stuff to make it brief:</para>
624
625 <programlisting>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
626 &lt;xsl:output omit-xml-declaration="yes" indent="no"/&gt;
627 &lt;xsl:param name="inputFile"&gt;-&lt;/xsl:param&gt;
628 &lt;xsl:template match="/"&gt;
629 &lt;xsl:call-template name="t1"/&gt;
630 &lt;/xsl:template&gt;
631 &lt;xsl:template name="t1"&gt;
632 &lt;xsl:value-of select="count(/xml/table/rec/numField)"/&gt;
633 &lt;/xsl:template&gt;
634 &lt;/xsl:stylesheet&gt;</programlisting>
635
636 <para>Every -t option is mapped into XSLT template. Options after '-t'
637 are mapped into XSLT elements:</para>
638
639 <itemizedlist>
640 <listitem>
641 <para>-v to &lt;xsl:value-of&gt;</para>
642 </listitem>
643
644 <listitem>
645 <para>-c to &lt;xsl:copy-of&gt;</para>
646 </listitem>
647
648 <listitem>
649 <para>-e to &lt;xsl:element&gt;</para>
650 </listitem>
651
652 <listitem>
653 <para>-a to &lt;xsl:attribute&gt;</para>
654 </listitem>
655
656 <listitem>
657 <para>-s to &lt;xsl:sort&gt;</para>
658 </listitem>
659
660 <listitem>
661 <para>-m to &lt;xsl:for-each&gt;</para>
662 </listitem>
663
664 <listitem>
665 <para>-i to &lt;xsl:if&gt;</para>
666 </listitem>
667
668 <listitem>
669 <para>and so on</para>
670 </listitem>
671 </itemizedlist>
672
673 <para>By default subsequent options (for instance: -m) will result in
674 nested corresponding XSLT elements (&lt;xsl:for-each&gt; for '-m'). To
675 break this nesting you would have to put '-b' or '--break' after first
676 '-m'.</para>
677
678 <para>Below are few more examples:</para>
679
680 <para>EXAMPLE</para>
681
682 <para>Count all nodes in XML documents. Print input name and node count
683 after it.</para>
684
685 <programlisting><phrase role="PROG"/> sel -t -f -o " " -v "count(//node())" xml/table.xml xml/tab-obj.xml</programlisting>
686
687 <para>Output:</para>
688
689 <programlisting>xml/table.xml 32
690 xml/tab-obj.xml 41</programlisting>
691
692 <para></para>
693
694 <para>EXAMPLE</para>
695
696 <para>Find XML files matching XPath expression (containing 'object'
697 element)</para>
698
699 <programlisting><phrase role="PROG"/> sel -t -m //object -f xml/table.xml xml/tab-obj.xml</programlisting>
700
701 <para>Result output:</para>
702
703 <programlisting>xml/tab-obj.xml</programlisting>
704
705 <para></para>
706
707 <para>EXAMPLE</para>
708
709 <para>Calculate EXSLT (XSLT extentions) XPath value</para>
710
711 <programlisting>echo "&lt;x/&gt;" | <phrase role="PROG"/> sel -t -v "math:abs(-1000)"</programlisting>
712
713 <para>Result output:</para>
714
715 <programlisting>1000</programlisting>
716
717 <para></para>
718
719 <para>EXAMPLE</para>
720
721 <para>Adding elements and attributes using command line '<phrase role="PROG"/> sel'</para>
722
723 <programlisting>echo "&lt;x/&gt;" | <phrase role="PROG"/> sel -t -m / -e xml -e child -a data -o value</programlisting>
724
725 <para>Result Output:</para>
726
727 <programlisting>&lt;xml&gt;&lt;child data="value"/&gt;&lt;/xml&gt;</programlisting>
728
729 <para></para>
730
731 <para>EXAMPLE</para>
732
733 <para>Query XML document and produce sorted text table</para>
734
735 <programlisting><phrase role="PROG"/> sel -T -t -m /xml/table/rec -s D:N:- "@id" \
736 -v "concat(@id,'|',numField,'|',stringField)" -n xml/table.xml</programlisting>
737
738 <para>Result Output:</para>
739
740 <programlisting>3|-23|stringValue
741 2|346|Text Value
742 1|123|String Value</programlisting>
743
744 <para>Equivalent stylesheet</para>
745
746 <programlisting>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
747 &lt;xsl:output omit-xml-declaration="yes" indent="no" method="text"/&gt;
748 &lt;xsl:param name="inputFile"&gt;-&lt;/xsl:param&gt;
749 &lt;xsl:template match="/"&gt;
750 &lt;xsl:call-template name="t1"/&gt;
751 &lt;/xsl:template&gt;
752 &lt;xsl:template name="t1"&gt;
753 &lt;xsl:for-each select="/xml/table/rec"&gt;
754 &lt;xsl:sort order="descending" data-type="number"
755 case-order="upper-first" select="@id"/&gt;
756 &lt;xsl:value-of select="concat(@id,'|',numField,'|',stringField)"/&gt;
757 &lt;xsl:value-of select="'&amp;#10;'"/&gt;
758 &lt;/xsl:for-each&gt;
759 &lt;/xsl:template&gt;
760 &lt;/xsl:stylesheet&gt;</programlisting>
761
762 <para></para>
763
764 <para>EXAMPLE</para>
765
766 <para>Predefine namespaces for XPath expressions</para>
767
768 <programlisting><phrase role="PROG"/> sel -N xsql=urn:oracle-xsql -t -v /xsql:query xsql/jobserve.xsql</programlisting>
769
770 <para>Input (xsql/jobserve.xsql)</para>
771
772 <programlisting>$ cat xsql/jobserve.xsql
773 &lt;?xml version="1.0"?&gt;
774 &lt;?xml-stylesheet type="text/xsl" href="jobserve.xsl"?&gt;
775 &lt;xsql:query connection="jobs" xmlns:xsql="urn:oracle-xsql" max-rows="5"&gt;
776 SELECT substr(title,1,26) short_title, title, location, skills
777 FROM job
778 WHERE UPPER(title) LIKE '%ORACLE%'
779 ORDER BY first_posted DESC
780 &lt;/xsql:query&gt;</programlisting>
781
782 <para>Result output</para>
783
784 <programlisting> SELECT substr(title,1,26) short_title, title, location, skills
785 FROM job
786 WHERE UPPER(title) LIKE '%ORACLE%'
787 ORDER BY first_posted DESC
788 </programlisting>
789
790 <para></para>
791
792 <para>EXAMPLE</para>
793
794 <para>Print structure of XML element using <phrase role="PROG"/> sel (advanced XPath
795 expressions and <phrase role="PROG"/> sel command usage)</para>
796
797 <programlisting><phrase role="PROG"/> sel -T -t -m '//*' \
798 -m 'ancestor-or-self::*' -v 'name()' -i 'not(position()=last())' -o . -b -b -n \
799 xml/structure.xml</programlisting>
800
801 <para>Input (xml/structure.xml)</para>
802
803 <programlisting>&lt;a1&gt;
804 &lt;a11&gt;
805 &lt;a111&gt;
806 &lt;a1111/&gt;
807 &lt;/a111&gt;
808 &lt;a112&gt;
809 &lt;a1121/&gt;
810 &lt;/a112&gt;
811 &lt;/a11&gt;
812 &lt;a12/&gt;
813 &lt;a13&gt;
814 &lt;a131/&gt;
815 &lt;/a13&gt;
816 &lt;/a1&gt;</programlisting>
817
818 <para>Result Output:</para>
819
820 <programlisting>a1
821 a1.a11
822 a1.a11.a111
823 a1.a11.a111.a1111
824 a1.a11.a112
825 a1.a11.a112.a1121
826 a1.a12
827 a1.a13
828 a1.a13.a131</programlisting>
829
830 <para>This example is a good demonstration of nesting control. Here is
831 corresponding XSLT:</para>
832
833 <programlisting>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
834 &lt;xsl:output omit-xml-declaration="yes" indent="no" method="text"/&gt;
835 &lt;xsl:param name="inputFile"&gt;-&lt;/xsl:param&gt;
836 &lt;xsl:template match="/"&gt;
837 &lt;xsl:call-template name="t1"/&gt;
838 &lt;/xsl:template&gt;
839 &lt;xsl:template name="t1"&gt;
840 &lt;xsl:for-each select="//*"&gt;
841 &lt;xsl:for-each select="ancestor-or-self::*"&gt;
842 &lt;xsl:value-of select="name()"/&gt;
843 &lt;xsl:if test="not(position()=last())"&gt;
844 &lt;xsl:value-of select="'.'"/&gt;
845 &lt;/xsl:if&gt;
846 &lt;/xsl:for-each&gt;
847 &lt;xsl:value-of select="'&amp;#10;'"/&gt;
848 &lt;/xsl:for-each&gt;
849 &lt;/xsl:template&gt;
850 &lt;/xsl:stylesheet&gt;</programlisting>
851
852 <para></para>
853
854 <para></para>
855
856 <para>EXAMPLE</para>
857
858 <para>Print all links of xhtml document</para>
859
860 <programlisting><phrase role="PROG"/> sel --net --html -T -t -m "//*[local-name()='a']" \
861 -o 'NAME: ' -v "translate(. , '&amp;#10;', ' ')" -n \
862 -o 'LINK: ' -v @href -n -n \
863 http://xmlstar.sourceforge.net/</programlisting>
864
865 <para>Sample output</para>
866
867 <programlisting>NAME: XmlStarlet SourceForge Site
868 LINK: http://sourceforge.net/projects/xmlstar/
869
870 NAME: XmlStarlet CVS Source
871 LINK: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/xmlstar/
872
873 NAME: XmlStarlet on Freshmeat.Net
874 LINK: http://freshmeat.net/projects/xmlstarlet/
875
876 NAME: XMLStarlet Sourceforge forums
877 LINK: http://sourceforge.net/forum/?group_id=66612
878
879 NAME: XMLStarlet mailing list
880 LINK: http://lists.sourceforge.net/lists/listinfo/xmlstar-devel
881 </programlisting>
882
883 <para></para>
884 </sect1>
885
886 <sect1>
887 <title>Transforming XML documents</title>
888
889 <para>Here is synopsis for '<phrase role="PROG"/> tr' command:</para>
890
891 <programlisting>XMLStarlet Toolkit: Transform XML document(s) using XSLT
892 Usage: <phrase role="PROG"/> tr [&lt;options&gt;] &lt;xsl-file&gt; {-p|-s &lt;name&gt;=&lt;value&gt;} [ &lt;xml-file-or-uri&gt; ... ]
893 where
894 &lt;xsl-file&gt; - main XSLT stylesheet for transformation
895 &lt;xml-file&gt; - input XML document file name (stdin is used if missing)
896 &lt;name&gt;=&lt;value&gt; - name and value of the parameter passed to XSLT processor
897 -p - parameter is XPATH expression ("'string'" to quote string)
898 -s - parameter is a string literal
899 &lt;options&gt; are:
900 --omit-decl - omit xml declaration &lt;?xml version="1.0"?&gt;
901 --show-ext - show list of extensions
902 --val - allow validate against DTDs or schemas
903 --net - allow fetch DTDs or entities over network
904 --xinclude - do XInclude processing on document input
905 --maxdepth val - increase the maximum depth
906 --html - input document(s) is(are) in HTML format
907 --catalogs - use SGML catalogs from $SGML_CATALOG_FILES
908 otherwise XML catalogs starting from
909 file:///etc/xml/catalog are activated by default
910
911 XMLStarlet is a command line toolkit to query/edit/check/transform
912 XML documents (for more information see http://xmlstar.sourceforge.net/)
913
914 Current implementation uses libxslt from GNOME codebase as XSLT processor
915 (see http://xmlsoft.org/ for more details)
916 </programlisting>
917
918 <para>EXAMPLE:</para>
919
920 <programlisting># Transform passing parameters to XSLT stylesheet
921 <phrase role="PROG"/> tr xsl/param1.xsl -p Count='count(/xml/table/rec)' -s Text="Count=" xml/table.xml
922 </programlisting>
923
924 <para>Input xsl/params1.xsl</para>
925
926 <programlisting>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
927 &lt;xsl:output method="text"/&gt;
928 &lt;xsl:param name="Text"/&gt;
929 &lt;xsl:param name="Count"/&gt;
930 &lt;xsl:template match="/"&gt;
931 &lt;xsl:call-template name="t1"/&gt;
932 &lt;/xsl:template&gt;
933 &lt;xsl:template name="t1"&gt;
934 &lt;xsl:for-each select="/xml"&gt;
935 &lt;xsl:value-of select="$Text"/&gt;
936 &lt;xsl:value-of select="$Count"/&gt;
937 &lt;xsl:value-of select="'&amp;#10;'"/&gt;
938 &lt;/xsl:for-each&gt;
939 &lt;/xsl:template&gt;
940 &lt;/xsl:stylesheet&gt;</programlisting>
941
942 <para>Output</para>
943
944 <programlisting>Count=3
945 </programlisting>
946 </sect1>
947
948 <sect1>
949 <title>Editing XML documents</title>
950
951 <para>Here is the synopsis for '<phrase role="PROG"/> ed' command:</para>
952
953 <programlisting>XMLStarlet Toolkit: Edit XML document(s)
954 Usage: <phrase role="PROG"/> ed &lt;global-options&gt; {&lt;action&gt;} [ &lt;xml-file-or-uri&gt; ... ]
955 where
956 &lt;global-options&gt; - global options for editing
957 &lt;xml-file-or-uri&gt; - input XML document file name/uri (stdin is used if missing)
958
959 &lt;global-options&gt; are:
960 -P (or --pf) - preserve original formatting
961 -S (or --ps) - preserve non-significant spaces
962 -O (or --omit-decl) - omit XML declaration (&lt;?xml ...?&gt;)
963 -N &lt;name&gt;=&lt;value&gt; - predefine namespaces (name without 'xmlns:')
964 ex: xsql=urn:oracle-xsql
965 Multiple -N options are allowed.
966 -N options must be last global options.
967 --help or -h - display help
968
969 where &lt;action&gt;
970 -d or --delete &lt;xpath&gt;
971 -i or --insert &lt;xpath&gt; -t (--type) elem|text|attr -n &lt;name&gt; -v (--value) &lt;value&gt;
972 -a or --append &lt;xpath&gt; -t (--type) elem|text|attr -n &lt;name&gt; -v (--value) &lt;value&gt;
973 -s or --subnode &lt;xpath&gt; -t (--type) elem|text|attr -n &lt;name&gt; -v (--value) &lt;value&gt;
974 -m or --move &lt;xpath1&gt; &lt;xpath2&gt;
975 -r or --rename &lt;xpath1&gt; -v &lt;new-name&gt;
976 -u or --update &lt;xpath&gt; -v (--value) &lt;value&gt;
977 -x (--expr) &lt;xpath&gt;
978
979 XMLStarlet is a command line toolkit to query/edit/check/transform
980 XML documents (for more information see http://xmlstar.sourceforge.net/)
981 </programlisting>
982
983 <para>EXAMPLE:</para>
984
985 <programlisting># Delete elements matching XPath expression
986 <phrase role="PROG"/> ed -d "/xml/table/rec[@id='2']" xml/table.xml
987 </programlisting>
988
989 <para>Input</para>
990
991 <programlisting>&lt;xml&gt;
992 &lt;table&gt;
993 &lt;rec id="1"&gt;
994 &lt;numField&gt;123&lt;/numField&gt;
995 &lt;stringField&gt;String Value&lt;/stringField&gt;
996 &lt;/rec&gt;
997 &lt;rec id="2"&gt;
998 &lt;numField&gt;346&lt;/numField&gt;
999 &lt;stringField&gt;Text Value&lt;/stringField&gt;
1000 &lt;/rec&gt;
1001 &lt;rec id="3"&gt;
1002 &lt;numField&gt;-23&lt;/numField&gt;
1003 &lt;stringField&gt;stringValue&lt;/stringField&gt;
1004 &lt;/rec&gt;
1005 &lt;/table&gt;
1006 &lt;/xml&gt;
1007 </programlisting>
1008
1009 <para>Output</para>
1010
1011 <programlisting>&lt;xml&gt;
1012 &lt;table&gt;
1013 &lt;rec id="1"&gt;
1014 &lt;numField&gt;123&lt;/numField&gt;
1015 &lt;stringField&gt;String Value&lt;/stringField&gt;
1016 &lt;/rec&gt;
1017 &lt;rec id="3"&gt;
1018 &lt;numField&gt;-23&lt;/numField&gt;
1019 &lt;stringField&gt;stringValue&lt;/stringField&gt;
1020 &lt;/rec&gt;
1021 &lt;/table&gt;
1022 &lt;/xml&gt;
1023 </programlisting>
1024
1025 <para>EXAMPLE</para>
1026
1027 <programlisting># Move element node
1028 echo '&lt;x id="1"&gt;&lt;a/&gt;&lt;b/&gt;&lt;/x&gt;' | <phrase role="PROG"/> ed -m "//b" "//a"
1029 </programlisting>
1030
1031 <para>Output</para>
1032
1033 <programlisting>&lt;x id="1"&gt;
1034 &lt;a&gt;
1035 &lt;b/&gt;
1036 &lt;/a&gt;
1037 &lt;/x&gt;
1038 </programlisting>
1039
1040 <para>EXAMPLE</para>
1041
1042 <programlisting># Rename attributes
1043 <phrase role="PROG"/> ed -r "//*/@id" -v ID xml/tab-obj.xml
1044 </programlisting>
1045
1046 <para>Output:</para>
1047
1048 <programlisting>&lt;xml&gt;
1049 &lt;table&gt;
1050 &lt;rec ID="1"&gt;
1051 &lt;numField&gt;123&lt;/numField&gt;
1052 &lt;stringField&gt;String Value&lt;/stringField&gt;
1053 &lt;object name="Obj1"&gt;
1054 &lt;property name="size"&gt;10&lt;/property&gt;
1055 &lt;property name="type"&gt;Data&lt;/property&gt;
1056 &lt;/object&gt;
1057 &lt;/rec&gt;
1058 &lt;rec ID="2"&gt;
1059 &lt;numField&gt;346&lt;/numField&gt;
1060 &lt;stringField&gt;Text Value&lt;/stringField&gt;
1061 &lt;/rec&gt;
1062 &lt;rec ID="3"&gt;
1063 &lt;numField&gt;-23&lt;/numField&gt;
1064 &lt;stringField&gt;stringValue&lt;/stringField&gt;
1065 &lt;/rec&gt;
1066 &lt;/table&gt;
1067 &lt;/xml&gt;
1068 </programlisting>
1069
1070 <para>EXAMPLE</para>
1071
1072 <programlisting># Rename elements
1073 <phrase role="PROG"/> ed -r "/xml/table/rec" -v record xml/tab-obj.xml
1074 </programlisting>
1075
1076 <para>Output:</para>
1077
1078 <programlisting>&lt;xml&gt;
1079 &lt;table&gt;
1080 &lt;record id="1"&gt;
1081 &lt;numField&gt;123&lt;/numField&gt;
1082 &lt;stringField&gt;String Value&lt;/stringField&gt;
1083 &lt;object name="Obj1"&gt;
1084 &lt;property name="size"&gt;10&lt;/property&gt;
1085 &lt;property name="type"&gt;Data&lt;/property&gt;
1086 &lt;/object&gt;
1087 &lt;/record&gt;
1088 &lt;record id="2"&gt;
1089 &lt;numField&gt;346&lt;/numField&gt;
1090 &lt;stringField&gt;Text Value&lt;/stringField&gt;
1091 &lt;/record&gt;
1092 &lt;record id="3"&gt;
1093 &lt;numField&gt;-23&lt;/numField&gt;
1094 &lt;stringField&gt;stringValue&lt;/stringField&gt;
1095 &lt;/record&gt;
1096 &lt;/table&gt;
1097 &lt;/xml&gt;
1098 </programlisting>
1099
1100 <para>EXAMPLE</para>
1101
1102 <programlisting># Update value of an attribute
1103 <phrase role="PROG"/> ed -u "/xml/table/rec[@id=3]/@id" -v 5 xml/tab-obj.xml
1104 </programlisting>
1105
1106 <para>Output:</para>
1107
1108 <programlisting>&lt;xml&gt;
1109 &lt;table&gt;
1110 &lt;rec id="1"&gt;
1111 &lt;numField&gt;123&lt;/numField&gt;
1112 &lt;stringField&gt;String Value&lt;/stringField&gt;
1113 &lt;object name="Obj1"&gt;
1114 &lt;property name="size"&gt;10&lt;/property&gt;
1115 &lt;property name="type"&gt;Data&lt;/property&gt;
1116 &lt;/object&gt;
1117 &lt;/rec&gt;
1118 &lt;rec id="2"&gt;
1119 &lt;numField&gt;346&lt;/numField&gt;
1120 &lt;stringField&gt;Text Value&lt;/stringField&gt;
1121 &lt;/rec&gt;
1122 &lt;rec id="5"&gt;
1123 &lt;numField&gt;-23&lt;/numField&gt;
1124 &lt;stringField&gt;stringValue&lt;/stringField&gt;
1125 &lt;/rec&gt;
1126 &lt;/table&gt;
1127 &lt;/xml&gt;
1128 </programlisting>
1129
1130 <para>EXAMPLE</para>
1131
1132 <programlisting># Update value of an element
1133 <phrase role="PROG"/> ed -u "/xml/table/rec[@id=1]/numField" -v 0 xml/tab-obj.xml
1134 </programlisting>
1135
1136 <para>Output:</para>
1137
1138 <programlisting>&lt;xml&gt;
1139 &lt;table&gt;
1140 &lt;rec id="1"&gt;
1141 &lt;numField&gt;0&lt;/numField&gt;
1142 &lt;stringField&gt;String Value&lt;/stringField&gt;
1143 &lt;object name="Obj1"&gt;
1144 &lt;property name="size"&gt;10&lt;/property&gt;
1145 &lt;property name="type"&gt;Data&lt;/property&gt;
1146 &lt;/object&gt;
1147 &lt;/rec&gt;
1148 &lt;rec id="2"&gt;
1149 &lt;numField&gt;346&lt;/numField&gt;
1150 &lt;stringField&gt;Text Value&lt;/stringField&gt;
1151 &lt;/rec&gt;
1152 &lt;rec id="3"&gt;
1153 &lt;numField&gt;-23&lt;/numField&gt;
1154 &lt;stringField&gt;stringValue&lt;/stringField&gt;
1155 &lt;/rec&gt;
1156 &lt;/table&gt;
1157 &lt;/xml&gt;
1158 </programlisting>
1159 </sect1>
1160
1161 <sect1>
1162 <title>Validating XML documents</title>
1163
1164 <para>Here is synopsis for '<phrase role="PROG"/> val' command:</para>
1165
1166 <programlisting>XMLStarlet Toolkit: Validate XML document(s)
1167 Usage: <phrase role="PROG"/> val &lt;options&gt; [ &lt;xml-file-or-uri&gt; ... ]
1168 where &lt;options&gt;
1169 -w or --well-formed - validate well-formedness only (default)
1170 -d or --dtd &lt;dtd-file&gt; - validate against DTD
1171 -s or --xsd &lt;xsd-file&gt; - validate against XSD schema
1172 -r or --relaxng &lt;rng-file&gt; - validate against Relax-NG schema
1173 -e or --err - print verbose error messages on stderr
1174 -b or --list-bad - list only files which do not validate
1175 -g or --list-good - list only files which validate
1176 -q or --quiet - do not list files (return result code only)
1177
1178 NOTE: XML Schemas are not fully supported yet due to its incomplete
1179 support in libxml (see http://xmlsoft.org)
1180
1181 XMLStarlet is a command line toolkit to query/edit/check/transform
1182 XML documents (for more information see http://xmlstar.sourceforge.net/)
1183 </programlisting>
1184
1185 <para>EXAMPLE</para>
1186
1187 <programlisting># Validate XML document against DTD
1188 <phrase role="PROG"/> val --dtd dtd/table.dtd xml/tab-obj.xml &gt;/dev/null 2&gt;&amp;1; echo $?
1189 </programlisting>
1190
1191 <para>Output:</para>
1192
1193 <programlisting>1
1194 </programlisting>
1195
1196 <para>EXAMPLE</para>
1197
1198 <programlisting># Validate against XSD schema
1199 <phrase role="PROG"/> val -b -s xsd/table.xsd xml/table.xml xml/tab-obj.xml 2&gt;/dev/null; echo $?
1200 </programlisting>
1201
1202 <para>Output:</para>
1203
1204 <programlisting>xml/tab-obj.xml
1205 1
1206 </programlisting>
1207 </sect1>
1208
1209 <sect1>
1210 <title>Formatting XML documents</title>
1211
1212 <para>Here is synopsis for '<phrase role="PROG"/> fo' command:</para>
1213
1214 <programlisting>XMLStarlet Toolkit: Format XML document
1215 Usage: <phrase role="PROG"/> fo [&lt;options&gt;] &lt;xml-file&gt;
1216 where &lt;options&gt; are
1217 -n or --noindent - do not indent
1218 -t or --indent-tab - indent output with tabulation
1219 -s or --indent-spaces &lt;num&gt; - indent output with &lt;num&gt; spaces
1220 -o or --omit-decl - omit xml declaration &lt;?xml version="1.0"?&gt;
1221 -R or --recover - try to recover what is parsable
1222 -D or --dropdtd - remove the DOCTYPE of the input docs
1223 -C or --nocdata - replace cdata section with text nodes
1224 -N or --nsclean - remove redundant namespace declarations
1225 -e or --encode &lt;encoding&gt; - output in the given encoding (utf-8, unicode...)
1226 -H or --html - input is HTML
1227 -h or --help - print help
1228
1229 XMLStarlet is a command line toolkit to query/edit/check/transform
1230 XML documents (for more information see http://xmlstar.sourceforge.net/)
1231 </programlisting>
1232
1233 <para>EXAMPLE</para>
1234
1235 <programlisting># Format XML document disabling indent
1236 cat xml/tab-obj.xml | <phrase role="PROG"/> fo --noindent
1237 </programlisting>
1238
1239 <para>Output:</para>
1240
1241 <programlisting>&lt;xml&gt;
1242 &lt;table&gt;
1243 &lt;rec id="1"&gt;
1244 &lt;numField&gt;123&lt;/numField&gt;
1245 &lt;stringField&gt;String Value&lt;/stringField&gt;
1246 &lt;object name="Obj1"&gt;
1247 &lt;property name="size"&gt;10&lt;/property&gt;
1248 &lt;property name="type"&gt;Data&lt;/property&gt;
1249 &lt;/object&gt;
1250 &lt;/rec&gt;
1251 &lt;rec id="2"&gt;
1252 &lt;numField&gt;346&lt;/numField&gt;
1253 &lt;stringField&gt;Text Value&lt;/stringField&gt;
1254 &lt;/rec&gt;
1255 &lt;rec id="3"&gt;
1256 &lt;numField&gt;-23&lt;/numField&gt;
1257 &lt;stringField&gt;stringValue&lt;/stringField&gt;
1258 &lt;/rec&gt;
1259 &lt;/table&gt;
1260 &lt;/xml&gt;
1261 </programlisting>
1262
1263 <para>EXAMPLE</para>
1264
1265 <programlisting># Recover malformed XML document
1266 <phrase role="PROG"/> fo -R xml/malformed.xml 2&gt;/dev/null
1267 </programlisting>
1268
1269 <para>Input:</para>
1270
1271 <programlisting>&lt;test_output&gt;
1272 &lt;test_name&gt;foo&lt;/testname&gt;
1273 &lt;subtest&gt;...&lt;/subtest&gt;
1274 &lt;/test_output&gt;
1275 </programlisting>
1276
1277 <para>Output:</para>
1278
1279 <programlisting>&lt;test_output&gt;
1280 &lt;test_name&gt;foo&lt;/test_name&gt;
1281 &lt;subtest&gt;...&lt;/subtest&gt;
1282 &lt;/test_output&gt;
1283 </programlisting>
1284 </sect1>
1285
1286 <sect1>
1287 <title>Canonicalization of XML documents</title>
1288
1289 <para>Here is synopsis for '<phrase role="PROG"/> c14n' command:</para>
1290
1291 <programlisting>XMLStarlet Toolkit: XML canonicalization
1292 Usage: <phrase role="PROG"/> c14n &lt;mode&gt; &lt;xml-file&gt; [&lt;xpath-file&gt;] [&lt;inclusive-ns-list&gt;]
1293 where
1294 &lt;xml-file&gt; - input XML document file name (stdin is used if '-')
1295 &lt;xpath-file&gt; - XML file containing XPath expression for
1296 c14n XML canonicalization
1297 Example:
1298 &lt;?xml version="1.0"?&gt;
1299 &lt;XPath xmlns:n0="http://a.example.com" xmlns:n1="http://b.example"&gt;
1300 (//. | //@* | //namespace::*)[ancestor-or-self::n1:elem1]
1301 &lt;/XPath&gt;
1302
1303 &lt;inclusive-ns-list&gt; - the list of inclusive namespace prefixes
1304 (only for exclusive canonicalization)
1305 Example: 'n1 n2'
1306
1307 &lt;mode&gt; is one of following:
1308 --with-comments XML file canonicalization w comments (default)
1309 --without-comments XML file canonicalization w/o comments
1310 --exc-with-comments Exclusive XML file canonicalization w comments
1311 --exc-without-comments Exclusive XML file canonicalization w/o comments
1312
1313 XMLStarlet is a command line toolkit to query/edit/check/transform
1314 XML documents (for more information see http://xmlstar.sourceforge.net/)
1315 </programlisting>
1316
1317 <para>EXAMPLE</para>
1318
1319 <programlisting># XML canonicalization
1320 <phrase role="PROG"/> c14n --with-comments ../examples/xml/structure.xml ; echo $?
1321 </programlisting>
1322
1323 <para>Input ../examples/xml/structure.xml</para>
1324
1325 <programlisting>&lt;a1&gt;
1326 &lt;a11&gt;
1327 &lt;a111&gt;
1328 &lt;a1111/&gt;
1329 &lt;/a111&gt;
1330 &lt;a112&gt;
1331 &lt;a1121/&gt;
1332 &lt;/a112&gt;
1333 &lt;/a11&gt;
1334 &lt;a12/&gt;
1335 &lt;a13&gt;
1336 &lt;a131/&gt;
1337 &lt;/a13&gt;
1338 &lt;/a1&gt;</programlisting>
1339
1340 <para>Output</para>
1341
1342 <programlisting>&lt;a1&gt;
1343 &lt;a11&gt;
1344 &lt;a111&gt;
1345 &lt;a1111&gt;&lt;/a1111&gt;
1346 &lt;/a111&gt;
1347 &lt;a112&gt;
1348 &lt;a1121&gt;&lt;/a1121&gt;
1349 &lt;/a112&gt;
1350 &lt;/a11&gt;
1351 &lt;a12&gt;&lt;/a12&gt;
1352 &lt;a13&gt;
1353 &lt;a131&gt;&lt;/a131&gt;
1354 &lt;/a13&gt;
1355 &lt;/a1&gt;
1356 0
1357 </programlisting>
1358
1359 <para>EXAMPLE</para>
1360
1361 <programlisting># XML exclusive canonicalization
1362 <phrase role="PROG"/> c14n --exc-with-comments ../examples/xml/c14n.xml ../examples/xml/c14n.xpath
1363 </programlisting>
1364
1365 <para>Input</para>
1366
1367 <programlisting>../examples/xml/c14n.xml
1368
1369 &lt;n0:pdu xmlns:n0='http://a.example.com'&gt;
1370 &lt;n1:elem1 xmlns:n1='http://b.example'&gt;
1371 content
1372 &lt;/n1:elem1&gt;
1373 &lt;/n0:pdu&gt;
1374
1375 ../examples/xml/c14n.xpath
1376
1377 &lt;XPath xmlns:n0="http://a.example.com" xmlns:n1="http://b.example"&gt;
1378 (//. | //@* | //namespace::*)[ancestor-or-self::n1:elem1]
1379 &lt;/XPath&gt;
1380
1381 </programlisting>
1382
1383 <para>Output</para>
1384
1385 <programlisting>&lt;n1:elem1 xmlns:n1="http://b.example"&gt;
1386 content
1387 &lt;/n1:elem1&gt;
1388 </programlisting>
1389 </sect1>
1390
1391 <sect1>
1392 <title>XML and PYX format</title>
1393
1394 <para>Here is synopsis for '<phrase role="PROG"/> pyx' command:</para>
1395
1396 <programlisting>XMLStarlet Toolkit: Convert XML into PYX format (based on ESIS - ISO 8879)
1397 Usage: <phrase role="PROG"/> pyx {&lt;xml-file&gt;}
1398 where
1399 &lt;xml-file&gt; - input XML document file name (stdin is used if missing)
1400
1401 The PYX format is a line-oriented representation of
1402 XML documents that is derived from the SGML ESIS format.
1403 (see ESIS - ISO 8879 Element Structure Information Set spec,
1404 ISO/IEC JTC1/SC18/WG8 N931 (ESIS))
1405
1406 A non-validating, ESIS generating tool originally developed for
1407 pyxie project (see http://pyxie.sourceforge.net/)
1408 ESIS Generation by Sean Mc Grath http://www.digitome.com/sean.html
1409
1410 XMLStarlet is a command line toolkit to query/edit/check/transform
1411 XML documents (for more information see http://xmlstar.sourceforge.net/)
1412 </programlisting>
1413
1414 <para>EXAMPLE</para>
1415
1416 <programlisting><phrase role="PROG"/> pyx input.xml
1417 </programlisting>
1418
1419 <para>Input (input.xml)</para>
1420
1421 <programlisting>&lt;books&gt;
1422 &lt;book type='hardback'&gt;
1423 &lt;title&gt;Atlas Shrugged&lt;/title&gt;
1424 &lt;author&gt;Ayn Rand&lt;/author&gt;
1425 &lt;isbn id='1'&gt;0525934189&lt;/isbn&gt;
1426 &lt;/book&gt;
1427 &lt;/books&gt;</programlisting>
1428
1429 <para>Output</para>
1430
1431 <programlisting>(books
1432 -\n
1433 (book
1434 Atype hardback
1435 -\n
1436 (title
1437 -Atlas Shrugged
1438 )title
1439 -\n
1440 (author
1441 -Ayn Rand
1442 )author
1443 -\n
1444 (isbn
1445 Aid 1
1446 -0525934189
1447 )isbn
1448 -\n
1449 )book
1450 -\n
1451 )books</programlisting>
1452
1453 <para>PYX is a line oriented format for XML files which can be helpful
1454 (and very efficient) when used in combination with regular line oriented
1455 UNIX command such as sed, grep, awk.</para>
1456
1457 <para>'depyx' option is used for conversion back from PYX into
1458 XML.</para>
1459
1460 <para>EXAMPLE (Delete all attributes). This should work really fast for
1461 very large XML documents.</para>
1462
1463 <programlisting><phrase role="PROG"/> pyx input.xml | grep -v "^A" | <phrase role="PROG"/> depyx</programlisting>
1464
1465 <para>Output</para>
1466
1467 <programlisting>&lt;books&gt;
1468 &lt;book&gt;
1469 &lt;title&gt;Atlas Shrugged&lt;/title&gt;
1470 &lt;author&gt;Ayn Rand&lt;/author&gt;
1471 &lt;isbn&gt;0525934189&lt;/isbn&gt;
1472 &lt;/book&gt;
1473 &lt;/books&gt;</programlisting>
1474
1475 <para>Here is an article which describes how PYX format can be used to
1476 grep XML. <link
1477 xlink:href="http://www-106.ibm.com/developerworks/xml/library/x-matters17.html">http://www-106.ibm.com/developerworks/xml/library/x-matters17.html</link></para>
1478 </sect1>
1479
1480 <sect1>
1481 <title>Escape/Unescape special XML characters</title>
1482
1483 <para>Here is synopsis for '<phrase role="PROG"/> esc' command:</para>
1484
1485 <programlisting><phrase role="PROG"/> esc --help
1486 XMLStarlet Toolkit: Escape special XML characters
1487 Usage: <phrase role="PROG"/> esc [&lt;options&gt;] [&lt;string&gt;]
1488 where &lt;options&gt; are
1489 --help - print usage
1490 (TODO: more to be added in future)
1491 if &lt;string&gt; is missing stdin is used instead.
1492
1493 XMLStarlet is a command line toolkit to query/edit/check/transform
1494 XML documents (for more information see http://xmlstar.sourceforge.net/)
1495 </programlisting>
1496
1497 <para>EXAMPLE</para>
1498
1499 <programlisting># Escape special XML characters
1500 cat xml/structure.xml | <phrase role="PROG"/> esc
1501 </programlisting>
1502
1503 <para>Input</para>
1504
1505 <programlisting>&lt;a1&gt;
1506 &lt;a11&gt;
1507 &lt;a111&gt;
1508 &lt;a1111/&gt;
1509 &lt;/a111&gt;
1510 &lt;a112&gt;
1511 &lt;a1121/&gt;
1512 &lt;/a112&gt;
1513 &lt;/a11&gt;
1514 &lt;a12/&gt;
1515 &lt;a13&gt;
1516 &lt;a131/&gt;
1517 &lt;/a13&gt;
1518 &lt;/a1&gt;</programlisting>
1519
1520 <para>Output</para>
1521
1522 <programlisting>&amp;lt;a1&amp;gt;
1523 &amp;lt;a11&amp;gt;
1524 &amp;lt;a111&amp;gt;
1525 &amp;lt;a1111/&amp;gt;
1526 &amp;lt;/a111&amp;gt;
1527 &amp;lt;a112&amp;gt;
1528 &amp;lt;a1121/&amp;gt;
1529 &amp;lt;/a112&amp;gt;
1530 &amp;lt;/a11&amp;gt;
1531 &amp;lt;a12/&amp;gt;
1532 &amp;lt;a13&amp;gt;
1533 &amp;lt;a131/&amp;gt;
1534 &amp;lt;/a13&amp;gt;
1535 &amp;lt;/a1&amp;gt;
1536 </programlisting>
1537 </sect1>
1538
1539 <sect1>
1540 <title>List directory as XML</title>
1541
1542 <para>Here is synopsis for '<phrase role="PROG"/> ls' command:</para>
1543
1544 <programlisting>XMLStarlet Toolkit: List directory as XML
1545 Usage: <phrase role="PROG"/> ls
1546 Lists current directory in XML format.
1547
1548 XMLStarlet is a command line toolkit to query/edit/check/transform
1549 XML documents (for more information see http://xmlstar.sourceforge.net/)
1550 </programlisting>
1551
1552 <para>EXAMPLE</para>
1553
1554 <programlisting><phrase role="PROG"/> ls
1555 </programlisting>
1556
1557 <para>Output</para>
1558
1559 <programlisting>&lt;xml&gt;
1560 &lt;d p="rwxrwxrwx" a="20050107T050740Z" m="20050107T050740Z" s="0" n="old-resume"/&gt;
1561 &lt;f p="rw-rw-rw-" a="20050107T045941Z" m="20050107T045941Z" s="12" n="resume.2old"/&gt;
1562 &lt;f p="rw-rw-rw-" a="20050107T045924Z" m="20050107T045924Z" s="81" n="resume.xml"/&gt;
1563 &lt;/xml&gt;
1564 </programlisting>
1565 </sect1>
1566 </chapter>
1567
1568 <chapter>
1569 <title xml:id="s.5">Common problems</title>
1570
1571 <sect1>
1572 <title xml:id="s.5.1">Namespaces and default namespace</title>
1573
1574 <sect2>
1575 <title xml:id="s.5.1.1">The Problem: Why does nothing match?</title>
1576
1577 <para>You try to extract the links from an XHTML document like
1578 this:
1579
1580 <programlisting><phrase role="PROG"/> sel -t -m "//a" -c . -n page.xhtml</programlisting>
1581
1582 The document contains an <code>&lt;a/&gt;</code> element, but
1583 there are no matches.
1584
1585 <programlisting><![CDATA[<html xmlns="http://www.w3.org/1999/xhtml"><body>
1586 <a href="http://example.com">A link</a>
1587 </body></html>]]></programlisting>
1588 </para>
1589
1590 <para>
1591 The problem is the
1592 <code>xmlns="http://www.w3.org/1999/xhtml"</code> attribute
1593 on the root element, meaning that it, and all elements below
1594 have this url as part of their name.
1595 </para>
1596 </sect2>
1597
1598 <sect2>
1599 <title xml:id="s.5.1.2">The Solution</title>
1600 <para>
1601 To match namespaced elements you must bind the namespace to
1602 a prefix and prepend it to the name:
1603 <programlisting><phrase
1604 role="PROG"/> sel -N x="http://www.w3.org/1999/xhtml" -t -m "//x:a" -c . -n page.xhtml</programlisting>
1605 </para>
1606 </sect2>
1607
1608 <sect2>
1609 <title xml:id="s.5.1.3">A More Convenient Solution</title>
1610 <para>
1611 XML documents can also use different namespace prefixes, on
1612 any element in the document. In order to handle namespaces
1613 with greater ease, XMLStarlet (versions 1.2.1+) will use the
1614 namespace prefixes declared on the root element of the input
1615 document. The default namespace will be bound to the
1616 prefixes "_" and "DEFAULT" (in versions 1.5.0+). So another
1617 way to solve handle the previous example would be:
1618
1619 <programlisting><phrase
1620 role="PROG"/> sel -t -m "//_:a" -c . -n page.xhtml</programlisting>
1621 </para>
1622
1623 <para>
1624 This feature can be disabled (versions 1.6.0+) by the global
1625 <code>--no-doc-namespace</code> option. When should you
1626 disable it? Suppose you are writing a script that handles
1627 XML documents that look like this:
1628
1629 <programlisting><![CDATA[<data xmlns:a="http://example.com">
1630 <a:important-data>...</a:important-data>
1631 </data>]]>
1632 </programlisting>
1633 and also this:
1634 <programlisting><![CDATA[<data xmlns:b="http://example.com">
1635 <b:important-data>...</b:important-data>
1636 </data>]]>
1637 </programlisting>
1638 Since both documents use the same namespace they are
1639 equivalent, even though the prefixes happen to be different.
1640 By using <code>--no-doc-namespace</code> and binding the
1641 namespace with -N, you can be sure that XMLStarlet's
1642 behaviour will be independant of the input document.
1643 </para>
1644 </sect2>
1645
1646 <sect2>
1647 <title>Deleting namespace declarations </title>
1648
1649 <para>Delete namespace declarations and all elements from non default
1650 namespace from the following XML document:</para>
1651
1652 <para>Input (file ns2.xml)<programlisting>&lt;doc xmlns="http://www.a.com/xyz" xmlns:ns="http://www.c.com/xyz"&gt;
1653 &lt;A&gt;test&lt;/A&gt;
1654 &lt;B&gt;
1655 &lt;ns:C&gt;xyz&lt;/ns:C&gt;
1656 &lt;/B&gt;
1657 &lt;/doc&gt;
1658 </programlisting></para>
1659
1660 <para>Command:<programlisting><phrase role="PROG"/> ed -N N="http://www.c.com/xyz" -d '//N:*' ns2.xml | \
1661 sed -e 's/ xmlns.*=".*"//g'</programlisting></para>
1662
1663 <para>Output<programlisting>&lt;doc&gt;
1664 &lt;A&gt;test&lt;/A&gt;
1665 &lt;B/&gt;
1666 &lt;/doc&gt;
1667 </programlisting></para>
1668 </sect2>
1669 </sect1>
1670
1671 <sect1>
1672 <title xml:id="s.5.2">Special characters</title>
1673
1674 <para>Sometimes issues appear with handling of special characters, where
1675 'special' means in XML sence as well as in 'shell' terms. Examples below
1676 should clear at least some of the confusions.</para>
1677
1678 <para>You should not forget about the fact that your command lines are
1679 executed by shell and shell does substitutions of its special characters
1680 too. So for example, one may ask:</para>
1681
1682 <para>"Why does the following query return nothing?"
1683 <programlisting>echo '&lt;X name="foo"&gt;EEE&lt;/X&gt;' | <phrase role="PROG"/> sel -t -m /X[@name='foo'] -v .</programlisting></para>
1684
1685 <para>The answer lies in the way shell substitues 'foo', which simply
1686 becomes foo before the command is run. So the correct way to write that
1687 would be</para>
1688
1689 <para><programlisting>echo '&lt;X name="foo"&gt;EEE&lt;/X&gt;' | <phrase role="PROG"/> sel -t -m "/X[@name='foo']" -v .</programlisting></para>
1690
1691 <para>Another example involves XML special characters. Question: How to
1692 search for &amp;apos; in text nodes?</para>
1693
1694 <para>The following should help<programlisting><phrase role="PROG"/> sel -t -m "//line[contains(text(),&amp;quot;'&amp;quot;)]" -c .
1695 </programlisting></para>
1696 </sect1>
1697
1698 <sect1>
1699 <title xml:id="s.5.3">Sorting</title>
1700
1701 <para>Let's take a look at XSLT produced by the following '<phrase role="PROG"/> sel'
1702 command:</para>
1703
1704 <para><programlisting># Query XML document and produce sorted text table
1705 <phrase role="PROG"/> sel -T -t -m /xml/table/rec -s D:N:- "@id" \
1706 -v "concat(@id,'|',numField,'|',stringField)" -n xml/table.xml
1707 </programlisting></para>
1708
1709 <para><programlisting>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
1710 &lt;xsl:output omit-xml-declaration="yes" indent="no" method="text"/&gt;
1711 &lt;xsl:param name="inputFile"&gt;-&lt;/xsl:param&gt;
1712 &lt;xsl:template match="/"&gt;
1713 &lt;xsl:call-template name="t1"/&gt;
1714 &lt;/xsl:template&gt;
1715 &lt;xsl:template name="t1"&gt;
1716 &lt;xsl:for-each select="/xml/table/rec"&gt;
1717 &lt;xsl:sort order="descending" data-type="number"
1718 case-order="upper-first" select="@id"/&gt;
1719 &lt;xsl:value-of select="concat(@id,'|',numField,'|',stringField)"/&gt;
1720 &lt;xsl:value-of select="'&amp;#10;'"/&gt;
1721 &lt;/xsl:for-each&gt;
1722 &lt;/xsl:template&gt;
1723 &lt;/xsl:stylesheet&gt;
1724 </programlisting></para>
1725
1726 <para>-s option of '<phrase role="PROG"/> sel' command controls 'order', 'data-type', and
1727 'case-order' attributes of &lt;xsl:sort/&gt; element .</para>
1728 </sect1>
1729
1730 <sect1>
1731 <title xml:id="s.5.4">Validation</title>
1732
1733 <para>Many questions are asked about XSD (XML schema) validation. Well,
1734 XmlStarlet relies on libxml2 which has incomplete support for XML
1735 schemas. Untill it is done in libxml2 it will not be in
1736 XmlStarlet.</para>
1737
1738 <para></para>
1739
1740 <para></para>
1741 </sect1>
1742 </chapter>
1743
1744 <chapter>
1745 <title>Other XmlStarlet Resources</title>
1746
1747 <para>Here are few articles on the Internet.</para>
1748
1749 <itemizedlist>
1750 <listitem>
1751 <para><link
1752 xlink:href="http://www.freesoftwaremagazine.com/free_issues/issue_06/xml_starlet/">XMLStarlet:
1753 a Unix toolkit for XML</link></para>
1754 </listitem>
1755
1756 <listitem>
1757 <para><link
1758 xlink:href="http://www-128.ibm.com/developerworks/xml/library/x-starlet.html">Start
1759 working with XMLStarlet</link></para>
1760 </listitem>
1761
1762 <listitem>
1763 <para><link
1764 xlink:href="http://blogicblog.blogspot.com/2004/09/xmlstarlet-gentle-introduction-into.html">XMLStarlet:
1765 A gentle introduction into XSLT </link></para>
1766 </listitem>
1767
1768 <listitem>
1769 <para><link
1770 xlink:href="http://blogs.applibase.net/pramod/index.php/archives/command-line-xml-with-xmlstarlet ">Command
1771 line XML with XMLStarlet </link></para>
1772 </listitem>
1773
1774 <listitem>
1775 <para><link
1776 xlink:href="http://www.pinkjuice.com/howto/vimxml/moresetup.xml#xmlstarlet">Using
1777 vi as XML editor</link></para>
1778 </listitem>
1779 </itemizedlist>
1780 </chapter>
1781 </book>