lxprintf — Format text extracted from an XML document
lxprintf
[
-xmlns[:prefix]=uri
...] [
-e each-query
]
format
item-query
... [
input-file
]
lxprintf extracts text from an XML document and
formats it according the format
argument, which
resembles that of printf() in C. For each item in
the format
there must be an
item-query
argument. If an each-query
is specified with -e
, the item-queries
are applied relative
to each element matching the each-query
, otherwise they
are applied once at the document root.
The input-file argument may be a URI instead of a filename. If no input-file argument is given, standard input is used.
binds a prefix (or the default namespace) to a URI for use in XPath queries.
an XPath query selecting elements to be processed. For each matching element,
the format is called with the results of evaluating the
item-queries
relative to that element.
a C-style format string. The format specifiers allowed are
%s
(string),
%f
(floating-point),
%d
(integer), and
%%
(percent sign).
Field width and precision may be specified (see the examples below).
The escapes
\n
(newline),
\r
(carriage return),
\t
(tab), and
\a
(alert, i.e. bell)
are also allowed.
an XPath query which is evaluated to produce a value to be formatted. The result is converted to a string or number as appropriate for the corresponding format specifier.
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>
lxprintf "$%.2f\n" "//product[. = 'Chicken']/@price" <prices.xml
Print the price of a chicken as a number with two decimal places. The output is:
3ドル.00
lxprintf -e product "%-20s $%5.2f\n" . @price <prices.xml
Print a list of products and their prices. The product name will be padded to appear at the left of a 20-character field, and the price will be padded to appear at the right of a 5-character field. The output is:
Chicken $ 3.00 Lobster 11ドル.50 Apple $ 0.20 Milk (2 litres) $ 1.09