Name

lxsort — Sort elements in an XML document

Synopsis

lxsort [ -xmlns[:prefix]=uri ...] [ -n ] [ -r ] domain-query element-query key-query [ input-file ]

Description

lxsort sorts elements in an XML document. The domain-query is an XPath that identifies elements within which sorting is done. Within each domain the element-query is used to select elements to be sorted. For each element the key-query is evaluated to to produce a sort key.

The input-file argument may be a URI instead of a filename. If no input-file argument is given, standard input is used.

The domain-query is streamed. The subtree rooted at each matching element is read, and is available to the element-query and key-query.

-xmlns[:prefix]=uri

binds a prefix (or the default namespace) to a URI for use in XPath queries.

-n

compare the keys as numbers rather than strings.

-r

reverse the sense of the sort.

Examples

In these examples, we assume a file of products and their prices containing the following data:

<products>
  <product price="3">Chicken</product>
  <product price="11.50">Lobster</product>
  <product price=".20">Apple</product>
  <product price="1.09">Milk (2 litres)</product>
</products>

lxsort products product . <prices.xml

Sort the products into alphabetic order of their names. (The XPath expression "." returns the text value of an element.) The output is:

<products>
  <product price=".20">Apple</product>
  <product price="3">Chicken</product>
  <product price="11.50">Lobster</product>
  <product price="1.09">Milk (2 litres)</product>
</products>

lxsort -n products product @price <prices.xml

Sort the products into ascending price order. The output is:

<products>
  <product price=".20">Apple</product>
  <product price="1.09">Milk (2 litres)</product>
  <product price="3">Chicken</product>
  <product price="11.50">Lobster</product>
</products>

lxsort -n -r products product @price <prices.xml

Sort the products into descending price order. The output is:

<products>
  <product price="11.50">Lobster</product>
  <product price="3">Chicken</product>
  <product price="1.09">Milk (2 litres)</product>
  <product price=".20">Apple</product>
</products>