Skip to content
프로젝트

소개

XML 및 HTML을 보기 좋게 정리하고 콘텐츠를 추출하는 명령줄 도구

웹 및 앱 개발

이 수치를 측정하는 방법

What you can do

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

측정된 스타 증가

in 24h
0
in 7 days
-1
in 15 days
+1
in 1 month
0

서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.

README.md

Original

xq

build Codecov Scc Count Homebrew Macports

Command-line XML and HTML beautifier and content extractor.

xq

Features

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

Usage

Format an XML file and highlight the syntax:

xq test/data/xml/unformatted.xml

xq also accepts input through stdin:

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

HTML content can be formatted and highlighted as well (using -m flag):

xq -m test/data/html/formatted.html

Format multiple files at once:

xq test/data/xml/unformatted.xml test/data/xml/unformatted2.xml

In place formatting is supported as well (using -i flag):

xq -i test/data/xml/unformatted.xml

It is possible to extract the content using XPath query language. -x parameter accepts XPath expression.

Extract the text content of all nodes with city name:

cat test/data/xml/unformatted.xml | xq -x //city

Extract the value of attribute named status and belonging to user:

cat test/data/xml/unformatted.xml | xq -x /user/@status

See https://en.wikipedia.org/wiki/XPath for details.

It is possible to use CSS selector to extract the content as well:

cat test/data/html/unformatted.html | xq -q "body > p"

Extract an attribute value instead of node content additional option --attr (-a) can be used:

cat test/data/html/unformatted.html | xq -q "head > script" -a "src"

Extract part of HTML with tags (not only text content) using CSS selector:

cat test/data/html/unformatted.html | xq -n -q "head"

By default, a query that finds no matches produces empty output and a zero exit code. Use the --strict (-s) option to get a non-zero exit code instead, e.g. for testing the presence of a node in scripts:

cat test/data/xml/unformatted.xml | xq -s -x //missing || echo "node not found"

Output the result as JSON:

cat test/data/xml/unformatted.xml | xq -j

This will output the result in JSON format, preserving the XML structure. The JSON output will be an object where:

  • XML elements become object keys
  • Attributes are prefixed with "@"
  • Text content is stored under "#text" if the element has attributes or child elements
  • Repeated elements are automatically converted to arrays
  • Elements with only text content are represented as strings

The output is piped to a pager if it is defined via the XQ_PAGER or PAGER environment variable (XQ_PAGER takes precedence). The pager can be disabled using the --no-pager option:

xq --no-pager test/data/xml/unformatted.xml

Installation

The preferable ways to install the utility are described below.

For macOS, via Homebrew:

brew install xq

For macOS, via MacPorts:

sudo port install xq

For Linux using custom installer:

curl -sSL https://bit.ly/install-xq | sudo bash

For Linux using custom installer, changing INSTALL_DIR, without sudo:

curl -sSL https://bit.ly/install-xq | INSTALL_DIR=$(pwd) bash

For Ubuntu 22.10 or higher via package manager:

apt-get install xq

For Fedora via package manager:

dnf install xq

A more detailed list of Linux distros that package the xq utility can be found here: https://repology.org/project/xq-sibprogrammer/versions

If you have Go toolchain installed, you can use the following command to install xq:

go install github.com/sibprogrammer/xq@latest

You can play with the xq utility using the Dockerized environment:

docker compose run --rm xq
xq /opt/examples/xml/unformatted.xml

관련 프로젝트 살펴보기

불러오는 중…