XML technologies / XML / XML in a Nutshell / XML as a document format / WordprocessingML

(Excerpt from “XML in a Nutshell” by Elliotte Rusty Harold & W. Scott Means)

With Microsoft Office 2003 for Windows (but not Office 2004 for Mac), Microsoft gave Word and the other Office components the ability to save all documents as XML. They still use a binary format by default, however. The XML application saved by Microsoft Word is called WordprocessingML ( WordML for short). Unlike DocBook, TEI and OpenOffice, which were designed from scratch without accounting for predecessor formats, WordprocessingML was conceived more as an XML representation of an existing binary file format. This makes it a highly unusual example of a narrative document format. We would not recommend imitating its design in your applications. Nevertheless, comparing it with the other three formats can be instructive.

The following code example, A WordprocessingML document, shows the same document as the previous three examples, encoded this time in WordprocessingML. This version seems the most opaque and cryptic of the formats examined in this chapter. It makes clear that XML is not magic fairy dust you can sprinkle over an existing format to produce clean, readable, maintainable data.

The root element of a WordprocessingML document is w:wordDocument. The prefix w is mapped here to the namespace URI "http://schemas.microsoft.com/office/word/2003/wordml". Several other namespaces are declared for other content that can be embedded in a Word file.

This root element can contain various pieces of metadata. I used three here: o:DocumentProperties for basic metadata such as author and title, a w:fonts element listing fonts and their metrics, and a w:styles element listing the styles referenced in the document. All are optional. A document saved by Word itself, however, would include all of them and several others.

The actual document content is stored in a w:body element. The body is divided into sections (wx:sect elements), which can in turn be divided into subsections (wx:subsection elements). Unusually, these are entirely optional: removing them would have no effect. They exist mainly for human convenience. The document’s actual structure comes not from sections and subsections, but from paragraphs with outline levels.

Three basic text elements occur inside a WordprocessingML body: w:t, w:r and w:p. w:t is for text, w:r for text spans such as span in HTML, and w:p for paragraphs. A w:p element contains w:r elements, each containing a w:t element. Neither w:r nor w:p elements can directly contain text. Inside w:t elements, whitespace is significant, but in most other elements it is not. Line breaks are treated as equivalent to spaces, matching the usual word-wrapping behavior of Word and most other word processors.

Apart from these and a few other elements, WordprocessingML markup gives no content information. Instead, many characters are spent reproducing page appearance exactly, including fonts, metrics, styles, line breaks, and so forth. In a document saved by Word, unlike this handwritten example, presentation information can easily occupy dozens of times more space than the content. Headings are not marked by a dedicated heading element: an outline-level property is set using a preceding sibling element w:pPr with a child element w:outlineLvl. Using siblings rather than attributes or parents to set properties is a very unusual pattern, poorly supported by most XML processing tools.

