Download: XmlStarlet Command Line XML Toolkit User’s Guide Mikhail Grushinskiy

XmlStarlet Command Line XML Toolkit User’s Guide Mikhail Grushinskiy XmlStarlet Command Line XML Toolkit User’s Guide by Mikhail Grushinskiy Table of Contents 1. Introduction... 1 1.1. About XmlStarlet ... 1 1.2. Main Features... 1 1.3. Supported Platforms... 2 2. Installation... 3 2.1. Installation on Linux ... 3 2.2. Installation on Solaris... 3 2.3. Installation on MacOS X... 3 2.4. Installation on Windows... 3 3. Getting Started... 4 3.1. Basic Command-Line Options... 4 3.2. Studying Structure of XML Document... 4 4. XmlStarlet Reference ... 7 4.1. Querying XML documents ... 7 4.2. Transfo...
Author: Walton Shared: 7/30/19
Downloads: 154 Views: 1175

Content

XmlStarlet Command Line XML Toolkit User’s Guide Mikhail Grushinskiy, XmlStarlet Command Line XML Toolkit User’s Guide by Mikhail Grushinskiy,

Table of Contents

1. Introduction... 1 1.1. About XmlStarlet ... 1 1.2. Main Features... 1 1.3. Supported Platforms... 2 2. Installation... 3 2.1. Installation on Linux ... 3 2.2. Installation on Solaris... 3 2.3. Installation on MacOS X... 3 2.4. Installation on Windows... 3 3. Getting Started... 4 3.1. Basic Command-Line Options... 4 3.2. Studying Structure of XML Document... 4 4. XmlStarlet Reference ... 7 4.1. Querying XML documents ... 7 4.2. Transforming XML documents... 14 4.3. Editing XML documents... 15 4.4. Validating XML documents ... 19 4.5. Formatting XML documents... 20 4.6. Canonicalization of XML documents ... 22 4.7. XML and PYX format ... 24 4.8. Escape/Unescape special XML characters... 25 4.9. List directory as XML... 27 5. Common problems... 28 5.1. Namespaces and default namespace ... 28 5.2. Special characters... 29 5.3. Sorting... 30 5.4. Validation ... 30 iii,

Chapter 1. Introduction

