Editing XML with Emacs

Dag-Erling Smørgrav des at linpro.no
Wed Feb 22 14:19:46 CET 2006

The default editing mode for XML files in FSF Emacs is James Clark's
sgml-mode.  There are many disadvantages to sgml-mode: it doesn't
really grok XML very well (it was designed for SGML), it doesn't grok
encodings, etc.

James Clark has written a new Emacs mode specifically for XML, called
nXML (nxml-mode).  It is not part of the FSF Emacs distribution (yet),
but it is available in both Ubuntu and FreeBSD as nxml-mode (or
editors/nxml-emacs21 if you want to build it from ports).  It has much
better syntax highlighting than sgml-mode, and proper indentation and
auto-fill support, which sgml-mode lacks completely.

Far more importantly, nxml-mode is schema-aware.  This means that it
offers completion of tags, namespace URIs etc., and can detect and
visualize (normally with red underlining) errors in the document.  All
this requires, of course, that nxml-mode knows the schema you are
using; it currently supports DocBook, XHTML, XSLT, RDF and a few

The only customization I have found necessary to feel comfortable with
nxml-mode is the following:

(defun des-nxml-mode-hook ()
  (local-set-key "\C-m" 'newline-and-indent)
  (local-set-key "\C-c\C-e" 'nxml-finish-element))
(add-hook 'nxml-mode-hook 'des-nxml-mode-hook)

I *highly* recommend nxml-mode for editing DocBook documentation for
Varnish - so much so that I'm considering making nxml-mode's default
formatting the required formatting style for DocBook.

Dag-Erling Smørgrav
Senior Software Developer
Linpro AS - www.linpro.no

More information about the varnish-misc mailing list