24

How can I highlight XML from stdin (e.g. piped from xmllint --format) to stdout?

I know how to get highlighting working in nano and view, but is there something that just outputs to stdout and exits?

What I'd link to do is just type something like

xmllint --format xmlfile.xml | some-highlighter

or maybe, for big files

xmllint --format xmlfile.xml | some-highlighter | less

and get pretty output.

Sietse
  • 574

5 Answers5

21

Supercat and grcat (grc) can do piped syntax highlighting. You'll probably have to make or find configuration files for XML. They are both available in the Ubuntu repositories as well as at the links provided.

Pygmentize has XML highlighting included. It's available as "python-pygments" in the Ubuntu repositories or by using easy_install Pygments.

xmllint --format xmlfile.xml | pygmentize -l xml | less
15

This is how you do it using GNU source-highlight and less:

source-highlight -i /tmp/foo.xml -f esc | less -r
Pete
  • 317
user78659
  • 151
9

I use bat:

xmllint --format - | bat -pP

-p removes all decoration (line numbers, etc.), -P disables the pager. Update: use -pp with newer versions of bat.

bat: https://github.com/sharkdp/bat

sebnukem
  • 191
8

I found highlight in Homebrew for OSX, and I'm sure it's available in the Ubuntu repository. It does highlighting and output to a number of formats, including terminal output.

Sietse
  • 574
0

You can try my cli tool xmq (https://libxmq.org) to pretty print and syntax highlight XML and HTML. Note that it renders the XML/HTML/JSON in the XMQ format which is easier to read and edit. There is however a 1-1 mapping between XMQ and XML. The tool is very useful for analyzing large xml and html files.

The xmq tool also includes a pager for the terminal: xmq xmlfile.xml page

The tool can also render into a temporary html file which is automatically viewed in your default browser: xmq xmlfile.xml browse

It picks the color scheme from your terminals background color (light or dark), but you can override it:

XMQ_BG=dark xmq xmlfile.xml browse

XMQ_BG=light xmq xmlfile.xml browse

It works in a pipeline as well: curl https://slashdot.org | xmq delete //script delete //style page

Apart from deleting xpath matched nodes, there are other commands to convert to and from xmq/xml/html/json and apply transformations to the content.