8.4.1 Writing XML with SGML Mode
Emacs's own SGML mode provides support for entering tags. We covered much of this earlier under HTML mode, so we provide just one brief example here. Inserting, hiding, and showing tags are especially helpful features provided by SGML mode.
Let's look at a chapter on enumerated types by Java in a Nutshell author David Flanagan. This chapter uses the DocBook DTD.
Initial state:

Editing a document that uses the DocBook DTD (Mac OS X).
Note that Emacs displays XML on the mode line. XML mode in this context is a subset of SGML mode. Actually, despite this name, all the commands in this mode start with sgml, not xml. The menu of relevant commands is called SGML as well. Emacs doesn't pretend to have extensive XML support.
We want to insert a paragraph before the first paragraph.
Add a blank line following the title and type: C-c C-t

Emacs inserts an open angle bracket and prompts for the tag name (Mac OS X).
Type: para Enter

Emacs inserts opening and closing paragraph tags (Mac OS X).
Note that Emacs is not following our indentation style. We can correct it by moving to the beginning of the line and pressing Tab. See Table 8-4earlier in this chapter for details on SGML mode commands.
8.4.2 TEI Emacs: XML Authoring for Linux and Windows
The Text Encoding Initiative (TEI) wanted an XML authoring environment for Emacs, so it created (the somewhat misleadingly named) TEI Emacs. [50]Despite its name, TEI Emacs does not include Emacs itself. Rather, it creates an authoring environment for writing XML using nxml mode or psgml mode. It incorporates XSLT tools, along with most of the standard DTDs, such as the three forms of XHTML DTDs (strict, frameset, and transitional), DocBook DTDs, and more. Naturally, the TEI's own DTDs and schemas are also included.
The active development of this tool and its careful packaging led us to describe this tool despite the fact that it is limited to Linux and Windows at this writing. [51]You should have Emacs 21.3 already installed before you install this tool. Installing TEI Emacs is trivial. The Windows version has an installer, and Linux users follow simple instructions at http://www.tei-c.org/Software/tei-emacs/, the web site for downloading TEI Emacs.
8.4.3 Writing XHTML Using nxml Mode
James Clark, an XML pioneer, wrote nxml mode to provide Emacs support for his schema standard RELAX NG. For details on the standard, visit http://www.relaxng.org/ or pick up a copy of RELAX NG by Eric van der Vlist (O'Reilly). The important thing about nxml mode is that it validates text as you type instead of making validation and debugging separate steps.
If you did not install TEI Emacs, you can download nxml mode and its schemas from http://thaiopensource.com/download/. If you decide to become an active nxml mode user, you may want to join a related Yahoo Group discussion list (see http://groups.yahoo.com/group/emacs-nxml-mode/).
In this section, we change our running HTML example to XHTML, first using a RELAX NG schema and nxml mode. Open dickens.html , then enter nxml mode.
Type: C-x C-f dickens.html Enter M-x nxml-mode Enter

Editing dickens.html in nxml mode.
nxml mode tells you what schema it is using in the minibuffer. It's smart enough to know that its XHTML schema is best for this purpose.
The mode line tells us that this file is currently invalid. Emacs highlights errors with red underscores. Let's deal with these errors one at a time.
Move the cursor to the red underscore at the end of the html tag.

The minibuffer describes what's missing.
Editing XHTML with a schema requires a namespace definition in the tag. nxml mode knows what we need. This is a good time to use nxml's completion feature to let it supply the details for us. C-Entercompletes the current tag.
Type: Space xmlns=" C-Enter

Emacs inserts the rest of the namespace declaration.
The mode line tells us that this file is still invalid. Moving to the underlined address tag gives us a fairly cryptic reason; it says, Element not allowed in this context
. Let's move down to the closing body tag to see if that error provides any more insight into the problem.
Move to
.

The minibuffer says Missing end-tag "p"
.
This message provides a clue. Although HTML authors are not accustomed to adding closing tags to paragraphs, XHTML requires them. Let's insert a closing tag after our paragraph.
Move to the line following the Dickens paragraph and type: </</strong>

Emacs inserts a closing tag.
Note that just typing </</strong>was adequate to insert a closing tag for the current element. We don't need to type C-Enterto invoke completion. That's because in nxml mode, slash is bound to nxml-electric-slash. It automatically completes the nearest open element, another shortcut for us.
A similar command is C-c C-f(for nxml-finish-element). With C-c C-f, you don't have to type anything; it inserts the relevant closing tag for you.
Look at the mode line now. It says valid. Using nxml mode, it's not too tough to take an HTML file and change it to valid XHTML.
Validating text as you type it is a key feature of nxml mode. It's validating against a schema. To specify a different schema, type C-c C-s(for rng-set-schema-and-validate). The minibuffer prompts for the file where the schema resides. A number of schemas can be found online at http://www.relaxng.org/#schemas. You can also convert DTDs to schemas using tools listed on that page.
Your menus vary depending on whether you install nxml mode directly or whether you use TEI's version. TEI provides support for encoded characters using the UniChar menu. It also provides extensive XSLT support. TEI's NXML menu includes some TEI skeletons as well as nxml mode options. Nxml mode installed from thaiopensource.org includes an XML menu with options for setting the schema and customizing the mode. Table 8-7lists some of the commands available in nxml mode.
Читать дальше