Книга: Learning GNU Emacs, 3rd Edition

8.4.3 Writing XHTML Using nxml Mode

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 <html> 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-Enter completes 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 </body>.


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: </


Emacs inserts a closing tag.

Note that just typing </ was adequate to insert a closing tag for the current element. We don't need to type C-Enter to 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-7 lists some of the commands available in nxml mode.

Table 8-7. Nxml mode commands

Keystrokes Command name Action
C-Enter nxml-complete Complete the current tag.
/ nxml-electric-slash Add a closing tag for the last open element.
C-c C-n rng-next-error Move to the next error.
C-c C-l rng-save-schema-location Creates (or updates) a file called schemas.xml in your home directory. This file associates schemas with files.
C-c C-s rng-set-schema-and-validate Set the schema and validate against it.
C-c C-a rng-auto-set-schema Set the schema automatically according to the contents of the file.
C-c C-w rng-what-schema Show in the minibuffer the current schema associated with this file.
C-c C-v rng-validate-mode Toggles whether the mode line indicates that the file is valid or invalid.
C-c C-u nxml-insert-named-char Insert a named character; press Tab to see a list.
(none) nxml-insert-xml-declaration Insert an XML declaration at the beginning of the file.
C-c Tab nxml-balanced-close-start-tag-inline Insert the ending tag for the starting tag you are typing, putting the ending tag on the current line.
C-c C-b nxml-balanced-close-start-tag-block Insert the ending tag for the starting tag you are typing, putting the ending tag on a separate line.
C-c C-f nxml-finish-element Finish the current element.
M-h nxml-mark-paragraph Mark the current paragraph.
M-} nxml-forward-paragraph Move forward one paragraph.
M-{ nxml-backward-paragraph Move back one paragraph.
C-M-p nxml-backward-element Move back one element.
C-M-n nxml-forward-element Move forward one element.
C-M-d nxml-down-element Move down one element (if nested).
C-M-u nxml-backward-up-element Move up one element (if nested).

Оглавление книги


Генерация: 1.471. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз