| 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&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&system=&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"/> [<options>] <command> [<cmd-options>] |
| 312 |
where <command> 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 |
<options> 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"/> <command> --help <ENTER> 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><xml> |
| 350 |
<table> |
| 351 |
<rec id="1"> |
| 352 |
<numField>123</numField> |
| 353 |
<stringField>String Value</stringField> |
| 354 |
</rec> |
| 355 |
<rec id="2"> |
| 356 |
<numField>346</numField> |
| 357 |
<stringField>Text Value</stringField> |
| 358 |
</rec> |
| 359 |
<rec id="3"> |
| 360 |
<numField>-23</numField> |
| 361 |
<stringField>stringValue</stringField> |
| 362 |
</rec> |
| 363 |
</table> |
| 364 |
</xml></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><xml> |
| 571 |
<table> |
| 572 |
<rec id="1"> |
| 573 |
<numField>123</numField> |
| 574 |
<stringField>String Value</stringField> |
| 575 |
</rec> |
| 576 |
<rec id="2"> |
| 577 |
<numField>346</numField> |
| 578 |
<stringField>Text Value</stringField> |
| 579 |
</rec> |
| 580 |
<rec id="3"> |
| 581 |
<numField>-23</numField> |
| 582 |
<stringField>stringValue</stringField> |
| 583 |
</rec> |
| 584 |
</table> |
| 585 |
</xml></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 |
<?xml version="1.0"?> |
| 597 |
<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"> |
| 613 |
<xsl:output omit-xml-declaration="yes" indent="no"/> |
| 614 |
<xsl:param name="inputFile">-</xsl:param> |
| 615 |
<xsl:template match="/"> |
| 616 |
<xsl:call-template name="t1"/> |
| 617 |
</xsl:template> |
| 618 |
<xsl:template name="t1"> |
| 619 |
<xsl:value-of select="count(/xml/table/rec/numField)"/> |
| 620 |
</xsl:template> |
| 621 |
</xsl:stylesheet></programlisting> |
| 622 |
|
| 623 |
<para>Ignoring some XSLT stuff to make it brief:</para> |
| 624 |
|
| 625 |
<programlisting><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
| 626 |
<xsl:output omit-xml-declaration="yes" indent="no"/> |
| 627 |
<xsl:param name="inputFile">-</xsl:param> |
| 628 |
<xsl:template match="/"> |
| 629 |
<xsl:call-template name="t1"/> |
| 630 |
</xsl:template> |
| 631 |
<xsl:template name="t1"> |
| 632 |
<xsl:value-of select="count(/xml/table/rec/numField)"/> |
| 633 |
</xsl:template> |
| 634 |
</xsl:stylesheet></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 <xsl:value-of></para> |
| 642 |
</listitem> |
| 643 |
|
| 644 |
<listitem> |
| 645 |
<para>-c to <xsl:copy-of></para> |
| 646 |
</listitem> |
| 647 |
|
| 648 |
<listitem> |
| 649 |
<para>-e to <xsl:element></para> |
| 650 |
</listitem> |
| 651 |
|
| 652 |
<listitem> |
| 653 |
<para>-a to <xsl:attribute></para> |
| 654 |
</listitem> |
| 655 |
|
| 656 |
<listitem> |
| 657 |
<para>-s to <xsl:sort></para> |
| 658 |
</listitem> |
| 659 |
|
| 660 |
<listitem> |
| 661 |
<para>-m to <xsl:for-each></para> |
| 662 |
</listitem> |
| 663 |
|
| 664 |
<listitem> |
| 665 |
<para>-i to <xsl:if></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 (<xsl:for-each> 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 "<x/>" | <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 "<x/>" | <phrase role="PROG"/> sel -t -m / -e xml -e child -a data -o value</programlisting> |
| 724 |
|
| 725 |
<para>Result Output:</para> |
| 726 |
|
| 727 |
<programlisting><xml><child data="value"/></xml></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><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
| 747 |
<xsl:output omit-xml-declaration="yes" indent="no" method="text"/> |
| 748 |
<xsl:param name="inputFile">-</xsl:param> |
| 749 |
<xsl:template match="/"> |
| 750 |
<xsl:call-template name="t1"/> |
| 751 |
</xsl:template> |
| 752 |
<xsl:template name="t1"> |
| 753 |
<xsl:for-each select="/xml/table/rec"> |
| 754 |
<xsl:sort order="descending" data-type="number" |
| 755 |
case-order="upper-first" select="@id"/> |
| 756 |
<xsl:value-of select="concat(@id,'|',numField,'|',stringField)"/> |
| 757 |
<xsl:value-of select="'&#10;'"/> |
| 758 |
</xsl:for-each> |
| 759 |
</xsl:template> |
| 760 |
</xsl:stylesheet></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 |
<?xml version="1.0"?> |
| 774 |
<?xml-stylesheet type="text/xsl" href="jobserve.xsl"?> |
| 775 |
<xsql:query connection="jobs" xmlns:xsql="urn:oracle-xsql" max-rows="5"> |
| 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 |
</xsql:query></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><a1> |
| 804 |
<a11> |
| 805 |
<a111> |
| 806 |
<a1111/> |
| 807 |
</a111> |
| 808 |
<a112> |
| 809 |
<a1121/> |
| 810 |
</a112> |
| 811 |
</a11> |
| 812 |
<a12/> |
| 813 |
<a13> |
| 814 |
<a131/> |
| 815 |
</a13> |
| 816 |
</a1></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><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
| 834 |
<xsl:output omit-xml-declaration="yes" indent="no" method="text"/> |
| 835 |
<xsl:param name="inputFile">-</xsl:param> |
| 836 |
<xsl:template match="/"> |
| 837 |
<xsl:call-template name="t1"/> |
| 838 |
</xsl:template> |
| 839 |
<xsl:template name="t1"> |
| 840 |
<xsl:for-each select="//*"> |
| 841 |
<xsl:for-each select="ancestor-or-self::*"> |
| 842 |
<xsl:value-of select="name()"/> |
| 843 |
<xsl:if test="not(position()=last())"> |
| 844 |
<xsl:value-of select="'.'"/> |
| 845 |
</xsl:if> |
| 846 |
</xsl:for-each> |
| 847 |
<xsl:value-of select="'&#10;'"/> |
| 848 |
</xsl:for-each> |
| 849 |
</xsl:template> |
| 850 |
</xsl:stylesheet></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(. , '&#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 [<options>] <xsl-file> {-p|-s <name>=<value>} [ <xml-file-or-uri> ... ] |
| 893 |
where |
| 894 |
<xsl-file> - main XSLT stylesheet for transformation |
| 895 |
<xml-file> - input XML document file name (stdin is used if missing) |
| 896 |
<name>=<value> - 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 |
<options> are: |
| 900 |
--omit-decl - omit xml declaration <?xml version="1.0"?> |
| 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><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
| 927 |
<xsl:output method="text"/> |
| 928 |
<xsl:param name="Text"/> |
| 929 |
<xsl:param name="Count"/> |
| 930 |
<xsl:template match="/"> |
| 931 |
<xsl:call-template name="t1"/> |
| 932 |
</xsl:template> |
| 933 |
<xsl:template name="t1"> |
| 934 |
<xsl:for-each select="/xml"> |
| 935 |
<xsl:value-of select="$Text"/> |
| 936 |
<xsl:value-of select="$Count"/> |
| 937 |
<xsl:value-of select="'&#10;'"/> |
| 938 |
</xsl:for-each> |
| 939 |
</xsl:template> |
| 940 |
</xsl:stylesheet></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 <global-options> {<action>} [ <xml-file-or-uri> ... ] |
| 955 |
where |
| 956 |
<global-options> - global options for editing |
| 957 |
<xml-file-or-uri> - input XML document file name/uri (stdin is used if missing) |
| 958 |
|
| 959 |
<global-options> are: |
| 960 |
-P (or --pf) - preserve original formatting |
| 961 |
-S (or --ps) - preserve non-significant spaces |
| 962 |
-O (or --omit-decl) - omit XML declaration (<?xml ...?>) |
| 963 |
-N <name>=<value> - 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 <action> |
| 970 |
-d or --delete <xpath> |
| 971 |
-i or --insert <xpath> -t (--type) elem|text|attr -n <name> -v (--value) <value> |
| 972 |
-a or --append <xpath> -t (--type) elem|text|attr -n <name> -v (--value) <value> |
| 973 |
-s or --subnode <xpath> -t (--type) elem|text|attr -n <name> -v (--value) <value> |
| 974 |
-m or --move <xpath1> <xpath2> |
| 975 |
-r or --rename <xpath1> -v <new-name> |
| 976 |
-u or --update <xpath> -v (--value) <value> |
| 977 |
-x (--expr) <xpath> |
| 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><xml> |
| 992 |
<table> |
| 993 |
<rec id="1"> |
| 994 |
<numField>123</numField> |
| 995 |
<stringField>String Value</stringField> |
| 996 |
</rec> |
| 997 |
<rec id="2"> |
| 998 |
<numField>346</numField> |
| 999 |
<stringField>Text Value</stringField> |
| 1000 |
</rec> |
| 1001 |
<rec id="3"> |
| 1002 |
<numField>-23</numField> |
| 1003 |
<stringField>stringValue</stringField> |
| 1004 |
</rec> |
| 1005 |
</table> |
| 1006 |
</xml> |
| 1007 |
</programlisting> |
| 1008 |
|
| 1009 |
<para>Output</para> |
| 1010 |
|
| 1011 |
<programlisting><xml> |
| 1012 |
<table> |
| 1013 |
<rec id="1"> |
| 1014 |
<numField>123</numField> |
| 1015 |
<stringField>String Value</stringField> |
| 1016 |
</rec> |
| 1017 |
<rec id="3"> |
| 1018 |
<numField>-23</numField> |
| 1019 |
<stringField>stringValue</stringField> |
| 1020 |
</rec> |
| 1021 |
</table> |
| 1022 |
</xml> |
| 1023 |
</programlisting> |
| 1024 |
|
| 1025 |
<para>EXAMPLE</para> |
| 1026 |
|
| 1027 |
<programlisting># Move element node |
| 1028 |
echo '<x id="1"><a/><b/></x>' | <phrase role="PROG"/> ed -m "//b" "//a" |
| 1029 |
</programlisting> |
| 1030 |
|
| 1031 |
<para>Output</para> |
| 1032 |
|
| 1033 |
<programlisting><x id="1"> |
| 1034 |
<a> |
| 1035 |
<b/> |
| 1036 |
</a> |
| 1037 |
</x> |
| 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><xml> |
| 1049 |
<table> |
| 1050 |
<rec ID="1"> |
| 1051 |
<numField>123</numField> |
| 1052 |
<stringField>String Value</stringField> |
| 1053 |
<object name="Obj1"> |
| 1054 |
<property name="size">10</property> |
| 1055 |
<property name="type">Data</property> |
| 1056 |
</object> |
| 1057 |
</rec> |
| 1058 |
<rec ID="2"> |
| 1059 |
<numField>346</numField> |
| 1060 |
<stringField>Text Value</stringField> |
| 1061 |
</rec> |
| 1062 |
<rec ID="3"> |
| 1063 |
<numField>-23</numField> |
| 1064 |
<stringField>stringValue</stringField> |
| 1065 |
</rec> |
| 1066 |
</table> |
| 1067 |
</xml> |
| 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><xml> |
| 1079 |
<table> |
| 1080 |
<record id="1"> |
| 1081 |
<numField>123</numField> |
| 1082 |
<stringField>String Value</stringField> |
| 1083 |
<object name="Obj1"> |
| 1084 |
<property name="size">10</property> |
| 1085 |
<property name="type">Data</property> |
| 1086 |
</object> |
| 1087 |
</record> |
| 1088 |
<record id="2"> |
| 1089 |
<numField>346</numField> |
| 1090 |
<stringField>Text Value</stringField> |
| 1091 |
</record> |
| 1092 |
<record id="3"> |
| 1093 |
<numField>-23</numField> |
| 1094 |
<stringField>stringValue</stringField> |
| 1095 |
</record> |
| 1096 |
</table> |
| 1097 |
</xml> |
| 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><xml> |
| 1109 |
<table> |
| 1110 |
<rec id="1"> |
| 1111 |
<numField>123</numField> |
| 1112 |
<stringField>String Value</stringField> |
| 1113 |
<object name="Obj1"> |
| 1114 |
<property name="size">10</property> |
| 1115 |
<property name="type">Data</property> |
| 1116 |
</object> |
| 1117 |
</rec> |
| 1118 |
<rec id="2"> |
| 1119 |
<numField>346</numField> |
| 1120 |
<stringField>Text Value</stringField> |
| 1121 |
</rec> |
| 1122 |
<rec id="5"> |
| 1123 |
<numField>-23</numField> |
| 1124 |
<stringField>stringValue</stringField> |
| 1125 |
</rec> |
| 1126 |
</table> |
| 1127 |
</xml> |
| 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><xml> |
| 1139 |
<table> |
| 1140 |
<rec id="1"> |
| 1141 |
<numField>0</numField> |
| 1142 |
<stringField>String Value</stringField> |
| 1143 |
<object name="Obj1"> |
| 1144 |
<property name="size">10</property> |
| 1145 |
<property name="type">Data</property> |
| 1146 |
</object> |
| 1147 |
</rec> |
| 1148 |
<rec id="2"> |
| 1149 |
<numField>346</numField> |
| 1150 |
<stringField>Text Value</stringField> |
| 1151 |
</rec> |
| 1152 |
<rec id="3"> |
| 1153 |
<numField>-23</numField> |
| 1154 |
<stringField>stringValue</stringField> |
| 1155 |
</rec> |
| 1156 |
</table> |
| 1157 |
</xml> |
| 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 <options> [ <xml-file-or-uri> ... ] |
| 1168 |
where <options> |
| 1169 |
-w or --well-formed - validate well-formedness only (default) |
| 1170 |
-d or --dtd <dtd-file> - validate against DTD |
| 1171 |
-s or --xsd <xsd-file> - validate against XSD schema |
| 1172 |
-r or --relaxng <rng-file> - 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 >/dev/null 2>&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>/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 [<options>] <xml-file> |
| 1216 |
where <options> are |
| 1217 |
-n or --noindent - do not indent |
| 1218 |
-t or --indent-tab - indent output with tabulation |
| 1219 |
-s or --indent-spaces <num> - indent output with <num> spaces |
| 1220 |
-o or --omit-decl - omit xml declaration <?xml version="1.0"?> |
| 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 <encoding> - 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><xml> |
| 1242 |
<table> |
| 1243 |
<rec id="1"> |
| 1244 |
<numField>123</numField> |
| 1245 |
<stringField>String Value</stringField> |
| 1246 |
<object name="Obj1"> |
| 1247 |
<property name="size">10</property> |
| 1248 |
<property name="type">Data</property> |
| 1249 |
</object> |
| 1250 |
</rec> |
| 1251 |
<rec id="2"> |
| 1252 |
<numField>346</numField> |
| 1253 |
<stringField>Text Value</stringField> |
| 1254 |
</rec> |
| 1255 |
<rec id="3"> |
| 1256 |
<numField>-23</numField> |
| 1257 |
<stringField>stringValue</stringField> |
| 1258 |
</rec> |
| 1259 |
</table> |
| 1260 |
</xml> |
| 1261 |
</programlisting> |
| 1262 |
|
| 1263 |
<para>EXAMPLE</para> |
| 1264 |
|
| 1265 |
<programlisting># Recover malformed XML document |
| 1266 |
<phrase role="PROG"/> fo -R xml/malformed.xml 2>/dev/null |
| 1267 |
</programlisting> |
| 1268 |
|
| 1269 |
<para>Input:</para> |
| 1270 |
|
| 1271 |
<programlisting><test_output> |
| 1272 |
<test_name>foo</testname> |
| 1273 |
<subtest>...</subtest> |
| 1274 |
</test_output> |
| 1275 |
</programlisting> |
| 1276 |
|
| 1277 |
<para>Output:</para> |
| 1278 |
|
| 1279 |
<programlisting><test_output> |
| 1280 |
<test_name>foo</test_name> |
| 1281 |
<subtest>...</subtest> |
| 1282 |
</test_output> |
| 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 <mode> <xml-file> [<xpath-file>] [<inclusive-ns-list>] |
| 1293 |
where |
| 1294 |
<xml-file> - input XML document file name (stdin is used if '-') |
| 1295 |
<xpath-file> - XML file containing XPath expression for |
| 1296 |
c14n XML canonicalization |
| 1297 |
Example: |
| 1298 |
<?xml version="1.0"?> |
| 1299 |
<XPath xmlns:n0="http://a.example.com" xmlns:n1="http://b.example"> |
| 1300 |
(//. | //@* | //namespace::*)[ancestor-or-self::n1:elem1] |
| 1301 |
</XPath> |
| 1302 |
|
| 1303 |
<inclusive-ns-list> - the list of inclusive namespace prefixes |
| 1304 |
(only for exclusive canonicalization) |
| 1305 |
Example: 'n1 n2' |
| 1306 |
|
| 1307 |
<mode> 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><a1> |
| 1326 |
<a11> |
| 1327 |
<a111> |
| 1328 |
<a1111/> |
| 1329 |
</a111> |
| 1330 |
<a112> |
| 1331 |
<a1121/> |
| 1332 |
</a112> |
| 1333 |
</a11> |
| 1334 |
<a12/> |
| 1335 |
<a13> |
| 1336 |
<a131/> |
| 1337 |
</a13> |
| 1338 |
</a1></programlisting> |
| 1339 |
|
| 1340 |
<para>Output</para> |
| 1341 |
|
| 1342 |
<programlisting><a1> |
| 1343 |
<a11> |
| 1344 |
<a111> |
| 1345 |
<a1111></a1111> |
| 1346 |
</a111> |
| 1347 |
<a112> |
| 1348 |
<a1121></a1121> |
| 1349 |
</a112> |
| 1350 |
</a11> |
| 1351 |
<a12></a12> |
| 1352 |
<a13> |
| 1353 |
<a131></a131> |
| 1354 |
</a13> |
| 1355 |
</a1> |
| 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 |
<n0:pdu xmlns:n0='http://a.example.com'> |
| 1370 |
<n1:elem1 xmlns:n1='http://b.example'> |
| 1371 |
content |
| 1372 |
</n1:elem1> |
| 1373 |
</n0:pdu> |
| 1374 |
|
| 1375 |
../examples/xml/c14n.xpath |
| 1376 |
|
| 1377 |
<XPath xmlns:n0="http://a.example.com" xmlns:n1="http://b.example"> |
| 1378 |
(//. | //@* | //namespace::*)[ancestor-or-self::n1:elem1] |
| 1379 |
</XPath> |
| 1380 |
|
| 1381 |
</programlisting> |
| 1382 |
|
| 1383 |
<para>Output</para> |
| 1384 |
|
| 1385 |
<programlisting><n1:elem1 xmlns:n1="http://b.example"> |
| 1386 |
content |
| 1387 |
</n1:elem1> |
| 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 {<xml-file>} |
| 1398 |
where |
| 1399 |
<xml-file> - 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><books> |
| 1422 |
<book type='hardback'> |
| 1423 |
<title>Atlas Shrugged</title> |
| 1424 |
<author>Ayn Rand</author> |
| 1425 |
<isbn id='1'>0525934189</isbn> |
| 1426 |
</book> |
| 1427 |
</books></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><books> |
| 1468 |
<book> |
| 1469 |
<title>Atlas Shrugged</title> |
| 1470 |
<author>Ayn Rand</author> |
| 1471 |
<isbn>0525934189</isbn> |
| 1472 |
</book> |
| 1473 |
</books></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 [<options>] [<string>] |
| 1488 |
where <options> are |
| 1489 |
--help - print usage |
| 1490 |
(TODO: more to be added in future) |
| 1491 |
if <string> 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><a1> |
| 1506 |
<a11> |
| 1507 |
<a111> |
| 1508 |
<a1111/> |
| 1509 |
</a111> |
| 1510 |
<a112> |
| 1511 |
<a1121/> |
| 1512 |
</a112> |
| 1513 |
</a11> |
| 1514 |
<a12/> |
| 1515 |
<a13> |
| 1516 |
<a131/> |
| 1517 |
</a13> |
| 1518 |
</a1></programlisting> |
| 1519 |
|
| 1520 |
<para>Output</para> |
| 1521 |
|
| 1522 |
<programlisting>&lt;a1&gt; |
| 1523 |
&lt;a11&gt; |
| 1524 |
&lt;a111&gt; |
| 1525 |
&lt;a1111/&gt; |
| 1526 |
&lt;/a111&gt; |
| 1527 |
&lt;a112&gt; |
| 1528 |
&lt;a1121/&gt; |
| 1529 |
&lt;/a112&gt; |
| 1530 |
&lt;/a11&gt; |
| 1531 |
&lt;a12/&gt; |
| 1532 |
&lt;a13&gt; |
| 1533 |
&lt;a131/&gt; |
| 1534 |
&lt;/a13&gt; |
| 1535 |
&lt;/a1&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><xml> |
| 1560 |
<d p="rwxrwxrwx" a="20050107T050740Z" m="20050107T050740Z" s="0" n="old-resume"/> |
| 1561 |
<f p="rw-rw-rw-" a="20050107T045941Z" m="20050107T045941Z" s="12" n="resume.2old"/> |
| 1562 |
<f p="rw-rw-rw-" a="20050107T045924Z" m="20050107T045924Z" s="81" n="resume.xml"/> |
| 1563 |
</xml> |
| 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><a/></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><doc xmlns="http://www.a.com/xyz" xmlns:ns="http://www.c.com/xyz"> |
| 1653 |
<A>test</A> |
| 1654 |
<B> |
| 1655 |
<ns:C>xyz</ns:C> |
| 1656 |
</B> |
| 1657 |
</doc> |
| 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><doc> |
| 1664 |
<A>test</A> |
| 1665 |
<B/> |
| 1666 |
</doc> |
| 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 '<X name="foo">EEE</X>' | <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 '<X name="foo">EEE</X>' | <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 &apos; in text nodes?</para> |
| 1693 |
|
| 1694 |
<para>The following should help<programlisting><phrase role="PROG"/> sel -t -m "//line[contains(text(),&quot;'&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><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
| 1710 |
<xsl:output omit-xml-declaration="yes" indent="no" method="text"/> |
| 1711 |
<xsl:param name="inputFile">-</xsl:param> |
| 1712 |
<xsl:template match="/"> |
| 1713 |
<xsl:call-template name="t1"/> |
| 1714 |
</xsl:template> |
| 1715 |
<xsl:template name="t1"> |
| 1716 |
<xsl:for-each select="/xml/table/rec"> |
| 1717 |
<xsl:sort order="descending" data-type="number" |
| 1718 |
case-order="upper-first" select="@id"/> |
| 1719 |
<xsl:value-of select="concat(@id,'|',numField,'|',stringField)"/> |
| 1720 |
<xsl:value-of select="'&#10;'"/> |
| 1721 |
</xsl:for-each> |
| 1722 |
</xsl:template> |
| 1723 |
</xsl:stylesheet> |
| 1724 |
</programlisting></para> |
| 1725 |
|
| 1726 |
<para>-s option of '<phrase role="PROG"/> sel' command controls 'order', 'data-type', and |
| 1727 |
'case-order' attributes of <xsl:sort/> 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> |