CryptoSys Home > xmlsq > Using xmlsq from the command line

Using xmlsq from the command line


Command-line Syntax | Options | An example | Dealing with non-ASCII characters | A note about the "asciify" option | Contact us

Command-line Syntax

Usage: xmlsq {[-g]|-f|-c} [OPTION]... QUERY [INFILE]
Perform XPath 1.0 queries on an XML document.
  -g, --get-text      get text for first matching node [default]
  -f, --full-query    execute full XPath 1.0 query
  -c, --count         return the integer value of `count(query)`
OPTIONS:
  -@, --stdin         read input from stdin [default=INFILE]
  -i, --input=INFILE  optional way to specify INFILE
  -a, --asciify       output non-ASCII chars as XML character references
  -r, --raw           output nodeset in raw format [default=prettify]
  -t, --trim          trim leading/trailing whitespace
                       (and collapse whitespace for an attribute value)
  -d, --delim=DELIM   enclose output in delimiter(s), eg ' or []
  -v, --version       print program version and exit
  -h, --help          print this help and exit
  -E, --examples      print examples and exit
INFILE must be specified unless `--stdin` option is used.
Exit status is 0 on success or 1 if error.

For examples type `xmlsq --examples`

Options

Equivalent ways to specify the input file using --stdin (-@) and --input (-i):
> xmlsq  //book/title bookstore.xml
Harry Potter

> xmlsq --stdin  //book/title < bookstore.xml
Harry Potter

> xmlsq -i bookstore.xml  //book/title
Harry Potter
Use the --asciify (-a) option to convert non-ASCII characters to XML character references. (See also Dealing with non-ASCII characters and A note about the "asciify" option.)
> xmlsq /bookstore/book[3]/title bookstore.xml
El critic├│n

> xmlsq --asciify /bookstore/book[3]/title bookstore.xml
El critic&#xF3;n
Use the --raw (-r) option to turn off the default prettify behaviour for nodesets:
> xmlsq -f /a hello.xml
<a>
        <b foo="baz">hello</b>
        <b>world</b>
</a>

> xmlsq --raw -f /a hello.xml
<a><b foo="baz">hello</b><b>world</b></a>
Use the --trim (-t) option to trim leading and trailing whitespace:
<EncryptedData>
  <!-- ... -->
    <CipherValue>
      QMpxhXq1DtBeyC9KfSaMQWrEtefe+e935gF/x62spvmL6IW0XeS0W4Kk31OgWzN0
    </CipherValue>
  <!-- ... -->
</EncryptedData> 
> xmlsq "/EncryptedData/CipherData/CipherValue" encrypt-data-aes128-cbc.xml

      QMpxhXq1DtBeyC9KfSaMQWrEtefe+e935gF/x62spvmL6IW0XeS0W4Kk31OgWzN0


> xmlsq --trim "/EncryptedData/CipherData/CipherValue" encrypt-data-aes128-cbc.xml
QMpxhXq1DtBeyC9KfSaMQWrEtefe+e935gF/x62spvmL6IW0XeS0W4Kk31OgWzN0
For attribute values, the --trim option also collapses whitespace
> xmlsq --delim=[] /a/@foo "<a foo='  spaced    out  '/>
[  spaced    out  ]

> xmlsq --delim=[] --trim /a/@foo "<a foo='  spaced    out  '/>
[spaced out]
The --delim (-d) option adds delimiting characters to the output. Specify one character (--delim==') or two characters (--delim==[]). Useful for detecting empty or missing results or showing leading and trailing whitespace.
> xmlsq --delim=' /a/e "<a><e /></a>"
''

> xmlsq -d [] /a/e "<a><e /></a>"
[]

An example

Input file: bookstore.xml
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
  <title lang="en">Harry Potter</title>
  <price>29.99</price>
</book>
<book>
  <title lang="en">Learning XML</title>
  <price>39.95</price>
</book>
<book>
  <title lang="es">El criticón</title>
  <price>19.95</price>
</book>
</bookstore>
> xmlsq --count /bookstore/book bookstore.xml
3

> xmlsq /bookstore/book/title bookstore.xml
Harry Potter

> xmlsq /bookstore/book[2]/title bookstore.xml
Learning XML

> xmlsq -f /bookstore/book[1]/title bookstore.xml
<title lang="en">Harry Potter</title>

> xmlsq --asciify -f /bookstore/book/title bookstore.xml
<title lang="en">Harry Potter</title>
<title lang="en">Learning XML</title>
<title lang="es">El critic&#xF3;n</title>

> xmlsq -f /bookstore/book/title/@lang bookstore.xml
lang="en"
lang="en"
lang="es"

> xmlsq  "//book/price[../title='Learning XML']" bookstore.xml
39.95

Dealing with non-ASCII characters

Non-ASCII characters like the letter ó may appear as "funny" characters in your console, perhaps as ├│ or ó. For example:
> xmlsq /bookstore/book[3]/title bookstore.xml
El critic├│n
Fixed!Fixed 2020-09-04: The new CLI version 0.9.0a released 4 September 2020 has fixed this CLI display problem.
> xmlsq /bookstore/book[3]/title bookstore.xml
El criticón

> xmlsq "//*[text()='El criticón']" bookstore.xml
El criticón
Alternatively, you can use the --asciify option to display as an XML character reference:
> xmlsq --asciify /bookstore/book[3]/title bookstore.xml
El critic&#xF3;n
or set your console to display UTF-8-encoded characters using the chcp command with code page 65001.
> chcp 65001
Active code page: 65001

> xmlsq /bookstore/book[3]/title bookstore.xml
El criticón

A note about the "asciify" option

The --asciify (-a) option converts all non-ASCII characters to XML numeric character references. This option is provided as a convenience for display on a console where UTF-8-encoded characters do not display properly. Just be aware that some output may not be valid XML. XML character references are only valid inside element content or an attribute value. They cannot be used inside an element or attribute name.

Input file: numero.xml
<doc>
<a n="veintidós" />
<b número="22" />
</doc>
Output here is valid XML (character reference &#xF3; inside attribute value is OK).
> xmlsq --asciify -f "//a" numero.xml
<a n="veintid&#xF3;s" />
But output here is invalid XML (character reference &#xFA; not permitted inside attribute name.
> xmlsq --asciify -f "//b" numero.xml
<b n&#xFA;mero="22" />
In most cases, this should not be a problem. Just be aware of it if you are copying output into a new XML document.

Contact us

To contact us or comment on this page, please send us a message.

This page last updated 15 September 2020

[Go to top]