Linkwerk Logo

How to set up Emacs/PSGML for editing XML and XHTML

Although there are a lot of tutorials for Emacs and PSGML available online, it took some time to get things work the way I want. I began to use Emacs/PSGML in the early nineties (past millenium, maybe you remember) and wrote a lot of SGML documents, including HTML. Nowadays every new web page I write is XHTML. Fortunately Lennart Staflin, the author of PSGML, added an XML mode in PSGML. Here's how to get it running. If you're an expert: Don't waste your time; you already know this. If you're new to Emacs/PSGML/XML this might help you. Anyway, this will help me at least, if I work on a new machine someday...

  1. First of all you should have both software, Emacs and PSGML. Install it according to the installation manual.

  2. Set up your .emacs file to autoload the xml-mode and switch to that mode, depending on the filename extension.

    (autoload 'xml-mode "psgml" "Major mode to edit XML files." t)
    (setq auto-mode-alist
             '(("\\.html$" . xml-mode))
             '(("\\.xml$" . xml-mode))
  3. If you like syntax coloring add some faces definitions to your .emacs:

    (add-hook 'xml-mode-hook   ; XML-specific settings
            (function (lambda()
    ; faces creation
    (make-face 'sgml-comment-face)
    (make-face 'sgml-start-tag-face)
    (make-face 'sgml-end-tag-face)
    (make-face 'sgml-doctype-face)
    ; faces definitions
    (set-face-foreground 'sgml-comment-face "SeaGreen")
    (set-face-foreground 'sgml-start-tag-face "OrangeRed")
    (set-face-foreground 'sgml-end-tag-face "OrangeRed")
    (set-face-foreground 'sgml-doctype-face "MintCream")
    ; markup to face mappings
    ; (see for details)
    (setq sgml-markup-faces
          '((comment . sgml-comment-face)
            (start-tag . sgml-start-tag-face)
            (end-tag . sgml-end-tag-face)
            (doctype . sgml-doctype-face)
    ; turn faces on
    (setq sgml-set-face t)

    If you like you can add more XML mode specific settings in this section.

  4. Now you have Emacs set up to work in XML mode for files with an extension of .xml or .html. What's missing is the context sensitive editing feature of Emacs/PSGML. That's because Emacs can't find your DTDs yet.

    You now need a DTD for the document type you want to edit. Let's assume that you want to write XHTML 1.0 documents. Retrieve the DTDs, entity sets and the catalog from the XHTML 1 specification. The catalog is an SGML Open Catalog, which allows a software to map public identifiers (such as -//W3C//DTD XHTML 1.0 Strict//EN) to system identifiers (such as a local file name). Unzip the set of XHTML-1-DTDs to any directory.

    Finally set the environment variable SGML_CATALOG_FILES to point to your catalog file. For example:


    If you're using several XML doctypes you will probably have more than one catalog file. You can have one master catalog file, which includes the others with the CATALOG directive (follow the SGML Open Catalog link above to get a syntax description).

  5. Open a new document, such as test.html, and insert the following lines:

    <?xml version="1.0"?>
    <!DOCTYPE html
         PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

    Place the cursor on a new line below the doctype declaration and type C-c C-e. Have fun :-)

Now you should have a free, open source XML editing environment with context sensitive editing features and syntax coloring. PSGML has some more features, such as validating with an external parser. You'll find details in the PSGML manual.

BTW: There's an alternative for PSGML. James Clark wrote nXML, an XML editing mode for GNU Emacs. nXML validates the document against a Relax NG schema, which is fine, but not what I want for web pages. Sometimes I like to include these pretty little buttons at the bottom of my web pages to show: Yes, I've read specs and understand them. You can click on the button below and — if I did not make a mistake — W3C's validator should tell you, that everything's ok with this file. But, at the time of writing this, this works only with DTDs, not with Relax NG schemas.

Finally, if you'd like to see how XML editing in (GNU) Emacs looks like, here's a screenshot (do not wonder about the Windows like window. Emacs is running on a Unix machine, accessed from a Windows client machine):

Screenshot of Emacs/PSGML
© Stefan Mintert

Valid XHTML 1.0!