Betsy Rolland
LIS 600 Independent Study

Transforming XML to Microsoft XML
Terry Brooks
Winter 2006

Adding Styles to WordprocessingML

The instructions below will demonstrate how to add styles to your WordprocessingML so you can control how your text is displayed when the document is opened in Microsoft Word.

For complete coverage of adding styles to MicrosoftML, please see Office 2003 XML by Evan Lenz, Mary McRae & Simon St. Laurent (2004).

There are four possible style types in Word:

If no style is explicitly defined, Word's default style is applied. A paragraph style can be applied to both paragraph and run structures. A character style can be applied to runs. A table style can be applied to tables and a list style can be applied to lists.

All styles need to be defined at the top of the document, after the <w:wordDocument> tag and before the <w:body> tag. Paragraph, run and table styles need to be defined within a <w:styles> tag. List styles need to be defined within a <w:lists> tag.

Full options:

The best way to see all options available in defining styles is to create a document in Word, apply whatever formatting options you are interested in, then save the document as XML. Because WordprocessingML has been designed as a lossless format, all available options will be shown for every aspect of WordprocessingML. It is also possible to consult the Office 2003 XML Reference Schemas for more information, though the volume of information found there can be quite overwhelming.

Paragraph style:

There are 34 possible style elements that can be used to apply formatting to paragraphs, including "alignment, indentation, spacing, tab stops, widow/orphan control, paragraph borders, etc. tags" (Lenz, p. 49).

There are two ways to apply formatting to paragraphs in WordprocessingML. The first is called "direct formatting," where properties are defined immediately within the <w:pPr> element. The second is through applying a defined paragraph style to a paragraph. Note that "[i]ndividual paragraph properties applied through direct formatting always override the corresponding properties defined in the associated paragraph style" (Lenz, p. 49).

Paragraph and run styles are complementary and can be used together in a style.

Direct formatting:

In the example below, the text will be centered.

<w:p>    
  <w:pPr>  
    <w:jc w:val="center" /> <!-- The "w:jc" elements represents the paragraph justification settings. -->
  </w:pPr>  
  <w:r>  
    <w:t>Sample text will be centered</w:t>
  </w:r>  
</w:p>    

Using paragraph styles:

In the example below, this text will also be centered.

This needs to be defined at the top:
<w:styles>      
  <w:style w:type="paragraph" w:styleId="Centered1"> <!-- Use this name to refer to style -->
    <w:name w:val="centered 1" /> <!-- name displayed in Word in the styles box -->
    <w:pPr>  
      <w:jc w:val="center" /> <!-- The "w:jc" elements represents the paragraph justification settings. -->
   
</w:pPr>
 
  </w:style>    
</w:styles>      
       
       
Later in the document, wherever your paragraph is that needs to be formatted:
<w:p>      
  <w:pPr>    
   

<w:pStyle w:val="Centered1" /> <!--Associates style with this paragraph structure -->

  </w:pPr>    
  <w:r>    
    <w:t>Sample text will be centered</w:t>
  </w:r>    
</w:p>      

Sample file

 

Character style:

There are 42 possible style elements that can be used to specify formatting information such as "font, font size, font color, bold, italic, underline, strikethrough, character spacing, text effects, etc." (Lenz, p. 43).

There are two ways to apply formatting to runs in WordprocessingML. The first is called "direct formatting," where properties are defined immediately within the <w:rPr> element. The second is through applying a defined paragraph or run style to a run. Note that "[i]ndividual font properties applied through direct formatting always override the corresponding properties defined in the associated paragraph or character style" (Lenz, p. 43).

Paragraph and run styles are complementary and can be used together in a style.

Direct formatting:

In this example, the text will be bold.

<w:p>      
  <w:r>    
    <w:rPr>  
      <w:b /> <!-- The bold tag-->
    </w:rPr>  
      <w:t>Sample text will be bold </w:t>
  </w:r>    
</w:p>      

Using run styles:

In the example below, this text will also be bold.

This needs to be defined at the top:
<w:styles>      
  <w:style w:type="character" w:styleId="Bold1"> <!-- Use this name to refer to style -->
    <w:name w:val="bold 1" /> <!-- name displayed in Word in the styles box -->
    <w:rPr>  
      <w:b /> <!-- Bold -->
   
</w:rPr>
 
  </w:style>    
</w:styles>      
       
       
Later in the document, wherever your paragraph is that needs to be formatted:
<w:p>      
  <w:r>    
    <w:rPr>  
      <w:rStyle w:val="Bold1" /> <!-- associate Bold1 style with run-->
    </w:rPr>  
    <w:t>Sample text will be bold </w:t>
  </w:r>    
</w:p>      

Sample file

Table style:

To be completed...

List style:

List styles are very complicated in WordprocessingML. Each list item needs to be placed into a paragraph container and that paragraph container needs to be associated with a list definition. Each list in a document has its own list definition. So, if you have five different lists, you will need five separate list definitions. These list definitions are then linked to the document's base list definitions.

This paragraph is a part of a list:

<w:p>      
  <w:pPr>    
    <w:listPR>  
      <w:ilvl w:val="0" />
<!-- how many levels to indent. 0 means top-level. defined here.-->
      <w:ilfo w:val="1" />
<!-- associates this list with list definition 1. defined here.-->
    </w:listPR>  
  </w:pPr>    
  <w:r>    
    <w:t>Popcorn</w:t> <!-- list item text -->
  </w:r>    
</w:p>      

This is the list definition:

<w:lists>

  <w:listDef w:listDefId="0"> <!-- base list definition 0, defines how list will display -->
    <w:lsid w:val="6A98048A" />
    <w:plt w:val="Multilevel" />
    <w:tmpl w:val="A95E166A" />
    <w:lvl w:ilvl="0"> <!-- defines indent level 0, as used here --> (See note)
      <w:start w:val="1" /> <!-- number to use for first list element -->
      <w:nfc w:val="23" />
      <w:lvlText w:val="" /> <!-- symbol to use for list elements -->
      <w:lvlJc w:val="left" /> <!-- justification -->
      <w:pPr>
        <w:tabs>
          <w:tab w:val="list" w:pos="360" />
        </w:tabs>
        <w:ind w:left="360" w:hanging="360" />
      </w:pPr>
      <w:rPr>
        <w:rFonts w:ascii="Symbol" w:h-ansi="Symbol" w:hint="default" />
      </w:rPr>
    </w:lvl>      
  </w:listDef>        
  <w:list w:ilfo="1"> <!-- my list definition 1. corresponds to this use in my paragraph-->
    <w:ilst w:val="0" /> <!-- base list definition 0 corresponds to this element
  </w:list>        
</w:lists>

 

       

In the list definition, above, note that only indent level 0 is defined. WordprocessingML allows up to 9 (from 0 to 8) indent levels. Because I was only using level 0, I only defined that level.

Basically, in the body of your WordprocessingML document, you reference the "ilfo" number, which in your list definition references an "ilst" number in the base list definition.

Sample file

It is also possible to associate list styles with list definitions, which adds circular references and even more complexity. For more information on this, please see page 65 in Lenz et al.