Fandom

Vim Tips Wiki

Automatically indent an XML file using XSLT

Redirected from VimTip551

1,624pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Tip 551 Printable Monobook Previous Next

created September 5, 2003 · complexity advanced · author yijun · version 5.7


Do you want to indent an XML file? Try the following XSLT (say file named indent.xsl):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" indent="yes"/>
 <xsl:strip-space elements="*"/>
 <xsl:template match="/">
  <xsl:copy-of select="."/>
 </xsl:template>
</xsl:stylesheet>

In Vim, whenever a file with the ".xml" extension is loaded into the buffer, you can try the following in your vimrc to trigger the XSLT for filtering the buffer contents:

if version >= 540
  augroup filetype
    autocmd FileType xml '[,']!xsltproc indent.xsl %
  augroup END
endif
" other autocmds
if version>540
  autocmd!
endif

Next time when you load an XML file in Vim, it will be indented automatically.

xsltproc is a command line tool that is part of the gnome libxslt. See http://xmlsoft.org/XSLT.html

CommentsEdit

Using an XSLT which copies the document (not the file) is a common way to get pretty-printed XML.

Here are some more tips, variations, and alternatives:


If you don't have a copy of XSLT, you can use the following to indent XML/HTML:

:%s/></>\r</g
gg=G

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" indent="yes"/>
 <xsl:strip-space elements="*"/>
 <xsl:template match="/">
  <xsl:copy-of select="."/>
 </xsl:template>
</xsl:stylesheet>

Also on Fandom

Random Wiki