This is a mobile version, full one is here.

Yegor Bugayenko
29 August 2017

XCOP—XML Style Checker

One of the biggest advantages of XML versus many other data formats is that it is human-readable. Well, to some extent, you may say. Indeed, a badly formatted XML document may be rather difficult to digest. I’m not talking about XML validity now, but about its formatting style. Just like we format our Java/Ruby/Python nicely and then check their “prettiness” with static analyzers, we can also check our XML documents. Six years ago I asked the Stack Overflow community for such a tool, but unfortunately my question was down-voted and closed (you will need 10K+ reputation to see it). Last week I finally decided to create a tool myself and I called it xcop.

It’s a very simple command line Ruby gem. First, you install it:

$ gem install xcop

And then you ask it to check your XML file, say pom.xml:

$ xcop pom.xml

If the file is not “pretty,” xcop will complain and show what’s wrong. You can ask xcop to fix the file:

$ xcop --fix pom.xml

Moreover, in most cases you may need your XML files to include a license in their headers, especially if it’s open source. To enforce that, just point xcop to the file with the license:

$ xcop --license=LICENSE.txt pom.xml

I believe it’s good practice to use xcop together with Checkstyle (for Java files), Rubocop (for Ruby files), and other static analyzers, to ensure that your XML files always look pretty.

Read how you can integrate xcop with Rake, Maven, and other builders. I will appreciate it if you contribute your own integrations too.