How to Beautify XML and HTML with xq: A Comprehensive Guide

Dec 30, 2023 | Programming

Welcome! In this blog post, we will explore how to use the command-line tool xq to beautify XML and HTML, as well as extract content effectively. Let’s dive deep into its features and usage, making your coding life easier and more organized!

Features of xq

  • Syntax highlighting
  • Automatic indentation and formatting
  • Automatic pagination
  • Node content extraction

Usage of xq

The xq tool can format XML and HTML files while highlighting their syntax. Here’s a quick guide on how to use it!

Formatting XML Files

To format an unformatted XML file and highlight its syntax, use the following command:

xq testdataxmlunformatted.xml

Using Standard Input

xq also accepts input through stdin. Here’s how to format XML directly from a URL:

curl -s https://www.w3schools.com/xml/note.xml | xq

Formatting HTML Content

HTML content can be formatted and highlighted by utilizing the -m flag as shown below:

xq -m testdatahtmlformatted.html

Extracting Content Using XPath

xq allows for content extraction using the XPath query language, enhancing its utility significantly. For example, to extract the text content of all nodes with the city name, use:

cat testdataxmlunformatted.xml | xq -x city

You can also extract the value of an attribute named status belonging to the user:

cat testdataxmlunformatted.xml | xq -x user@status

CSS Selector Usage

In addition to XPath, xq supports CSS selectors for content extraction. For example, to extract the body and p tags from HTML, use:

cat testdatahtmlunformatted.html | xq -q body p

Extracting Attribute Values

If you want to extract an attribute value rather than node content, you can make use of the additional option –attr (-a) as follows:

cat testdatahtmlunformatted.html | xq -q head script -a src

Installation of xq

Installing xq is straightforward and can be accomplished using several methods depending on your operating system:

  • For macOS via Homebrew: brew install xq
  • For macOS via MacPorts: sudo port install xq
  • For Linux using the custom installer: curl -sSL https://bit.ly/install-xq | sudo bash
  • For Ubuntu 22.10 or higher: apt-get install xq
  • For Fedora: dnf install xq
  • For other Linux distributions: Check this link.
  • If you have the Go toolchain installed: go install github.com/sibprogrammer/xq@latest
  • Use Docker: docker-compose run --rm xq xq opt examples xmlunformatted.xml

Troubleshooting

If you encounter issues while using xq, consider the following ideas:

  • Double-check the format of your XML or HTML files. Ensure they are well-formed.
  • Make sure you have the correct permissions if using installation commands (e.g., with sudo).
  • Review the provided commands for any typing errors; even a small mistake can lead to failure.
  • If you require more assistance, for insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox