Using the XMLStarlet Toolkit: Query, edit, check, convert and transform XML documents (with examples)
XMLStarlet is a command-line tool that allows you to manipulate XML documents with ease. It provides a variety of subcommands to query, edit, check, convert, and transform XML files.
In this article, we will explore the different use cases of the XML command, including displaying general help, executing subcommands with input from files or URIs, executing subcommands using standard input/output, outputting to a file, and displaying the version of the XMLStarlet Toolkit.
Use case 1: Display general help
To display the general help, including the list of subcommands, you can use the following command:
xml --help
Motivation: You want to get an overview of the available subcommands and their usage.
Explanation: The --help
option is used to display the general help menu. It lists all the available subcommands and provides a brief description of each.
Example output:
XMLStarlet Toolkit: Query, edit, check, convert and transform XML documents.
This command also has documentation about its subcommands, e.g. `xml validate`.
More information: <http://xmlstar.sourceforge.net/docs.php>.
Usage: xml [--version] [--help] <command> [<options>]
Possible commands (some of the subcommands might not be available):
c14n canonicalization
edit Edit/Update XML document(s)
el Select element(s)
fo XSL-FO formatting object generation
ls List nodes
pyx convert XML to PYX format (based on ESIS - ISO 8879)
sel Select data value(s)
sel1 Select data value(s) (only 1st occurrence is printed)
tr Translate, i.e. perform a find/replace on XML data values
val Validate (well-balanced) XML document
ver Display XML document(s) version
x2d Convert XML document to Diff format
x2p Convert XML document to Perl hash
x2s Convert XML document to XML Schema
x2t Apply XSLT stylesheet
xd Display XML document
elid Visit an element based on its Id
Run `xml <command> --help' for help on a specific command.
Use case 2: Execute a subcommand with input from a file or URI, printing to stdout
To execute a subcommand with input from a file or URI and print the result to the standard output (stdout), you can use the following command:
xml subcommand options path/to/input.xml|URI
Motivation: You want to perform a specific operation on an XML document and view the result.
Explanation: Replace subcommand
with the desired subcommand, options
with the appropriate options for the subcommand, and path/to/input.xml|URI
with the path to the input XML file or URI.
Example command:
xml sel -t -v "/root/element" sample.xml
In this example, sel
is the subcommand used to select an element from the XML document specified by sample.xml
. The -t
option specifies that the output should be treated as a template string, and the -v
option is used to print the value of the selected element.
Example output:
element value
Use case 3: Execute a subcommand using stdin and stdout
To execute a subcommand using standard input (stdin) and outputting the result to stdout, use the following command:
xml subcommand options
Motivation: You want to perform a specific operation on XML data provided through standard input.
Explanation: Replace subcommand
with the desired subcommand and options
with the appropriate options for the subcommand.
Example command:
echo "<root>data</root>" | xml sel -t -v "/"
In this example, the XML data <root>data</root>
is provided to the sel
subcommand through standard input. The -t
option treats the output as a template string, and the -v
option prints the selected value.
Example output:
data
Use case 4: Execute a subcommand with input from a file or URI and output to a file
To execute a subcommand with input from a file or URI and output the result to a file, use the following command:
xml subcommand options path/to/input.xml|URI > path/to/output
Motivation: You want to save the output of the XML operation to a file for further processing or analysis.
Explanation: Replace subcommand
with the desired subcommand, options
with the appropriate options for the subcommand, path/to/input.xml|URI
with the path to the input XML file or URI, and path/to/output
with the desired path for the output file.
Example command:
xml sel -t -v "/root/element" sample.xml > output.txt
In this example, the sel
subcommand is used to select an element from the sample.xml
file. The selected element’s value is then saved to the output.txt
file.
Use case 5: Display help for a subcommand
To display help for a specific subcommand, use the following command:
xml subcommand --help
Motivation: You want detailed information about a specific subcommand and its usage.
Explanation: Replace subcommand
with the desired subcommand for which you want to display help.
Example command:
xml sel --help
Example output:
Subcommand: sel
Summary: Select data value(s)
Usage: sel [-t] [-v value_expr] [-m match_expr] [file...]
Options:
-t or --template Treat input as a template
-v or --value-of Print value-of for xpath expr against each input node
-c or --copy-of Print copy-of for xpath expr against each input node
-u or --unique Print unique nodes of xpath expr for each input node
-m or --match Copy input to output if matching xpath expr
-i or --if If xpath evals to non-empty string, then do
-e or --elem Wrap with an element whose name is given by
-n or --nl Add newline at end when printing
-1 or --once Select only first node (if template option is used)
-0 or --if0 If xpath evals to empty string, then do
-B or --break-long Break line if it's longer than given number of chars
-C or --combo Use ` to combine templates
-F or --fix-nested-off workaround for broken nested xsl:if support
-H or --html generate human-readable output
-M or --match-name Which part of the node to match against
-N or --name Name-prefix for hardly predictable namespace names
-P or --prefix Remove prefix
-R or --reset Reset counters
-S or --indent-spaces Number of spaces to use for each indentation level
-T or --text Show printable text nodes only
-V or --value-of-nodes Show children text nodes' value
--without-comments Skip comment nodes when copying the tree
--without-ns-default Do not output default namespace declarations
--with-ns Copy namespace nodes by default
XPath expression syntax:
See 'xml sel --help' for more info on XPath expression syntax
Use case 6: Display the version of the XMLStarlet Toolkit
To display the version of the XMLStarlet Toolkit, use the following command:
xml --version
Motivation: You want to confirm which version of the XMLStarlet Toolkit you are using.
Explanation: The --version
option displays the version number of the XMLStarlet Toolkit.
Example command:
xml --version
Example output:
XMLStarlet Toolkit version 1.6.1
Conclusion
XMLStarlet’s command-line tool offers great flexibility for manipulating, querying, and transforming XML documents. In this article, we explored different use cases, including displaying general help, executing subcommands with input from files or URIs, using standard input/output, outputting to a file, and displaying the version of the XMLStarlet Toolkit. By understanding these features, you can efficiently work with XML documents and automate XML-related tasks.