1.1. About XmlStarlet XMLStarlet (http://xmlstar.sourceforge.net/) is a set of command line utilities (tools) which can be used to transform, query, validate, and edit XML documents and files using simple set of shell commands in similar way it is done for plain text files using UNIX grep, sed, awk, diff, patch, join, etc commands. This set of command line utilities can be used by those who deal with many XML documents on UNIX shell command prompt as well as for automated XML processing with shell scripts. XMLStarlet command line utility is written in C and uses libxml2 and libxslt from http://xmlsoft.org/. Implementation of extensive choice of options for XMLStarlet utility was only possible because of rich feature set of libxml2 and libxslt (many thanks to the developers of those libraries for great work). ’diff’ and ’patch’ options are not currently implemented. Other features need some work too. Please, send an email to the project administrator (see http://sourceforge.net/projects/xmlstar/) if you wish to help. XMLStarlet is linked statically to both libxml2 and libxslt, so generally all you need to process XML documents is one executable file. To run XmlStarlet utility you can simple type ’xml’ on command line and see list of options available. XMLStarlet is open source freeware under MIT license which allows free use and distribution for both commercial and non-commercial projects. We welcome any user’s feedback on this project which would greatly help us to improve its quality. Comments, suggestions, feature requests, bug reports can be done via SourceForge project web site (see XMLStarlet Sourceforge forums (http://sourceforge.net/forum/?group_id=66612), or XMLStarlet mailing list (http://lists.sourceforge.net/lists/listinfo/xmlstar-devel/)) 1.2. Main Features The toolkit’s feature set includes options to: • Check or validate XML files (simple well-formedness check, DTD, XSD, RelaxNG) • Calculate values of XPath expressions on XML files (such as running sums, etc) • Search XML files for matches to given XPath expressions • Apply XSLT stylesheets to XML documents (including EXSLT support, and passing parameters to stylesheets), Chapter 1. Introduction • Query XML documents (ex. query for value of some elements of attributes, sorting, etc) • Modify or edit XML documents (ex. delete some elements) • Format or "beautify" XML documents (as changing indentation, etc) • Fetch XML documents using http:// or ftp:// URLs • Browse tree structure of XML documents (in similar way to ’ls’ command for directories) • Include one XML document into another using XInclude • XML c14n canonicalization • Escape/unescape special XML characters in input text • Print directory as XML document • Convert XML into PYX format (based on ESIS - ISO 8879), and vice versa 1.3. Supported Platforms Here is a list of platforms on which XmlStarlet is known to work. • Linux • Solaris • Windows • MacOS X • FreeBSD • HP-UX You might be able to compile and make it on others too.,

Chapter 2. Installation

2.1. Installation on Linux Execute the following command as root rpm -i xmlstarlet-x.x.x-1.i386.rpm where x.x.x indicates package version. You can use http://rpmfind.net (http://fr2.rpmfind.net/linux/rpm2html/search.php?query=xmlstarlet&system=&arch=) to search for RPM appropriate for your distribution. 2.2. Installation on Solaris Execute the following commands as root gunzip xmlstarlet-x.x.x-sol8-sparc-local.gz pkgadd -d xmlstarlet-x.x.x-sol8-sparc-local all 2.3. Installation on MacOS X XmlStarlet is available on MacOS in Fink. See fink.sourceforge.net (http://fink.sourceforge.net/pdb/package.php/xmlstarlet) 2.4. Installation on Windows Unzip the file xmlstarlet-x.x.x-win32.zip to some directory. To take advantage of UNIX shell scripting you might want to run XmlStarlet from Cygwin. Consider installing Cygwin (http://www.cygwin.com/) on your Windows machine.,

Chapter 3. Getting Started

3.1. Basic Command-Line Options Basic command line syntax: bash-2.03$ xml XMLStarlet Toolkit: Command line utilities for XML Usage: xml [] [] where 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 are: -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 -help for command help XMLStarlet is a command line toolkit to query/edit/check/transform XML documents (for more information see http://xmlstar.sourceforge.net/) 3.2. Studying Structure of XML Document Before you do anything with your XML document you probably would like to know its structure at first. ’el’ option could be used for this purpose. Let’s say you have the following XML document (table.xml) 123, Chapter 3. Getting Started String Value 346 Text Value -23 stringValue
xml el table.xml would produce the following output. xml xml/table xml/table/rec xml/table/rec/numField xml/table/rec/stringField xml/table/rec xml/table/rec/numField xml/table/rec/stringField xml/table/rec xml/table/rec/numField xml/table/rec/stringField Every line in this output is an XPath expression which indicates a ’path’ to elements in XML document. You would use these XPath expressions to navigate through your XML documents in other XmlStarlet options. XML documents can be pretty large but with a very simple structure. (This is espesially true for data driven XML documents ex: XML formatted result of select from SQL table). If you just interested in structure but not order of the elements you can use -u switch combined with ’el’ option. EXAMPLE: xml el -u table.xml Output: xml xml/table xml/table/rec xml/table/rec/numField xml/table/rec/stringField, Chapter 3. Getting Started If you are interested not just in elements of your XML document, but you want to see attributes as well you can use -a switch with ’el’ option. And every line of the output will still be a valid XPath expression. EXAMPLE: xml el -a table.xml Output: xml xml/table 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 xml/table/rec xml/table/rec/@id xml/table/rec/numField xml/table/rec/stringField If you are looking for attribute values as well use -v switch of ’el’ option. And again - every line of output is a valid XPath expression. EXAMPLE: xml el -v table.xml Output: xml xml/table xml/table/rec[@id=’1’] xml/table/rec/numField xml/table/rec/stringField 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,

Chapter 4. XmlStarlet Reference

4.1. Querying XML documents XmlStarlet ’select’ or ’sel’ option can be used to query or search XML documents. Here is synopsis for ’xml sel’ command: XMLStarlet Toolkit: Select from XML document(s) Usage: xml sel {