Open Office XML: Introduction to WordprocessingML

Previously, we were introduced to Open Office XML. The ability to leverage OOXML in our applications will allow us to create documents, spreadsheets, and presentations easily opened and usable in the most popular office applications. The most common of these applications is the word processor, and we’ll introduce ourselves to the world in this tutorial.

A WordprocessingML package contains a main part that strongly resembles an HTML document structure. A body contains one to many paragraphs, which contain one to many runs. A run is akin to an HTML span, which encapsulates a segment of text that formatting can be applied to. In fact, a style is used to repeat formatting across various runs and/or paragraphs, much like CSS does in HTML.

As mentioned in our introduction, a final OOXML document is actually a collection of files that make up the document. Here is the structure we’ll be creating today:

/[Content_Types].xml
  /_rels/.rels
  /document.xml

This structure is actually the minimum required by OOXML, and when we begin to do more advanced layouts, we’ll need a much more complex structure. The [Content_Types].xml file defines what types of files this package contains. For our example:

<Types  xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
  <Default Extension="rels"  ContentType="application/vnd
  .openxmlformats-package.relationships+xml"/>
  <Override PartName="/document.xml"  ContentType="application/vnd
  .openxmlformats-officedocument.wordpressingml.document.main+xml"/>
  </Types>

The Default Extension element will tell the package to use the specified ContentType for any file with the specified extension. Override PartName will explicitly define what ContentType should be used for a given file (part). In our example, we have defined that every file with the .rels extension is a relationship, and the document.xml file is a WordprocessingML main part.

Next, we’ll define our single relationship in our "Hello, world" document:

<Relationships
xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/
  officeDocument/2006/relationships/officeDocument"  Target="document.xml"/>
  </Relationships>

Finally, we’ll build our document.xml:

<w:document>
  <w:body>
  <w:p>
  <w:r><w:t>Hello,  world</w:t></w:r>
  </w:p>
  </w:body>
  </w:document>

Now, we zip up our files as a .docx file, and we can open it up any OOXML compliant word processor! Next article, we’ll look at more advanced uses to format our document.

Michael Marr
About Michael Marr
Michael Marr is a staff writer for WebProNews

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>