<?xml version="1.0" encoding="UTF-8"?>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:o="urn:schemas-microsoft-com:office:office" xml:space="preserve">
  <o:DocumentProperties>
    <o:Title>XML in a Nutshell</o:Title>
    <o:Author>W. Scott Means</o:Author>
    <o:LastAuthor>Elliotte Rusty Harold</o:LastAuthor>
    <o:Revision>2</o:Revision>
    <o:TotalTime>0</o:TotalTime>
    <o:LastPrinted>1601-01-01T04:00:00Z</o:LastPrinted>
    <o:Created>2004-05-25T00:40:00Z</o:Created>
    <o:LastSaved>2004-05-25T00:40:00Z</o:LastSaved>
    <o:Pages>1</o:Pages>
    <o:Words>162</o:Words>
    <o:Characters>925</o:Characters>
    <o:Company>Cafe au Lait</o:Company>
    <o:Lines>7</o:Lines>
    <o:Paragraphs>2</o:Paragraphs>
    <o:CharactersWithSpaces>1085</o:CharactersWithSpaces>
    <o:Version>11.4920</o:Version>
  </o:DocumentProperties>
  <w:fonts>
    <w:defaultFonts w:ascii="Times New Roman" w:fareast="Times New Roman" w:h-ansi="Times New Roman" w:cs="Times New Roman"/>
    <w:font w:name="Helvetica"><w:panose-1 w:val="020B0604020202030204"/>
      <w:charset w:val="00"/>
      <w:family w:val="Swiss"/>
      <w:pitch w:val="variable"/>
      <w:sig w:usb-0="20003A87" w:usb-1="00000000" w:usb-2="00000000" w:usb-3="00000000" w:csb-0="000001FF" w:csb-1="00000000"/>
    </w:font>
  </w:fonts>
  <w:styles>
    <w:style w:type="character"  w:styleId="emphasis"  w:default="off"/>
  </w:styles>
  <w:body>
  <wx:sect>
     <w:p>
       <w:pPr>
          <w:outlineLvl w:val="0" />
       </w:pPr>
       <w:r>
         <w:t>Einführung in XML</w:t>
       </w:r>
     </w:p>
    <w:p></w:p>
  </wx:sect>

  <wx:sect>
     <w:p>
       <w:pPr>
          <w:outlineLvl w:val="0" />
       </w:pPr>
       <w:r>
         <w:t>XML als Dokumentformat</w:t>
       </w:r>
     </w:p>

   <w:p>
     <w:r>
       <w:t>XML ist vor allen Dingen ein Dokumentformat.Es war immer für Webseiten, Bücher, wissenschaftliche Artikel, Gedichte, Kurzgeschichten, Referenzhandbücher, Anleitungen, Lehrbücher, Plädoyers, Verträge, Gebrauchsanweisungen und andere Dokumente gedacht, die von Menschen gelesen werden. Sein Einsatz als Syntax für Computerdaten in Anwendungen wie Bestellabwicklung, Objektserialisierung, Datenbanken sowie elektronischen Datenaustauschsystemen ist eigentlich nur ein glücklicher Zufall.</w:t>
     </w:r>
   </w:p>

   <wx:subsection>
     <w:p>
       <w:pPr>
          <w:outlineLvl w:val="1" />
       </w:pPr>
       <w:r>
         <w:t>Das Vermächtnis von SGML</w:t>
       </w:r>
     </w:p>
     <w:p></w:p>
   </wx:subsection>
   <wx:subsection>
     <w:p>
       <w:pPr>
          <w:outlineLvl w:val="1" />
       </w:pPr>
       <w:r>
         <w:t>TEI</w:t>
       </w:r>
     </w:p>
     <w:p></w:p>
   </wx:subsection>

   <wx:subsection>
     <w:p>
       <w:pPr>
          <w:outlineLvl w:val="1" />
       </w:pPr>
       <w:r>
         <w:t>DocBook</w:t>
       </w:r>
     </w:p>
     <w:p>
       <w:hlink w:bookmark="http://www.docbook.org/">
         <w:r>
            <w:rPr>
               <w:rStyle w:val="Hyperlink" />
            </w:rPr>
            <w:t>DocBook</w:t>
         </w:r>
       </w:hlink>
       <w:r>
         <w:t>ist eine SGML-Anwendung, die für neue Dokumente, jedoch nicht für alte entworfen wurde. Es ist besonders bei Computer-Dokumentationen gebräuchlich. Verschiedene O'Reilly-Bücher wurden in DocBook geschrieben, einschließlich </w:t>
       </w:r>
       <w:r>
         <w:rPr>
            <w:rStyle w:val="emphasis"/>
         </w:rPr>
         <w:t>Norm Walsh und Leonard Muellners DocBook: The Definitive Guide</w:t>
      </w:r>
     <w:r>
       <w:t>. Ein Großteil des Texts des </w:t>
       </w:r>
       <w:hlink w:bookmark="http://www.linuxdoc.org/">
         <w:r>
            <w:rPr>
               <w:rStyle w:val="Hyperlink" />
            </w:rPr>
            <w:t>Linux Documentation Project (LDP)</w:t>
         </w:r>
         </w:hlink>
         <w:r>
       <w:t> wurde in DocBook geschrieben.</w:t>
       </w:r>
     </w:p>
   </wx:subsection>

  </wx:sect>

  <wx:sect>
     <w:p>
       <w:pPr>
          <w:outlineLvl w:val="0" />
       </w:pPr>
       <w:r>
         <w:t>XML im Web</w:t>
       </w:r>
     </w:p>
    <w:p></w:p>
  </wx:sect>
  </w:body>
</w:wordDocument>

Code example: A WordprocessingML document

Two things strike me about this format. First, the cryptic nature of short tag names such as t, r, p or the incredibly verbose rPr. Second, the large number of tags needed to mark up this rather simple document. The problem seems to be that Word bundles all style definitions with the document, then repeats styles for every paragraph even if used throughout. XML need not be verbose, but this example certainly is, although still less so than files I have seen Word 2003 actually save. DocBook and TEI are human-readable even as plain text. OpenOffice and WordprocessingML are not really, especially in their natural state.

  

 

 

 

back to top