Editing XML

Being new to XML, I had to decide upon a suitable editor. You can use any editor capable of editing ASCII text files, but you'll find that a editor which is smart makes editing XML files much more pleasant. At the time of this writing, I still prefer emacs.

For those that prefer the vi environment (and you know who you are), both vim and gvim provide nice color highlighting as shown below:

Unfortunately, color highlighting is about all vim and gvim offer. Once you get use to working with a powerful editor, you'll only find yourself using these tools in a pinch.

For those that prefer the lack of a slow GUI interface, the emacs editor is well suited for editing XML documents. It is very good at aiding the developer in inserting new elements into the document (or editing existing elements). The following shows that emacs is also capable of color syntax highlighting:

Unfortunately, emacs (at least version 21.2.1) doesn't do this out of the box. You will need to update your .emacs file. In addition, to suport XML validation, you will want to create a $HOME/.catalog file. Please note, this has only been tested on version 21.2.1 of emacs. The directions shown here will NOT work with version 20.7.1 of emacs.

The following shows what you should add to the end of your .emacs configuration file:

Figure 2.2. Adding XML Support to $HOME/.emacs

;; Enable sgml-mode for our editing of XML documents

(autoload 'sgml-mode "psgml" "Major mode to edit SGML files." t )
(require 'psgml)

;; You can add devloper catalogs (avoiding the need to update
;; /etc/sgml/catalog file), by keeping MASTER catalog file
;; $HOME/.catalog with lines indicating package catalog files to load
;; (in OASIS form). File contents has one or more lines resembling:
;;
;; CATALOG "/home/pkb/usr/share/keyval/catalog/keyval_catalog.oasis"

(setq-default sgml-catalog-files
      '("/etc/sgml/catalog"
	"~/.catalog"))

;;  Create faces for the various components of a XML document

(copy-face 'default 'sgml-start-tag-face)
(set-face-foreground 'sgml-start-tag-face "#4856ad")

(copy-face 'default 'sgml-end-tag-face)
(set-face-foreground 'sgml-end-tag-face "#72398e")

(copy-face 'italic 'sgml-comment-face)
(set-face-foreground 'sgml-entity-face "#298921")

(copy-face 'bold 'sgml-pi-face)
(set-face-foreground 'sgml-comment-face "red")

(copy-face 'bold 'sgml-sgml-face)
(set-face-foreground 'sgml-pi-face "darkgray")

(copy-face 'bold 'sgml-doctype-face)
(set-face-foreground 'sgml-doctype-face "#8e3939")

(copy-face 'default 'sgml-entity-face)
(set-face-foreground 'sgml-entity-face "#77502b")

(copy-face 'bold 'sgml-shortref-face)
(set-face-foreground 'sgml-shortref-face "#d8883c")

;; Map the new faces when psgml mode (xml).

(setq-default sgml-set-face t)

(setq sgml-markup-faces '((start-tag    . sgml-start-tag-face)
                          (end-tag      . sgml-end-tag-face)
                          (comment      . sgml-comment-face)
                          (pi           . sgml-pi-face)
                          (sgml         . sgml-sgml-face)
                          (doctype      . sgml-doctype-face)
                          (entity       . sgml-entity-face)
                          (shortref     . sgml-shortref-face)))

The pgsml-mode of emacs is capable of reading the DTD associated with your XML source file (provided it includes a valid DOCTYPE statement). This is really nice when editing the source XML. Unfortunately, this hits on one of the sore points when editing XML documents. It's very difficult to provide a single location for everyone in the world to get to the DTD which is used to validate a XML document. Catalogs are used to provide a public mapping to local file system to address this issue (its a pain in the butt - I know).

The following shows how I've configured my $HOME/.catalog file to load the catalog file provided by the xml-keyval package:

The jEdit - Open Source Text Editor editor is both free and a fairly powerful editor for working with XML documents. It provides a full GUI interface and is able to validate your XML document as you type. You will need to download the necessary plug-ins - but the plug-in extension is built into the editor and pretty close to painless. You can customize its appearance (tweak what views are shown and how they are placed). The following shows an example: