1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
|
XMLSTARLET USER'S GUIDE
see also http://xmlstar.sourceforge.net/
1. BASIC COMMAND LINE OPTIONS
====================================================
xml --help
XMLStarlet Toolkit: Command line utilities for XML
Usage: xml [<options>] <command> [<cmd-options>]
where <command> is one of:
ed (or edit) - Edit/Update XML document(s)
sel (or select) - Select data or query XML document(s) (XPATH, etc)
tr (or transform) - Transform XML document(s) using XSLT
val (or validate) - Validate XML document(s) (well-formed/DTD/XSD/RelaxNG)
fo (or format) - Format XML document(s)
el (or elements) - Display element structure of XML document
c14n (or canonic) - XML canonicalization
ls (or list) - List directory as XML
esc (or escape) - Escape special XML characters
unesc (or unescape) - Unescape special XML characters
pyx (or xmln) - Convert XML into PYX format (based on ESIS - ISO 8879)
p2x (or depyx) - Convert PYX into XML
<options> are:
-q or --quiet - no error output
--doc-namespace - extract namespace bindings from input doc (default)
--no-doc-namespace - don't extract namespace bindings from input doc
--version - show version
--help - show help
Wherever file name mentioned in command help it is assumed
that URL can be used instead as well.
Type: xml <command> --help <ENTER> for command help
2. Select/Query XML documents
====================================================
xml sel --help
XMLStarlet Toolkit: Select from XML document(s)
Usage: xml sel <global-options> {<template>} [ <xml-file> ... ]
where
<global-options> - global options for selecting
<xml-file> - input XML document file name/uri (stdin is used if missing)
<template> - template for querying XML document with following syntax:
<global-options> are:
-Q or --quiet - do not write anything to standard output.
-C or --comp - display generated XSLT
-R or --root - print root element <xsl-select>
-T or --text - output is text (default is XML)
-I or --indent - indent output
-D or --xml-decl - do not omit xml declaration line
-B or --noblanks - remove insignificant spaces from XML tree
-E or --encode <encoding> - output in the given encoding (utf-8, unicode...)
-N <name>=<value> - predefine namespaces (name without 'xmlns:')
ex: xsql=urn:oracle-xsql
Multiple -N options are allowed.
--net - allow fetch DTDs or entities over network
--help - display help
Syntax for templates: -t|--template <options>
where <options>
-c or --copy-of <xpath> - print copy of XPATH expression
-v or --value-of <xpath> - print value of XPATH expression
-o or --output <string> - output string literal
-n or --nl - print new line
-f or --inp-name - print input file name (or URL)
-m or --match <xpath> - match XPATH expression
--var <name> <value> --break or
--var <name>=<value> - declare a variable (referenced by $name)
-i or --if <test-xpath> - check condition <xsl:if test="test-xpath">
--elif <test-xpath> - check condition if previous conditions failed
--else - check if previous conditions failed
-e or --elem <name> - print out element <xsl:element name="name">
-a or --attr <name> - add attribute <xsl:attribute name="name">
-b or --break - break nesting
-s or --sort op xpath - sort in order (used after -m) where
op is X:Y:Z,
X is A - for order="ascending"
X is D - for order="descending"
Y is N - for data-type="numeric"
Y is T - for data-type="text"
Z is U - for case-order="upper-first"
Z is L - for case-order="lower-first"
There can be multiple --match, --copy-of, --value-of, etc options
in a single template. The effect of applying command line templates
can be illustrated with the following XSLT analogue
xml sel -t -c "xpath0" -m "xpath1" -m "xpath2" -v "xpath3" \
-t -m "xpath4" -c "xpath5"
is equivalent to applying the following XSLT
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:call-template name="t1"/>
<xsl:call-template name="t2"/>
</xsl:template>
<xsl:template name="t1">
<xsl:copy-of select="xpath0"/>
<xsl:for-each select="xpath1">
<xsl:for-each select="xpath2">
<xsl:value-of select="xpath3"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
<xsl:template name="t2">
<xsl:for-each select="xpath4">
<xsl:copy-of select="xpath5"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
3. Editing XML documents
====================================================
xml ed --help
XMLStarlet Toolkit: Edit XML document(s)
Usage: xml ed <global-options> {<action>} [ <xml-file-or-uri> ... ]
where
<global-options> - global options for editing
<xml-file-or-uri> - input XML document file name/uri (stdin otherwise)
<global-options> are:
-P, or -S - preserve whitespace nodes.
(or --pf, --ps) Note that space between attributes is not preserved
-O (or --omit-decl) - omit XML declaration (<?xml ...?>)
-L (or --inplace) - edit file inplace
-N <name>=<value> - predefine namespaces (name without 'xmlns:')
ex: xsql=urn:oracle-xsql
Multiple -N options are allowed.
-N options must be last global options.
--net - allow network access
--help or -h - display help
where <action>
-d or --delete <xpath>
--var <name> <xpath>
-i or --insert <xpath> -t (--type) elem|text|attr -n <name> [-v (--value) <value>]
-a or --append <xpath> -t (--type) elem|text|attr -n <name> [-v (--value) <value>]
-s or --subnode <xpath> -t (--type) elem|text|attr -n <name> [-v (--value) <value>]
-m or --move <xpath1> <xpath2>
-r or --rename <xpath1> -v <new-name>
-u or --update <xpath> -v (--value) <value>
-x (--expr) <xpath>
4. Using XSLT to transform XML documents
====================================================
xml tr --help
XMLStarlet Toolkit: Transform XML document(s) using XSLT
Usage: xml tr [<options>] <xsl-file> {-p|-s <name>=<value>} [<xml-file>...]
where
<xsl-file> - main XSLT stylesheet for transformation
<xml-file> - input XML document file/URL (stdin is used if missing)
<name>=<value> - name and value of the parameter passed to XSLT processor
-p - parameter is XPATH expression ("'string'" to quote string)
-s - parameter is a string literal
<options> are:
--help or -h - display help message
--omit-decl - omit xml declaration <?xml version="1.0"?>
--embed or -E - allow applying embedded stylesheet
--show-ext - show list of extensions
--val - allow validate against DTDs or schemas
--net - allow fetch DTDs or entities over network
--xinclude - do XInclude processing on document input
--maxdepth val - increase the maximum depth
--html - input document(s) is(are) in HTML format
5. Formatting XML documents
====================================================
xml fo --help
XMLStarlet Toolkit: Format XML document
Usage: xml fo [<options>] <xml-file>
where <options> are
-n or --noindent - do not indent
-t or --indent-tab - indent output with tabulation
-s or --indent-spaces <num> - indent output with <num> spaces
-o or --omit-decl - omit xml declaration <?xml version="1.0"?>
--net - allow network access
-R or --recover - try to recover what is parsable
-D or --dropdtd - remove the DOCTYPE of the input docs
-C or --nocdata - replace cdata section with text nodes
-N or --nsclean - remove redundant namespace declarations
-e or --encode <encoding> - output in the given encoding (utf-8, unicode...)
-H or --html - input is HTML
-h or --help - print help
6. Validating XML documents
====================================================
xml val --help
XMLStarlet Toolkit: Validate XML document(s)
Usage: xml val <options> [ <xml-file-or-uri> ... ]
where <options>
-w or --well-formed - validate well-formedness only (default)
-d or --dtd <dtd-file> - validate against DTD
--net - allow network access
-s or --xsd <xsd-file> - validate against XSD schema
-E or --embed - validate using embedded DTD
-r or --relaxng <rng-file> - validate against Relax-NG schema
-e or --err - print verbose error messages on stderr
-S or --stop - stop on first error
-b or --list-bad - list only files which do not validate
-g or --list-good - list only files which validate
-q or --quiet - do not list files (return result code only)
NOTE: XML Schemas are not fully supported yet due to its incomplete
support in libxml2 (see http://xmlsoft.org)
7. Displaying structure of XML documents
====================================================
xml el --help
XMLStarlet Toolkit: Display element structure of XML document
Usage: xml el [<options>] <xml-file>
where
<xml-file> - input XML document file name (stdin is used if missing)
<options> is one of:
-a - show attributes as well
-v - show attributes and their values
-u - print out sorted unique lines
-d<n> - print out sorted unique lines up to depth <n>
8. Escape/Unescape special XML characters
====================================================
xml esc --help
XMLStarlet Toolkit: Escape special XML characters
Usage: xml esc [<options>] [<string>]
where <options> are
--help - print usage
(TODO: more to be added in future)
if <string> is missing stdin is used instead.
9. List directory as XML
====================================================
xml ls --help
XMLStarlet Toolkit: List directory as XML
Usage: xml ls [ <dir> | --help ]
Lists current directory in XML format.
Time is shown per ISO 8601 spec.
10. XML canonicalization
====================================================
xml c14n --help
XMLStarlet Toolkit: XML canonicalization
Usage: xml c14n [--net] <mode> <xml-file> [<xpath-file>] [<inclusive-ns-list>]
where
<xml-file> - input XML document file name (stdin is used if '-')
<xpath-file> - XML file containing XPath expression for
c14n XML canonicalization
Example:
<?xml version="1.0"?>
<XPath xmlns:n0="http://a.example.com" xmlns:n1="http://b.example">
(//. | //@* | //namespace::*)[ancestor-or-self::n1:elem1]
</XPath>
<inclusive-ns-list> - the list of inclusive namespace prefixes
(only for exclusive canonicalization)
Example: 'n1 n2'
<mode> is one of following:
--with-comments XML file canonicalization w comments (default)
--without-comments XML file canonicalization w/o comments
--exc-with-comments Exclusive XML file canonicalization w comments
--exc-without-comments Exclusive XML file canonicalization w/o comments
11. Convert XML into PYX format (based on ESIS - ISO 8879)
====================================================
xml pyx --help
XMLStarlet Toolkit: Convert XML into PYX format (based on ESIS - ISO 8879)
Usage: xml pyx {<xml-file>}
where
<xml-file> - input XML document file name (stdin is used if missing)
The PYX format is a line-oriented representation of
XML documents that is derived from the SGML ESIS format.
(see ESIS - ISO 8879 Element Structure Information Set spec,
ISO/IEC JTC1/SC18/WG8 N931 (ESIS))
A non-validating, ESIS generating tool originally developed for
pyxie project (see http://pyxie.sourceforge.net/)
ESIS Generation by Sean Mc Grath http://www.digitome.com/sean.html
12. Examples:
====================================================
Input1
examples/xml/table.xml
<?xml version="1.0"?>
<xml>
<table>
<rec id="1">
<numField>123</numField>
<stringField>String Value</stringField>
</rec>
<rec id="2">
<numField>346</numField>
<stringField>Text Value</stringField>
</rec>
<rec id="3">
<numField>-23</numField>
<stringField>stringValue</stringField>
</rec>
</table>
</xml>
Input2
examples/xml/tab-obj.xml
<?xml version="1.0"?>
<xml>
<table>
<rec id="1">
<numField>123</numField>
<stringField>String Value</stringField>
<object name="Obj1">
<property name="size">10</property>
<property name="type">Data</property>
</object>
</rec>
<rec id="2">
<numField>346</numField>
<stringField>Text Value</stringField>
</rec>
<rec id="3">
<numField>-23</numField>
<stringField>stringValue</stringField>
</rec>
</table>
</xml>
Input3
examples/html/hello1.html
<html>
<head>
<title>Hello World</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<div align="center">Hello World!<br></div>
</body>
</html>
Input4
examples/sgml/docbook1.sgml
<!DOCTYPE book
PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
"http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd">
<book>
<bookinfo>
<title>DocBook document example</title>
<author>
<firstname>Mikhail</firstname>
<surname>Grushinskiy</surname>
</author>
<copyright>
<year>2002</year>
<holder>Mikhail Grushinskiy</holder>
</copyright>
</bookinfo>
<preface>
<title>Sample document</title>
<para>A simple DocBook example document.</para>
</preface>
<chapter>
<title>XMLStarlet Example</title>
<para>The <emphasis>XMLStarlet</emphasis> command line toolkit
allows querying/checking/editing/transforming/formatting XML documents
from command line</para>
<para>To find out more on how to use the
<emphasis>XMLStarlet</emphasis> for XML processing, point
your browser to <ulink
url="http://xmlstar.sourceforge.net/">http://xmlstar.sourceforge.net/</ulink>.
</para>
</chapter>
</book>
Stylesheet1
examples/xsl/sum1.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:param name="inputFile">-</xsl:param>
<xsl:template match="/">
<xsl:call-template name="t1"/>
</xsl:template>
<xsl:template name="t1">
<xsl:value-of select="sum(/xml/table/rec/numField)"/>
<xsl:value-of select="' '"/>
</xsl:template>
</xsl:stylesheet>
Stylesheet2
examples/xsl/hello1.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:param name="inputFile">-</xsl:param>
<xsl:template match="/">
<xsl:call-template name="t1"/>
</xsl:template>
<xsl:template name="t1">
<xsl:for-each select="/">
<xsl:value-of select="/html/body/div"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Stylesheet3
examples/xsl/param1.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:param name="Text"/>
<xsl:param name="Count"/>
<xsl:template match="/">
<xsl:call-template name="t1"/>
</xsl:template>
<xsl:template name="t1">
<xsl:for-each select="/xml">
<xsl:value-of select="$Text"/>
<xsl:value-of select="$Count"/>
<xsl:value-of select="' '"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Command:
# XML canonicalization
xml c14n --with-comments ../examples/xml/structure.xml ; echo $?
Result Output:
<a1>
<a11>
<a111>
<a1111></a1111>
</a111>
<a112>
<a1121></a1121>
</a112>
</a11>
<a12></a12>
<a13>
<a131></a131>
</a13>
</a1>0
Command:
# XML exclusive canonicalization
xml c14n --exc-with-comments ../examples/xml/c14n.xml ../examples/xml/c14n.xpath
Result Output:
<n1:elem1 xmlns:n1="http://b.example">
content
</n1:elem1>
Command:
# Count elements matching XPath expression
xml sel -T -t -v "count(/xml/table/rec/numField)" xml/table.xml
Result Output:
3
Command:
# Count all nodes in XML document
xml sel -T -t -f -o " " -v "count(//node())" --nl xml/table.xml xml/tab-obj.xml
Result Output:
xml/table.xml 32
xml/tab-obj.xml 41
Command:
# Delete elements matching XPath expression
xml ed -d '/xml/table/rec[@id="2"]' xml/table.xml
Result Output:
<?xml version="1.0"?>
<xml>
<table>
<rec id="1">
<numField>123</numField>
<stringField>String Value</stringField>
</rec>
<rec id="3">
<numField>-23</numField>
<stringField>stringValue</stringField>
</rec>
</table>
</xml>
Command:
# Generate HTML from given SGML docbook document
xml tr --omit-decl --docbook /usr/share/sgml/docbook/yelp/docbook/html/docbook.xsl sgml/docbook1.sgml | \
xml fo --html --indent-spaces 2
Result Output:
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"/>
<title>DocBook document example</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.48"/>
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="book">
<div class="titlepage">
<div>
<h1 class="title"><a name="id2765244"/>DocBook document example</h1>
</div>
<div>
<h3 class="author">Mikhail Grushinskiy</h3>
</div>
<div>
<p class="copyright">Copyright 2002 Mikhail Grushinskiy</p>
</div>
<hr/>
</div>
<div class="toc">
<p>
<b>Table of Contents</b>
</p>
<dl>
<dt>
<a href="#id2765482">Sample document</a>
</dt>
<dt>1. <a href="#id2767329">XMLStarlet Example</a></dt>
</dl>
</div>
<div class="preface">
<div class="titlepage">
<div>
<h2 class="title"><a name="id2765482"/>Sample document</h2>
</div>
</div>
<p>A simple DocBook example document.</p>
</div>
<div class="chapter">
<div class="titlepage">
<div>
<h2 class="title"><a name="id2767329"/>Chapter 1. XMLStarlet Example</h2>
</div>
</div>
<p>The <span class="emphasis"><i>XMLStarlet</i></span> command line toolkit
allows querying/checking/editing/transforming/formatting XML documents
from command line</p>
<p>To find out more on how to use the
<span class="emphasis"><i>XMLStarlet</i></span> for XML processing, point
your browser to <a href="http://xmlstar.sourceforge.net/" target="_top">http://xmlstar.sourceforge.net/</a>.
</p>
</div>
</div>
</body>
</html>
Command:
# Validate XML document against DTD
xml val --dtd dtd/table.dtd xml/tab-obj.xml >/dev/null 2>&1; echo $?
Result Output:
1
Command:
# Validate XML document against DTD
xml val --dtd dtd/table.dtd xml/table.xml >/dev/null 2>&1; echo $?
Result Output:
0
Command:
# Validate XML document against DTD
xml val --dtd xml/foo.dtd xml/foo.xml 2>/dev/null
Result Output:
xml/foo.xml - invalid
Command:
# make sure we don't look for embedded dtd if not asked
xml val --dtd dtd/table.dtd xml/table.xml
Result Output:
xml/table.xml - valid
Command:
xml ed \
-s /xml/table/rec -t elem -n new-field -v new-value \
-i '$prev' -t attr -n new-attr -v new-attr-value \
xml/table.xml
Result Output:
<?xml version="1.0"?>
<xml>
<table>
<rec id="1">
<numField>123</numField>
<stringField>String Value</stringField>
<new-field new-attr="new-attr-value">new-value</new-field>
</rec>
<rec id="2">
<numField>346</numField>
<stringField>Text Value</stringField>
<new-field new-attr="new-attr-value">new-value</new-field>
</rec>
<rec id="3">
<numField>-23</numField>
<stringField>stringValue</stringField>
<new-field new-attr="new-attr-value">new-value</new-field>
</rec>
</table>
</xml>
Command:
xml ed \
-s /xml/table/rec -t elem -n new-field -v new-value \
--var new-field '$prev' \
-i '$new-field' -t attr -n new-attr -v new-attr-value \
-s '$new-field' -t elem -n new-subelem -v '' \
xml/table.xml | xml c14n
Result Output:
<xml>
<table>
<rec id="1">
<numField>123</numField>
<stringField>String Value</stringField>
<new-field new-attr="new-attr-value">new-value<new-subelem></new-subelem></new-field>
</rec>
<rec id="2">
<numField>346</numField>
<stringField>Text Value</stringField>
<new-field new-attr="new-attr-value">new-value<new-subelem></new-subelem></new-field>
</rec>
<rec id="3">
<numField>-23</numField>
<stringField>stringValue</stringField>
<new-field new-attr="new-attr-value">new-value<new-subelem></new-subelem></new-field>
</rec>
</table>
</xml>
Command:
# Display element structure of XML document
xml el ./xml/tab-obj.xml
Result Output:
xml
xml/table
xml/table/rec
xml/table/rec/numField
xml/table/rec/stringField
xml/table/rec/object
xml/table/rec/object/property
xml/table/rec/object/property
xml/table/rec
xml/table/rec/numField
xml/table/rec/stringField
xml/table/rec
xml/table/rec/numField
xml/table/rec/stringField
Command:
# Display element structure of XML document (including attributes)
xml el -a ./xml/tab-obj.xml
Result Output:
xml
xml/table
xml/table/rec
xml/table/rec/@id
xml/table/rec/numField
xml/table/rec/stringField
xml/table/rec/object
xml/table/rec/object/@name
xml/table/rec/object/property
xml/table/rec/object/property/@name
xml/table/rec/object/property
xml/table/rec/object/property/@name
xml/table/rec
xml/table/rec/@id
xml/table/rec/numField
xml/table/rec/stringField
xml/table/rec
xml/table/rec/@id
xml/table/rec/numField
xml/table/rec/stringField
Command:
# Display element structure of XML document (including attribute values)
xml el -v ./xml/tab-obj.xml
Result Output:
xml
xml/table
xml/table/rec[@id='1']
xml/table/rec/numField
xml/table/rec/stringField
xml/table/rec/object[@name='Obj1']
xml/table/rec/object/property[@name='size']
xml/table/rec/object/property[@name='type']
xml/table/rec[@id='2']
xml/table/rec/numField
xml/table/rec/stringField
xml/table/rec[@id='3']
xml/table/rec/numField
xml/table/rec/stringField
Command:
# Escape special XML characters
cat xml/structure.xml | xml esc
Result Output:
<a1>
<a11>
<a111>
<a1111/>
</a111>
<a112>
<a1121/>
</a112>
</a11>
<a12/>
<a13>
<a131/>
</a13>
</a1>
Command:
# Calculate EXSLT (XSLT extentions) XPath value
echo "<x/>" | xml sel -T -t -v "math:abs(-1000)" --nl
Result Output:
1000
Command:
# Find XML files matching XPath expression (containing 'object' element)
xml sel -T -t -m //object -f --nl xml/table.xml xml/tab-obj.xml
Result Output:
xml/tab-obj.xml
Command:
# Generate XML document using command line xml sel
echo "<x/>" | xml sel -t -m / -e xml -e child -a data -o value | xml c14n
echo
Result Output:
<xml><child data="value"></child></xml>
Command:
# Apply XSLT stylesheet to HTML input file
xml tr --html xsl/hello1.xsl html/hello1.html
Result Output:
Hello World!
Command:
# Use local-name() XSLT function in XPath expression
xml sel -T -t -v "//*[local-name()='query']" --nl xsql/jobserve.xsql
Result Output:
SELECT substr(title,1,26) short_title, title, location, skills
FROM job
WHERE UPPER(title) LIKE '%ORACLE%'
ORDER BY first_posted DESC
Command:
# Select text value of an XML element mathing given XPath expression
xml sel -T -t -m "/xml/table/rec[@id='2']" -v numField --nl xml/table.xml
Result Output:
346
Command:
# Move element node
echo '<x id="1"><a/><b/></x>' | xml ed -m "//b" "//a"
Result Output:
<?xml version="1.0"?>
<x id="1">
<a>
<b/>
</a>
</x>
Command:
# Format XML document disabling indent
cat xml/tab-obj.xml | xml fo --noindent
Result Output:
<?xml version="1.0"?>
<xml>
<table>
<rec id="1">
<numField>123</numField>
<stringField>String Value</stringField>
<object name="Obj1">
<property name="size">10</property>
<property name="type">Data</property>
</object>
</rec>
<rec id="2">
<numField>346</numField>
<stringField>Text Value</stringField>
</rec>
<rec id="3">
<numField>-23</numField>
<stringField>stringValue</stringField>
</rec>
</table>
</xml>
Command:
# Predefine namespaces for XPath expressions
xml sel -T -N xsql=urn:oracle-xsql -t -v /xsql:query --nl xsql/jobserve.xsql
Result Output:
SELECT substr(title,1,26) short_title, title, location, skills
FROM job
WHERE UPPER(title) LIKE '%ORACLE%'
ORDER BY first_posted DESC
Command:
# Recover malformed XML document
xml fo -R xml/malformed.xml 2>/dev/null
Result Output:
<?xml version="1.0"?>
<test_output>
<test_name>foo</test_name>
<subtest>...</subtest>
</test_output>
Command:
# Rename attributes
xml ed -r "//*/@id" -v ID xml/tab-obj.xml
Result Output:
<?xml version="1.0"?>
<xml>
<table>
<rec ID="1">
<numField>123</numField>
<stringField>String Value</stringField>
<object name="Obj1">
<property name="size">10</property>
<property name="type">Data</property>
</object>
</rec>
<rec ID="2">
<numField>346</numField>
<stringField>Text Value</stringField>
</rec>
<rec ID="3">
<numField>-23</numField>
<stringField>stringValue</stringField>
</rec>
</table>
</xml>
Command:
# Rename elements
xml ed -r "/xml/table/rec" -v record xml/tab-obj.xml
Result Output:
<?xml version="1.0"?>
<xml>
<table>
<record id="1">
<numField>123</numField>
<stringField>String Value</stringField>
<object name="Obj1">
<property name="size">10</property>
<property name="type">Data</property>
</object>
</record>
<record id="2">
<numField>346</numField>
<stringField>Text Value</stringField>
</record>
<record id="3">
<numField>-23</numField>
<stringField>stringValue</stringField>
</record>
</table>
</xml>
Command:
# Validate against XSD schema
xml val -b -s xsd/table.xsd xml/table.xml xml/tab-obj.xml 2>/dev/null; echo $?
Result Output:
xml/tab-obj.xml
1
Command:
# xsl:copy-of in xml sel command
xml sel -B -t -e ROOT -m /xml/table/rec -c . -n xml/table.xml | xml c14n
Result Output:
<ROOT><rec id="1"><numField>123</numField><stringField>String Value</stringField></rec>
<rec id="2"><numField>346</numField><stringField>Text Value</stringField></rec>
<rec id="3"><numField>-23</numField><stringField>stringValue</stringField></rec>
</ROOT>
Command:
# Query XML document and produce sorted text table
xml sel -T -t -m /xml/table/rec -s D:N:- "@id" -v "concat(@id,'|',numField,'|',stringField)" -n xml/table.xml
Result Output:
3|-23|stringValue
2|346|Text Value
1|123|String Value
Command:
# Sort on two fields
xml sel -T -t -m /root/elem -s A:N:U @rank -s A:T:U @name -v @name -n xml/unsorted.xml
Result Output:
A
a
E
e
B
b
C
c
D
d
Command:
# lower case first
xml sel -T -t -m /root/elem -s A:T:L @name -v @name -n xml/unsorted.xml
Result Output:
a
A
b
B
c
C
d
D
e
E
Command:
# Print structure of XML element using xml sel (advanced XPath expressions and xml sel command usage)
xml sel -T -t -m '//*' \
-m 'ancestor-or-self::*' -v 'name()' -i 'not(position()=last())' -o . -b -b -n \
xml/structure.xml
Result Output:
a1
a1.a11
a1.a11.a111
a1.a11.a111.a1111
a1.a11.a112
a1.a11.a112.a1121
a1.a12
a1.a13
a1.a13.a131
Command:
# Calculating running sum on XML document
xml sel -T -t -v "sum(/xml/table/rec/numField)" --nl xml/table.xml
Result Output:
446
Command:
# Indent XML document with tabs
cat xml/tab-obj.xml | xml fo --indent-tab
Result Output:
<?xml version="1.0"?>
<xml>
<table>
<rec id="1">
<numField>123</numField>
<stringField>String Value</stringField>
<object name="Obj1">
<property name="size">10</property>
<property name="type">Data</property>
</object>
</rec>
<rec id="2">
<numField>346</numField>
<stringField>Text Value</stringField>
</rec>
<rec id="3">
<numField>-23</numField>
<stringField>stringValue</stringField>
</rec>
</table>
</xml>
Command:
# Generate plain text table from XML document
xml sel -T -t -m /xml/table/rec -v "@id" -o "|" -v numField -o "|" -v stringField -n xml/table.xml
Result Output:
1|123|String Value
2|346|Text Value
3|-23|stringValue
Command:
# Generate plain text table from XML document
xml sel -T -t -m /xml/table/rec -v "concat(@id,'|',numField,'|',stringField)" -n xml/table.xml
Result Output:
1|123|String Value
2|346|Text Value
3|-23|stringValue
Command:
# Generate plain text table from XML document
xml sel -T \
-t -o "===================" -n \
-m xml/table/rec -v "concat(@id,'|',numField,'|',stringField)" -n \
-t -o "===================" -n xml/table.xml
Result Output:
===================
1|123|String Value
2|346|Text Value
3|-23|stringValue
===================
Command:
# Print subtree of elements having given element as a descendant (advanced XPath)
xml ed -d '//node()[not(descendant-or-self::isbn or parent::isbn)] | //*[parent::isbn]' xml/books.xml
Result Output:
<?xml version="1.0" encoding="ISO-8859-1"?>
<books>
<book type="hardback">
<isbn id="1">0525934189</isbn>
</book>
<book type="paperback">
<isbn id="2">0140185399</isbn>
</book>
</books>
Command:
# Select from XML document containing unicode characters
xml sel -T -t -m "//test[@lang='français']/@lang" -v . -n xml/unicode.xml
Result Output:
français
français
français
Command:
# Update value of an attribute
xml ed -u '/xml/table/rec[@id=3]/@id' -v 5 xml/tab-obj.xml
Result Output:
<?xml version="1.0"?>
<xml>
<table>
<rec id="1">
<numField>123</numField>
<stringField>String Value</stringField>
<object name="Obj1">
<property name="size">10</property>
<property name="type">Data</property>
</object>
</rec>
<rec id="2">
<numField>346</numField>
<stringField>Text Value</stringField>
</rec>
<rec id="5">
<numField>-23</numField>
<stringField>stringValue</stringField>
</rec>
</table>
</xml>
Command:
# Update value of an element
xml ed -u '/xml/table/rec[@id=1]/numField' -v 0 xml/tab-obj.xml
Result Output:
<?xml version="1.0"?>
<xml>
<table>
<rec id="1">
<numField>0</numField>
<stringField>String Value</stringField>
<object name="Obj1">
<property name="size">10</property>
<property name="type">Data</property>
</object>
</rec>
<rec id="2">
<numField>346</numField>
<stringField>Text Value</stringField>
</rec>
<rec id="3">
<numField>-23</numField>
<stringField>stringValue</stringField>
</rec>
</table>
</xml>
Command:
# Validate XML documents using well-formedness/DTD/XSD/RelaxNG checks
echo "==============================================="
echo "Well-Formedness Validation Tests"
echo "- 1 -------------------------------------------"
xml val xml/table.xml xml/tab-obj.xml xml/tab-bad.xml 2>/dev/null; echo $?
echo "- 2 -------------------------------------------"
xml val -g xml/table.xml xml/tab-obj.xml xml/tab-bad.xml 2>/dev/null; echo $?
echo "- 3 -------------------------------------------"
xml val -b xml/table.xml xml/tab-obj.xml xml/tab-bad.xml 2>/dev/null; echo $?
echo "- 4 -------------------------------------------"
xml val -q xml/table.xml xml/tab-obj.xml 2>/dev/null; echo $?
echo "==============================================="
echo "DTD Validation Tests"
echo "- 1 -------------------------------------------"
xml val -d dtd/table.dtd xml/table.xml xml/tab-obj.xml xml/tab-bad.xml 2>/dev/null; echo $?
echo "- 2 -------------------------------------------"
xml val -g -d dtd/table.dtd xml/table.xml xml/tab-obj.xml xml/tab-bad.xml 2>/dev/null; echo $?
echo "- 3 -------------------------------------------"
xml val -b -d dtd/table.dtd xml/table.xml xml/tab-obj.xml xml/tab-bad.xml 2>/dev/null; echo $?
echo "- 4 -------------------------------------------"
xml val -q -d dtd/table.dtd xml/table.xml 2>/dev/null; echo $?
echo "==============================================="
echo "Schema Validation Tests"
echo "- 1 -------------------------------------------"
xml val -s xsd/table.xsd xml/table.xml xml/tab-obj.xml xml/tab-bad.xml 2>/dev/null; echo $?
echo "- 2 -------------------------------------------"
xml val -g -s xsd/table.xsd xml/table.xml xml/tab-obj.xml xml/tab-bad.xml 2>/dev/null; echo $?
echo "- 3 -------------------------------------------"
xml val -b -s xsd/table.xsd xml/table.xml xml/tab-obj.xml xml/tab-bad.xml 2>/dev/null; echo $?
echo "- 4 -------------------------------------------"
xml val -q -s xsd/table.xsd xml/table.xml 2>/dev/null; echo $?
echo "==============================================="
echo "RelaxNG Schema Validation Tests"
echo "- 1 -------------------------------------------"
xml val -r relaxng/address.rng relaxng/address.xml relaxng/address-bad.xml 2>/dev/null; echo $?
echo "- 2 -------------------------------------------"
xml val -g -r relaxng/address.rng relaxng/address.xml relaxng/address-bad.xml 2>/dev/null; echo $?
echo "- 3 -------------------------------------------"
xml val -b -r relaxng/address.rng relaxng/address.xml relaxng/address-bad.xml 2>/dev/null; echo $?
echo "- 4 -------------------------------------------"
xml val -q -r relaxng/address.rng relaxng/address.xml 2>/dev/null; echo $?
Result Output:
===============================================
Well-Formedness Validation Tests
- 1 -------------------------------------------
xml/table.xml - valid
xml/tab-obj.xml - valid
xml/tab-bad.xml - invalid
1
- 2 -------------------------------------------
xml/table.xml
xml/tab-obj.xml
1
- 3 -------------------------------------------
xml/tab-bad.xml
1
- 4 -------------------------------------------
0
===============================================
DTD Validation Tests
- 1 -------------------------------------------
xml/table.xml - valid
xml/tab-obj.xml - invalid
xml/tab-bad.xml - invalid
1
- 2 -------------------------------------------
xml/table.xml
1
- 3 -------------------------------------------
xml/tab-obj.xml
xml/tab-bad.xml
1
- 4 -------------------------------------------
0
===============================================
Schema Validation Tests
- 1 -------------------------------------------
xml/table.xml - valid
xml/tab-obj.xml - invalid
xml/tab-bad.xml - invalid
1
- 2 -------------------------------------------
xml/table.xml
1
- 3 -------------------------------------------
xml/tab-obj.xml
xml/tab-bad.xml
1
- 4 -------------------------------------------
0
===============================================
RelaxNG Schema Validation Tests
- 1 -------------------------------------------
relaxng/address.xml - valid
relaxng/address-bad.xml - invalid
1
- 2 -------------------------------------------
relaxng/address.xml
1
- 3 -------------------------------------------
relaxng/address-bad.xml
1
- 4 -------------------------------------------
0
Command:
# Include one XML document into another using XInclude
xml tr --xinclude xsl/cat.xsl xml/document.xml | xml c14n
Result Output:
<document xmlns:xi="http://www.w3.org/2003/XInclude">
<p>120 Mz is adequate for an average home user.</p>
<disclaimer>
<p>The opinions represented herein represent those of the individual
and should not be interpreted as official policy endorsed by this
organization.</p>
</disclaimer>
</document>
Command:
# Passing parameters to XSLT stylesheet
xml tr xsl/param1.xsl -p Count='count(/xml/table/rec)' -s Text="Count=" xml/table.xml
Result Output:
Count=3
Command:
# Applying XSLT stylesheet to XML document
xml tr xsl/sum1.xsl xml/table.xml
Result Output:
446
|