How to pretty print XML from the command line?

Cover Image for How to pretty print XML from the command line?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

How to Pretty Print XML from the Command Line? 🖥️👨‍💻

Have you ever struggled with ugly, unreadable XML output on the command line? 🤔 Want to transform it into a beautiful, human-readable format? Look no further! In this guide, we'll show you some easy solutions to pretty print XML from the command line. 😄

The Problem 🚫🗒️

Let's say you have an XML file like this:

<root>
    <foo a="b">lorem</foo>
    <bar value="ipsum" />
</root>

But when you view it in the command line, it looks like a jumbled mess. 😫 You want it to appear neatly formatted, like this:

<root>
    <foo a="b">lorem</foo>
    <bar value="ipsum" />
</root>

Solution 1: XMLStarlet 🌟

One of the easiest ways to pretty print XML from the command line is by using XMLStarlet, a command-line XML toolkit. 🌟 XMLStarlet is a powerful tool that allows you to manipulate XML files, including formatting them nicely.

Here's how you can use XMLStarlet to pretty print your XML:

  1. Install XMLStarlet by running the following command:

sudo apt-get install xmlstarlet
  1. Save your XML content in a file, for example, input.xml.

  2. Run the following command to pretty print the XML file using XMLStarlet:

xmlstarlet fo input.xml

That's it! XMLStarlet will format the XML file, making it much easier to read on the command line. 😍

Solution 2: XSL Transformations 🎯

Another way to pretty print XML from the command line is by using XSL Transformations. XSLT is a language used to transform XML documents into different structures or formats. 📝

  1. Save the following XSLT template in a file, for example, pretty-print.xsl:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:template match="/">
    <xsl:copy-of select="."/>
  </xsl:template>
</xsl:stylesheet>
  1. Save your XML content in a file, for example, input.xml.

  2. Run the following command to pretty print the XML file using XSL Transformations:

xsltproc pretty-print.xsl input.xml

Just like that, your XML will be transformed into a beautifully formatted version. 👌

Call-to-Action: Share Your Experiences! 📢🔄

We hope these solutions helped you in pretty printing XML from the command line. Now, it's your turn! Share your experiences:

  • Have you encountered any other methods to achieve pretty printing?

  • Do you prefer using XMLStarlet or XSL Transformations?

  • What other command-line tricks do you want to learn about?

Leave a comment below and let's have a discussion! 🎉 Engage with our tech community and help each other out. Together, we can make the command line a more beautiful and accessible place. 🌈💻

Remember, sharing is caring! If you found this guide helpful, don't forget to share it with your friends and colleagues. Happy pretty printing! 🎇🌟


Related: How can I pretty-print JSON in (Unix) shell script?


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello