I am searching for an XSLT or command-line tool (or C# code that can be made into a command-line tool, etc) for Windows that will do XML pretty-printing. Specifically, I want one that has the ability to put attributes one-to-a-line, something like:
<Node>
<ChildNode
value1='5'
value2='6'
value3='happy' />
</Node>
It doesn't have to be EXACTLY like that, but I want to use it for an XML file that has nodes with dozens of attributes and spreading them across multiple lines makes them easier to read, edit, and text-diff.
NOTE: I think my preferred solution is an XSLT sheet I can pass through a C# method, though a Windows command-line tool is good too.
Here's a small C# sample, which can be used directly by your code, or built into an exe and called at the comand-line as "
myexe from.xml to.xml
":Sample input:
Sample output (note you can remove the
<?xml ...
withsettings.OmitXmlDeclaration
):Note that if you want a string rather than write to a file, just swap with
StringBuilder
:Here's a PowerShell script to do it. It takes the following input:
...and produces this as output:
Here you go:
Take that script, save it as C:\formatxml.ps1. Then, from a PowerShell prompt type the following:
This script is basically just using the .NET framework so you could very easily migrate this into a C# application.
I hope that's useful to you.
You can implement a simple SAX application that will copy everything
as is
and indent attributes how you like.UPD:
SAX stands for
Simple API for XML
. It is a push model of XML parsing (a classical example of Builder design pattern). The API is present in most of the current development platforms (though native .Net class library lacks one, having XMLReader intead)Here is a raw implementation in python, it is rather cryptic but you can realize the main idea.
Try Tidy over on SourceForge. Although its often used on [X]HTML, I've used it successfully on XML before - just make sure you use the
-xml
option.http://tidy.sourceforge.net/#docs
People have ported to several platforms and it available as an executable and callable library.
Tidy has a heap of options including:
http://api.html-tidy.org/tidy/quickref_5.0.0.html#indent
One caveat:
But I suspect unless your XML is really advanced, the tool should work fine.
Just use this xslt:
Or, as another option, here is a perl script: http://software.decisionsoft.com/index.html
There is a tool, that can split attributes to one per line: xmlpp. It's a perl script, so you'll have to install perl. Usage:
You can also determine the ordering of attributes by creating a file called attributeOrdering.txt, and calling
perl xmlpp.pl -s -t input.xml
. For more options, useperl xmlpp.pl -h
I hope, it doesn't have too many bugs, but it has worked for me so far.