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 StackOverflow 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:
And then you ask it to check your XML file, say
If the file is not "pretty,"
xcop will complain and show what's wrong. You can ask
xcop to fix the file:
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:
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.