--- xmlstarlet-1.0.1.orig/doc/xmlstarlet-ug.html 2004-11-04 06:03:59 +0300 +++ xmlstarlet-1.0.1/doc/xmlstarlet-ug.html 2007-12-10 14:40:56 +0300 @@ -1,9 +1,39 @@ - XmlStarlet Command Line XML Toolkit User's Guide

XmlStarlet Command Line XML Toolkit User's Guide

Mikhail Grushinskiy


Table of Contents

1. Introduction
1. About XmlStarlet
2. Main Features
3. Supported Platforms
4. Finding binary packages
2. Installation
1. Installation on Linux
2. Installation on Solaris
3. Installation on MacOS X
4. Installation on Windows
3. Getting Started
1. Basic Command-Line Options
2. Studying Structure of XML Document
4. XmlStarlet Reference
1. Querying XML documents
2. Transforming XML documents
3. Editing XML documents
4. Validating XML documents
5. Formatting XML documents
6. Canonicalization of XML documents
7. XML and PYX format
8. Escape/Unescape special XML characters
9. List directory as XML
5. Common problems
1. Namespaces and default namespace
2. Special characters
3. Sorting
4. Validation

Chapter 1. Introduction

1. About XmlStarlet

XMLStarlet 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, or XMLStarlet mailing list)

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)

  • 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

3. Supported Platforms

Here is a list of platforms on which XmlStarlet is known to work.

  • Linux

  • Solaris

  • Windows

  • MacOS X

  • FreeBSD/NetBSD

  • HP-UX

  • AIX

You might be able to compile and make it on others too.

4. Finding binary packages

Here is a list of sites where you can also find XmlStarlet binary packages.

Chapter 2. Installation

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 to search for RPM appropriate for your distribution.

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

3. Installation on MacOS X

XmlStarlet is available on MacOS in Fink. See fink.sourceforge.net

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 on your Windows machine.

Chapter 3. Getting Started

1. Basic Command-Line Options

Basic command line syntax:

bash-2.03$ xml
+   XmlStarlet Command Line XML Toolkit User's Guide
+   
+

XmlStarlet Command Line XML Toolkit User's Guide

Mikhail Grushinskiy


Chapter 1. Introduction

1. About XmlStarlet

XMLStarlet 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 'xmlstarlet' 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, or XMLStarlet mailing list)

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)

  • 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

3. Supported Platforms

Here is a list of platforms on which XmlStarlet is known to work.

  • Linux

  • Solaris

  • Windows

  • MacOS X

  • FreeBSD/NetBSD

  • HP-UX

  • AIX

You might be able to compile and make it on others too.

4. Finding binary packages

Here is a list of sites where you can also find XmlStarlet binary packages.

Chapter 2. Installation

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 to search for RPM appropriate for your distribution.

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

3. Installation on MacOS X

XmlStarlet is available on MacOS in Fink. See fink.sourceforge.net

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 on your Windows machine.

Chapter 3. Getting Started

1. Basic Command-Line Options

Basic command line syntax:

bash-2.03$ xmlstarlet
 XMLStarlet Toolkit: Command line utilities for XML
-Usage: xml [<options>] <command> [<cmd-options>]
+Usage: xmlstarlet [<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)
@@ -23,7 +53,7 @@ where <command> is one of:
 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
+Type: xmlstarlet <command> --help <ENTER> for command help
 
 XMLStarlet is a command line toolkit to query/edit/check/transform
 XML documents (for more information see http://xmlstar.sourceforge.net/)

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)

<xml>
@@ -41,7 +71,7 @@ XML documents (for more information see 
       <stringField>stringValue</stringField>
     </rec>
   </table>
-</xml>
xml el table.xml

would produce the following output.

xml
+</xml>
xmlstarlet el table.xml

would produce the following output.

xml
 xml/table
 xml/table/rec
 xml/table/rec/numField
@@ -51,11 +81,11 @@ 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/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:

xmlstarlet el -u table.xml

Output:

xml
 xml/table
 xml/table/rec
 xml/table/rec/numField
-xml/table/rec/stringField

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/rec/stringField

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:

xmlstarlet el -a table.xml

Output:

xml
 xml/table
 xml/table/rec
 xml/table/rec/@id
@@ -68,7 +98,7 @@ 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/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:

xmlstarlet el -v table.xml

Output:

xml
 xml/table
 xml/table/rec[@id='1']
 xml/table/rec/numField
@@ -78,8 +108,8 @@ 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

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 <global-options> {<template>} [ <xml-file> ... ]
+xml/table/rec/stringField

Chapter 4. XmlStarlet Reference

1. Querying XML documents

XmlStarlet 'select' or 'sel' option can be used to query or search XML documents. Here is synopsis for 'xmlstarlet sel' command:

XMLStarlet Toolkit: Select from XML document(s)
+Usage: xmlstarlet 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)
@@ -90,7 +120,7 @@ where
   -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
+  -D or --xml-decl   - do not omit xmlstarlet declaration line
   -B or --noblanks   - remove insignificant spaces from XML tree
   -N <name>=<value>  - predefine namespaces (name without 'xmlns:')
                        ex: xsql=urn:oracle-xsql
@@ -123,7 +153,7 @@ There can be multiple --match, --copy-of
 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" \
+xmlstarlet sel -t -c "xpath0" -m "xpath1" -m "xpath2" -v "xpath3" \
         -t -m "xpath4" -c "xpath5"
 
 is equivalent to applying the following XSLT
@@ -154,7 +184,7 @@ XML documents (for more information see 
 
 Current implementation uses libxslt from GNOME codebase as XSLT processor
 (see http://xmlsoft.org/ for more details)
-

'select' option allows you basically avoid writting XSLT stylesheet to perform some queries on XML documents. I.e. various combinations of command line parameters will let you to generate XSLT stylesheet and apply in to XML documents with a single command line. Very often you do not really care what XSLT was created for you 'select' command, but in those cases when you do; you can always use -C or --comp switch which will let you see exactly which XSLT is applied to your input.

'select' option supports many EXSLT functions in XPath expressions.

Here are few examples which will help to understand how 'xml select' works:

EXAMPLE:

Count elements matching XPath expression:

xml sel -t -v "count(/xml/table/rec/numField)" table.xml

Input (table.xml):

<xml>
+

'select' option allows you basically avoid writting XSLT stylesheet to perform some queries on XML documents. I.e. various combinations of command line parameters will let you to generate XSLT stylesheet and apply in to XML documents with a single command line. Very often you do not really care what XSLT was created for you 'select' command, but in those cases when you do; you can always use -C or --comp switch which will let you see exactly which XSLT is applied to your input.

'select' option supports many EXSLT functions in XPath expressions.

Here are few examples which will help to understand how 'xmlstarlet select' works:

EXAMPLE:

Count elements matching XPath expression:

xmlstarlet sel -t -v "count(/xml/table/rec/numField)" table.xml

Input (table.xml):

<xml>
   <table>
     <rec id="1">
       <numField>123</numField>
@@ -170,7 +200,7 @@ Current implementation uses libxslt from
     </rec>
   </table>
 </xml>

Output:

3
-

Let's take a close look what it did internally. For that we will use '-C' option

$ xml sel -C -t -v "count(/xml/table/rec/numField)"
+

Let's take a close look what it did internally. For that we will use '-C' option

$ xmlstarlet sel -C -t -v "count(/xml/table/rec/numField)"
 <?xml version="1.0"?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:exslt="http://exslt.org/common"
@@ -204,8 +234,8 @@ Current implementation uses libxslt from
 <xsl:template name="t1">
   <xsl:value-of select="count(/xml/table/rec/numField)"/>
 </xsl:template>
-</xsl:stylesheet>

Every -t option is mapped into XSLT template. Options after '-t' are mapped into XSLT elements:

  • -v to <xsl:value-of>

  • -c to <xsl:copy-of>

  • -e to <xsl:element>

  • -a to <xsl:attribute>

  • -s to <xsl:sort>

  • -m to <xsl:for-each>

  • -i to <xsl:if>

  • and so on

By default subsequent options (for instance: -m) will result in nested corresponding XSLT elements (<xsl:for-each> for '-m'). To break this nesting you would have to put '-b' or '--break' after first '-m'.

Below are few more examples:

EXAMPLE

Count all nodes in XML documents. Print input name and node count after it.

xml sel -t -f -o " " -v "count(//node())" xml/table.xml xml/tab-obj.xml

Output:

xml/table.xml 32
-xml/tab-obj.xml 41

EXAMPLE

Find XML files matching XPath expression (containing 'object' element)

xml sel -t -m //object -f xml/table.xml xml/tab-obj.xml

Result output:

xml/tab-obj.xml

EXAMPLE

Calculate EXSLT (XSLT extentions) XPath value

echo "<x/>" | xml sel -t -v "math:abs(-1000)"

Result output:

1000

EXAMPLE

Adding elements and attributes using command line 'xml sel'

echo "<x/>" | xml sel -t -m / -e xml -e child -a data -o value

Result Output:

<xml><child data="value"/></xml>

EXAMPLE

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
+</xsl:stylesheet>

Every -t option is mapped into XSLT template. Options after '-t' are mapped into XSLT elements:

  • -v to <xsl:value-of>

  • -c to <xsl:copy-of>

  • -e to <xsl:element>

  • -a to <xsl:attribute>

  • -s to <xsl:sort>

  • -m to <xsl:for-each>

  • -i to <xsl:if>

  • and so on

By default subsequent options (for instance: -m) will result in nested corresponding XSLT elements (<xsl:for-each> for '-m'). To break this nesting you would have to put '-b' or '--break' after first '-m'.

Below are few more examples:

EXAMPLE

Count all nodes in XML documents. Print input name and node count after it.

xmlstarlet sel -t -f -o " " -v "count(//node())" xml/table.xml xml/tab-obj.xml

Output:

xml/table.xml 32
+xml/tab-obj.xml 41

EXAMPLE

Find XML files matching XPath expression (containing 'object' element)

xmlstarlet sel -t -m //object -f xml/table.xml xml/tab-obj.xml

Result output:

xml/tab-obj.xml

EXAMPLE

Calculate EXSLT (XSLT extentions) XPath value

echo "<x/>" | xmlstarlet sel -t -v "math:abs(-1000)"

Result output:

1000

EXAMPLE

Adding elements and attributes using command line 'xmlstarlet sel'

echo "<x/>" | xmlstarlet sel -t -m / -e xml -e child -a data -o value

Result Output:

<xml><child data="value"/></xml>

EXAMPLE

Query XML document and produce sorted text table

xmlstarlet 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

Equivalent stylesheet

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="no" method="text"/>
@@ -220,7 +250,7 @@ xml/tab-obj.xml 41

EXAMPL <xsl:value-of select="'&#10;'"/> </xsl:for-each> </xsl:template> -</xsl:stylesheet>

EXAMPLE

Predefine namespaces for XPath expressions

xml sel -N xsql=urn:oracle-xsql -t -v /xsql:query xsql/jobserve.xsql

Input (xsql/jobserve.xsql)

$ cat xsql/jobserve.xsql
+</xsl:stylesheet>

EXAMPLE

Predefine namespaces for XPath expressions

xmlstarlet sel -N xsql=urn:oracle-xsql -t -v /xsql:query xsql/jobserve.xsql

Input (xsql/jobserve.xsql)

$ cat xsql/jobserve.xsql
 <?xml version="1.0"?>
 <?xml-stylesheet type="text/xsl" href="jobserve.xsl"?>
 <xsql:query connection="jobs" xmlns:xsql="urn:oracle-xsql" max-rows="5">
@@ -232,7 +262,7 @@ xml/tab-obj.xml 41

EXAMPL FROM job WHERE UPPER(title) LIKE '%ORACLE%' ORDER BY first_posted DESC -

EXAMPLE

Print structure of XML element using xml sel (advanced XPath expressions and xml sel command usage)

xml sel -T -t -m '//*' \
+

EXAMPLE

Print structure of XML element using xmlstarlet sel (advanced XPath expressions and xmlstarlet sel command usage)

xmlstarlet sel -T -t -m '//*' \
 -m 'ancestor-or-self::*' -v 'name()' -i 'not(position()=last())' -o . -b -b -n \
 xml/structure.xml

Input (xml/structure.xml)

<a1>
   <a11>
@@ -272,7 +302,7 @@ a1.a13.a131

This example is a go <xsl:value-of select="'&#10;'"/> </xsl:for-each> </xsl:template> -</xsl:stylesheet>

EXAMPLE

Print all links of xhtml document

xml sel --net --html -T -t -m "//*[local-name()='a']" \
+</xsl:stylesheet>

EXAMPLE

Print all links of xhtml document

xmlstarlet sel --net --html -T -t -m "//*[local-name()='a']" \
    -o 'NAME: ' -v "translate(. , '&#10;', ' ')" -n \
    -o 'LINK: ' -v @href -n -n \
    http://xmlstar.sourceforge.net/

Sample output

NAME: XmlStarlet SourceForge Site
@@ -289,8 +319,8 @@ LINK: http://sourceforge.net/forum/?grou
 
 NAME: XMLStarlet mailing list
 LINK: http://lists.sourceforge.net/lists/listinfo/xmlstar-devel
-

2. Transforming XML documents

Here is synopsis for 'xml tr' command:

XMLStarlet Toolkit: Transform XML document(s) using XSLT
-Usage: xml tr [<options>] <xsl-file> {-p|-s <name>=<value>} [ <xml-file-or-uri> ... ]
+

2. Transforming XML documents

Here is synopsis for 'xmlstarlet tr' command:

XMLStarlet Toolkit: Transform XML document(s) using XSLT
+Usage: xmlstarlet tr [<options>] <xsl-file> {-p|-s <name>=<value>} [ <xml-file-or-uri> ... ]
 where
    <xsl-file>      - main XSLT stylesheet for transformation
    <xml-file>      - input XML document file name (stdin is used if missing)
@@ -315,7 +345,7 @@ XML documents (for more information see 
 Current implementation uses libxslt from GNOME codebase as XSLT processor
 (see http://xmlsoft.org/ for more details)
 

EXAMPLE:

# Transform passing parameters to XSLT stylesheet
-xml tr xsl/param1.xsl -p Count='count(/xml/table/rec)' -s Text="Count=" xml/table.xml
+xmlstarlet tr xsl/param1.xsl -p Count='count(/xml/table/rec)' -s Text="Count=" xml/table.xml
 

Input xsl/params1.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:param name="Text"/>
@@ -331,8 +361,8 @@ xml tr xsl/param1.xsl -p Count='count(/x
   </xsl:for-each>
 </xsl:template>
 </xsl:stylesheet>

Output

Count=3
-

3. Editing XML documents

Here is the synopsis for 'xml ed' command:

XMLStarlet Toolkit: Edit XML document(s)
-Usage: xml ed <global-options> {<action>} [ <xml-file-or-uri> ... ]
+

3. Editing XML documents

Here is the synopsis for 'xmlstarlet ed' command:

XMLStarlet Toolkit: Edit XML document(s)
+Usage: xmlstarlet 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 is used if missing)
@@ -360,7 +390,7 @@ where <action>
 XMLStarlet is a command line toolkit to query/edit/check/transform
 XML documents (for more information see http://xmlstar.sourceforge.net/)
 

EXAMPLE:

# Delete elements matching XPath expression
-xml ed -d "/xml/table/rec[@id='2']" xml/table.xml
+xmlstarlet ed -d "/xml/table/rec[@id='2']" xml/table.xml
 

Input

<xml>
   <table>
     <rec id="1">
@@ -390,14 +420,14 @@ xml ed -d "/xml/table/rec[@id='2']" xml/
   </table>
 </xml>
 

EXAMPLE

# Move element node
-echo '<x id="1"><a/><b/></x>' | xml ed -m "//b" "//a"
+echo '<x id="1"><a/><b/></x>' | xmlstarlet ed -m "//b" "//a"
 

Output

<x id="1">
   <a>
     <b/>
   </a>
 </x>
 

EXAMPLE

# Rename attributes
-xml ed -r "//*/@id" -v ID xml/tab-obj.xml
+xmlstarlet ed -r "//*/@id" -v ID xml/tab-obj.xml
 

Output:

<xml>
   <table>
     <rec ID="1">
@@ -419,7 +449,7 @@ xml ed -r "//*/@id" -v ID xml/tab-obj.xm
   </table>
 </xml>
 

EXAMPLE

# Rename elements
-xml ed -r "/xml/table/rec" -v record xml/tab-obj.xml
+xmlstarlet ed -r "/xml/table/rec" -v record xml/tab-obj.xml
 

Output:

<xml>
   <table>
     <record id="1">
@@ -441,7 +471,7 @@ xml ed -r "/xml/table/rec" -v record xml
   </table>
 </xml>
 

EXAMPLE

# Update value of an attribute
-xml ed -u "/xml/table/rec[@id=3]/@id" -v 5 xml/tab-obj.xml
+xmlstarlet ed -u "/xml/table/rec[@id=3]/@id" -v 5 xml/tab-obj.xml
 

Output:

<xml>
   <table>
     <rec id="1">
@@ -463,7 +493,7 @@ xml ed -u "/xml/table/rec[@id=3]/@id" -v
   </table>
 </xml>
 

EXAMPLE

# Update value of an element
-xml ed -u "/xml/table/rec[@id=1]/numField" -v 0 xml/tab-obj.xml
+xmlstarlet ed -u "/xml/table/rec[@id=1]/numField" -v 0 xml/tab-obj.xml
 

Output:

<xml>
   <table>
     <rec id="1">
@@ -484,8 +514,8 @@ xml ed -u "/xml/table/rec[@id=1]/numFiel
     </rec>
   </table>
 </xml>
-

4. Validating XML documents

Here is synopsis for 'xml val' command:

XMLStarlet Toolkit: Validate XML document(s)
-Usage: xml val <options> [ <xml-file-or-uri> ... ]
+

4. Validating XML documents

Here is synopsis for 'xmlstarlet val' command:

XMLStarlet Toolkit: Validate XML document(s)
+Usage: xmlstarlet 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
@@ -502,14 +532,14 @@ NOTE: XML Schemas are not fully supporte
 XMLStarlet is a command line toolkit to query/edit/check/transform
 XML documents (for more information see http://xmlstar.sourceforge.net/)
 

EXAMPLE

# Validate XML document against DTD
-xml val --dtd dtd/table.dtd xml/tab-obj.xml >/dev/null 2>&1; echo $?
+xmlstarlet val --dtd dtd/table.dtd xml/tab-obj.xml >/dev/null 2>&1; echo $?
 

Output:

1
 

EXAMPLE

# Validate against XSD schema
-xml val -b -s xsd/table.xsd xml/table.xml xml/tab-obj.xml 2>/dev/null; echo $?
+xmlstarlet val -b -s xsd/table.xsd xml/table.xml xml/tab-obj.xml 2>/dev/null; echo $?
 

Output:

xml/tab-obj.xml
 1
-

5. Formatting XML documents

Here is synopsis for 'xml fo' command:

XMLStarlet Toolkit: Format XML document
-Usage: xml fo [<options>] <xml-file>
+

5. Formatting XML documents

Here is synopsis for 'xmlstarlet fo' command:

XMLStarlet Toolkit: Format XML document
+Usage: xmlstarlet fo [<options>] <xml-file>
 where <options> are
    -n or --noindent            - do not indent
    -t or --indent-tab          - indent output with tabulation
@@ -526,7 +556,7 @@ where <options> are
 XMLStarlet is a command line toolkit to query/edit/check/transform
 XML documents (for more information see http://xmlstar.sourceforge.net/)
 

EXAMPLE

# Format XML document disabling indent
-cat xml/tab-obj.xml | xml fo --noindent 
+cat xml/tab-obj.xml | xmlstarlet fo --noindent 
 

Output:

<xml>
 <table>
 <rec id="1">
@@ -548,7 +578,7 @@ cat xml/tab-obj.xml | xml fo --noindent 
 </table>
 </xml>
 

EXAMPLE

# Recover malformed XML document
-xml fo -R xml/malformed.xml 2>/dev/null
+xmlstarlet fo -R xml/malformed.xml 2>/dev/null
 

Input:

<test_output>
    <test_name>foo</testname>
    <subtest>...</subtest>
@@ -557,8 +587,8 @@ xml fo -R xml/malformed.xml 2>/dev/nu
   <test_name>foo</test_name>
   <subtest>...</subtest>
 </test_output>
-

6. Canonicalization of XML documents

Here is synopsis for 'xml c14n' command:

XMLStarlet Toolkit: XML canonicalization
-Usage: xml c14n <mode> <xml-file> [<xpath-file>] [<inclusive-ns-list>]
+

6. Canonicalization of XML documents

Here is synopsis for 'xmlstarlet c14n' command:

XMLStarlet Toolkit: XML canonicalization
+Usage: xmlstarlet c14n <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
@@ -582,7 +612,7 @@ where
 XMLStarlet is a command line toolkit to query/edit/check/transform
 XML documents (for more information see http://xmlstar.sourceforge.net/)
 

EXAMPLE

# XML canonicalization
-xml c14n --with-comments ../examples/xml/structure.xml ; echo $?
+xmlstarlet c14n --with-comments ../examples/xml/structure.xml ; echo $?
 

Input ../examples/xml/structure.xml

<a1>
   <a11>
     <a111>
@@ -612,7 +642,7 @@ xml c14n --with-comments ../examples/xml
 </a1>
 0
 

EXAMPLE

# XML exclusive canonicalization
-xml c14n --exc-with-comments ../examples/xml/c14n.xml ../examples/xml/c14n.xpath
+xmlstarlet c14n --exc-with-comments ../examples/xml/c14n.xml ../examples/xml/c14n.xpath
 

Input

../examples/xml/c14n.xml
 
 <n0:pdu xmlns:n0='http://a.example.com'>
@@ -630,8 +660,8 @@ content
 

Output

<n1:elem1 xmlns:n1="http://b.example">
 content
 </n1:elem1>
-

7. XML and PYX format

Here is synopsis for 'xml pyx' command:

XMLStarlet Toolkit: Convert XML into PYX format (based on ESIS - ISO 8879)
-Usage: xml pyx {<xml-file>}
+

7. XML and PYX format

Here is synopsis for 'xmlstarlet pyx' command:

XMLStarlet Toolkit: Convert XML into PYX format (based on ESIS - ISO 8879)
+Usage: xmlstarlet pyx {<xml-file>}
 where
    <xml-file> - input XML document file name (stdin is used if missing)
 
@@ -646,7 +676,7 @@ ESIS Generation by Sean Mc Grath http://
 
 XMLStarlet is a command line toolkit to query/edit/check/transform
 XML documents (for more information see http://xmlstar.sourceforge.net/)
-

EXAMPLE

xml pyx input.xml
+

EXAMPLE

xmlstarlet pyx input.xml
 

Input (input.xml)

<books>
 <book type='hardback'>
 <title>Atlas Shrugged</title>
@@ -673,15 +703,15 @@ Aid 1
 -\n
 )book
 -\n
-)books

PYX is a line oriented format for XML files which can be helpful (and very efficient) when used in combination with regular line oriented UNIX command such as sed, grep, awk.

'depyx' option is used for conversion back from PYX into XML.

EXAMPLE (Delete all attributes). This should work really fast for very large XML documents.

xml pyx input.xml | grep -v  "^A" | xml depyx

Output

<books>
+)books

PYX is a line oriented format for XML files which can be helpful (and very efficient) when used in combination with regular line oriented UNIX command such as sed, grep, awk.

'depyx' option is used for conversion back from PYX into XML.

EXAMPLE (Delete all attributes). This should work really fast for very large XML documents.

xmlstarlet pyx input.xml | grep -v  "^A" | xmlstarlet depyx

Output

<books>
 <book>
 <title>Atlas Shrugged</title>
 <author>Ayn Rand</author>
 <isbn>0525934189</isbn>
 </book>
-</books>

Here is an article which describes how PYX format can be used to grep XML. http://www-106.ibm.com/developerworks/xml/library/x-matters17.html

8. Escape/Unescape special XML characters

Here is synopsis for 'xml esc' command:

xml esc --help
+</books>

Here is an article which describes how PYX format can be used to grep XML. http://www-106.ibm.com/developerworks/xml/library/x-matters17.html

8. Escape/Unescape special XML characters

Here is synopsis for 'xmlstarlet esc' command:

xmlstarlet esc --help
 XMLStarlet Toolkit: Escape special XML characters
-Usage: xml esc [<options>] [<string>]
+Usage: xmlstarlet esc [<options>] [<string>]
 where <options> are
    --help      - print usage
    (TODO: more to be added in future)
@@ -690,7 +720,7 @@ if <string> is missing stdin is us
 XMLStarlet is a command line toolkit to query/edit/check/transform
 XML documents (for more information see http://xmlstar.sourceforge.net/)
 

EXAMPLE

# Escape special XML characters
-cat xml/structure.xml | xml esc
+cat xml/structure.xml | xmlstarlet esc
 

Input

<a1>
   <a11>
     <a111>
@@ -718,13 +748,13 @@ cat xml/structure.xml | xml esc
     &lt;a131/&gt;
   &lt;/a13&gt;
 &lt;/a1&gt;
-

9. List directory as XML

Here is synopsis for 'xml ls' command:

XMLStarlet Toolkit: List directory as XML
-Usage: xml ls
+

9. List directory as XML

Here is synopsis for 'xmlstarlet ls' command:

XMLStarlet Toolkit: List directory as XML
+Usage: xmlstarlet ls
 Lists current directory in XML format.
 
 XMLStarlet is a command line toolkit to query/edit/check/transform
 XML documents (for more information see http://xmlstar.sourceforge.net/)
-

EXAMPLE

xml ls
+

EXAMPLE

xmlstarlet ls
 

Output

<xml>
 <d a="rwxr-xr-x" acc="2004.02.13 00:06:03" mod="2004.02.13 00:06:00" sz="4096"  n="."/>
 <d a="rwxr-xr-x" acc="2004.02.12 23:54:35" mod="2004.02.13 00:00:09" sz="4096"  n=".."/>
@@ -744,19 +774,19 @@ XML documents (for more information see 
 ...
 </body>
 </html>
-

And the following (initially looking correct) query to print all links

xml sel -t -m "//a" -c . -n 

would return nothing. The issue with this query is that it is not addressing element <a> in the right namespace. XPath requires all namespaces used in XPath expression be defined. So for declared namespace <html xmlns="http://www.w3.org/1999/xhtml"> in input XML, you have to do same for XPath (or XSLT). There is another important detail: namespace equivalency is determined not by namespace prefix, but by URI. See query below, which would return expected result

xml sel -N x="http://www.w3.org/1999/xhtml" -t -m "//x:a" -c . -n

Example of deleting namespace declarations.

Delete namespace declarations and all elements from non default namespace from the following XML document:

Input (file ns2.xml)

<doc xmlns="http://www.a.com/xyz" xmlns:ns="http://www.c.com/xyz">
+

And the following (initially looking correct) query to print all links

xmlstarlet sel -t -m "//a" -c . -n 

would return nothing. The issue with this query is that it is not addressing element <a> in the right namespace. XPath requires all namespaces used in XPath expression be defined. So for declared namespace <html xmlns="http://www.w3.org/1999/xhtml"> in input XML, you have to do same for XPath (or XSLT). There is another important detail: namespace equivalency is determined not by namespace prefix, but by URI. See query below, which would return expected result

xmlstarlet sel -N x="http://www.w3.org/1999/xhtml" -t -m "//x:a" -c . -n

Example of deleting namespace declarations.

Delete namespace declarations and all elements from non default namespace from the following XML document:

Input (file ns2.xml)

<doc xmlns="http://www.a.com/xyz" xmlns:ns="http://www.c.com/xyz">
   <A>test</A>
   <B>
     <ns:C>xyz</ns:C>
   </B>
 </doc>
-

Command:

xml ed -N N="http://www.c.com/xyz" -d '//N:*' ns2.xml | sed -e 's/ xmlns.*=".*"//g'

Output

<doc>
+

Command:

xmlstarlet ed -N N="http://www.c.com/xyz" -d '//N:*' ns2.xml | sed -e 's/ xmlns.*=".*"//g'

Output

<doc>
   <A>test</A>
   <B/>
 </doc>
-

2. Special characters

Sometimes issues appear with handling of special characters, where 'special' means in XML sence as well as in 'shell' terms. Examples below should clear at least some of the confusions.

You should not forget about the fact that your command lines are executed by shell and shell does substitutions of its special characters too. So for example, one may ask:

"Why does the following query return nothing?"

echo '<X name="foo">EEE</X>' | xml sel -t -m /X[@name='foo'] -v .

The answer lies in the way shell substitues 'foo', which simply becomes foo before the command is run. So the correct way to write that would be

echo '<X name="foo">EEE</X>' | xml sel -t -m "/X[@name='foo']" -v .

Another example involves XML special characters. Question: How to search for &apos; in text nodes?

The following should help

xml sel -t -m "//line[contains(text(),&quot;'&quot;)]" -c .
-

3. Sorting

Let's take a look at XSLT produced by the following 'xml sel' 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
+

2. Special characters

Sometimes issues appear with handling of special characters, where 'special' means in XML sence as well as in 'shell' terms. Examples below should clear at least some of the confusions.

You should not forget about the fact that your command lines are executed by shell and shell does substitutions of its special characters too. So for example, one may ask:

"Why does the following query return nothing?"

echo '<X name="foo">EEE</X>' | xmlstarlet sel -t -m /X[@name='foo'] -v .

The answer lies in the way shell substitues 'foo', which simply becomes foo before the command is run. So the correct way to write that would be

echo '<X name="foo">EEE</X>' | xmlstarlet sel -t -m "/X[@name='foo']" -v .

Another example involves XML special characters. Question: How to search for &apos; in text nodes?

The following should help

xmlstarlet sel -t -m "//line[contains(text(),&quot;'&quot;)]" -c .
+

3. Sorting

Let's take a look at XSLT produced by the following 'xmlstarlet sel' command:

# Query XML document and produce sorted text table
+xmlstarlet sel -T -t -m /xml/table/rec -s D:N:- "@id" -v "concat(@id,'|',numField,'|',stringField)" -n xml/table.xml
 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="no" method="text"/>
 <xsl:param name="inputFile">-</xsl:param>
@@ -771,4 +801,4 @@ xml sel -T -t -m /xml/table/rec -s D:N:-
   </xsl:for-each>
 </xsl:template>
 </xsl:stylesheet>
-

-s option of 'xml sel' command controls 'order', 'data-type', and 'case-order' attributes of <xsl:sort/> element .

4. Validation

Many questions are asked about XSD (XML schema) validation. Well, XmlStarlet relies on libxml2 which has incomplete support for XML schemas. Untill it is done in libxml2 it will not be in XmlStarlet.

\ В конце файла нет новой строки +

-s option of 'xmlstarlet sel' command controls 'order', 'data-type', and 'case-order' attributes of <xsl:sort/> element .

4. Validation

Many questions are asked about XSD (XML schema) validation. Well, XmlStarlet relies on libxml2 which has incomplete support for XML schemas. Untill it is done in libxml2 it will not be in XmlStarlet.

\ В конце файла нет новой строки