The xq is a command-line tool designed for working with XML and HTML documents. It supports querying document structures, selecting specific elements, and extracting values directly from the terminal. This tutorial demonstrates how to install xq XML and HTML processor on Ubuntu 26.04.
Install xq
Fetch the latest xq release version from GitHub and store it in a variable:
XQ_VERSION=$(curl -s "https://api.github.com/repos/sibprogrammer/xq/releases/latest" | grep -Po '"tag_name": "v\K[0-9.]+')
Download the latest release archive and unpack the xq binary into /usr/local/bin:
curl -sSL https://github.com/sibprogrammer/xq/releases/latest/download/xq_${XQ_VERSION}_linux_amd64.tar.gz \
| sudo tar xz -C /usr/local/bin xq
After the installation, verify that the xq binary is available by checking its version:
xq --version
Testing xq
For a simple example, create an XML document named test.xml:
nano test.xml
Add the following content:
<?xml version="1.0" encoding="utf-8"?>
<people>
<person>
<name>John</name>
<age>25</age>
</person>
<person>
<name>James</name>
<age>29</age>
</person>
</people>
The xq can use XPath expressions to locate particular nodes within an XML document. The following example selects the name element belonging to the second person entry:
xq -x '/people/person[2]/name' test.xml
Output:
James
Uninstall xq
When xq is no longer required, delete its binary from the system:
sudo rm -rf /usr/local/bin/xq
Leave a Comment
Cancel reply