How can I automatically format an XML file?
October 31, 2004 1:14 PM   Subscribe

XMLFilter: I need to format an XML file nicely, with the correct indentation and so forth. Unfortunately the file is all on one line. Any suggestions for a Windows user?

I've been trying to get XML Cooktop to work, but if I open the entire file, I get a CCRYSTALTEXTVIEW error in the application, and if I cull the file down to only one record, and choose Tools > Format XML with indent attributes, it does nothing. I've tried copying from Firefox and IE, but that doesn't work neither.
posted by John Shaft to Computers & Internet (4 answers total)
 
xmlindent will do the job nicely, but I'm not sure about running it on a Windows box.
posted by Nelson at 1:23 PM on October 31, 2004


even just opening and editing an xml file with something like microsoft xmlNotepad usually results in it formatting the file for you.
posted by Hackworth at 3:36 PM on October 31, 2004


I would imagine you can get xmlindent to run under Cygwin.
posted by grouse at 3:57 PM on October 31, 2004


Run it through a simple XSL transformation.

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:param name="indent-increment" select="' '" />

<xsl:template match="*">
<xsl:param name="indent" select="'
'"/>

<xsl:value-of select="$indent"/>
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates>
<xsl:with-param name="indent"
select="concat($indent, $indent-increment)"/>
</xsl:apply-templates>
<xsl:value-of select="$indent"/>
</xsl:copy>
</xsl:template>

<xsl:template match="comment()|processing-instruction()">
<xsl:copy />
</xsl:template>

<!-- WARNING: this is dangerous. Handle with care -->
<xsl:template match="text()[normalize-space(.)='']"/>

</xsl:stylesheet>

posted by five fresh fish at 6:08 PM on October 31, 2004


« Older DivorceFilter   |   Removing unwanted animal residents in our attic Newer »
This thread is closed to new comments.