Tables

In most cases, tables can be used in WordML documents whenever paragraphs (<w:p> elements) are permitted; both elements are permitted on the same level.

The table concept of WordML is similar to the basic structure of HTML tables. Here you can find an overview of the equivalents:

WordMLHTML
<w:tbl> <table>
<w:tblGrid> <colgroup>
<w:gridCol> <col>
<w:tr> <tr>
<w:tc> <td>

The basic structure

In addition to these matches, there is a whole range of differences. Most of them concern the presentation of the tables, which means elements being necessary in WordML in order to define, for example, borders and which would be controlled via CSS styles in HTML tables.

The basic structure of a WordML table is as follows:

<w:tbl>                         (1)
   <w:tblPr>...</w:tblPr>       (2)
   <w:tblGrid>                  (3)
     <w:gridCol w:w="4606" />   (4)
   ...
   </w:tblGrid>
   <w:tr>                       (5)
    <w:trPr>...</w:trPr>        (6)
     <w:tc>                     (7)
      <w:tcPr>...</w:tcPr>      (8)
     </w:tc>
...
   </w:tr>
...
</w:tbl>
 

(1) The <w:tbl> element is the container element of a table and encloses it.

(2) The table properties (<w:tblPr> ) contain formatting instructions, such as borders or a reference to styles to be applied on the respective table.

(3) <w:tblGrid> defines a group of table columns and corresponds to the HTML element <colgroup>. If the <w:tblGrid> element is not used and therefore the number and width of the columns are not preset, Word 2003 calculates the column width automatically. This behaviour of Word 2003 is not a given insofar as <w:tblGrid> is a mandatory element of <w:tbl> and as a consequence, it should not be omitted here.

(4) <w:gridCol> determines with its w:w attribute the width of a column in twips and corresponds to the HTML element <col>.

(5) The <w:tr> element starts a new table row and corresponds in its functionality to the HTML element <tr>.

(6) <w:trPr> contains the formatting instructions for the table row.

(7) The <w:tc> element defines the table cell and corresponds to the HTML element <td>.

(8) <w:tcPr> contains the formatting instructions for the table cell.

Spanning and table headers

Here you can find an example for a table with a vertical and a horizontal spanning, a table header and text:

<w:tbl>
   <w:tblPr>
     <w:tblStyle w:val="TableGrid"/>                           (1)
     <w:tblW w:w="0" w:type="auto"/>                           (2)
     <w:tblLook w:val="01E0"/>                                 (3)
   </w:tblPr>
   <w:tblGrid>
     <w:gridCol w:w="3070"/>                                   (2)
     <w:gridCol w:w="3071"/>                                   (2)
     <w:gridCol w:w="3071"/>                                   (2)
   </w:tblGrid>
   <w:tr>
     <w:trPr>
       <w:tblHeader/>                                          (4)
     </w:trPr>
     <w:tc>
      <w:tcPr><w:tcW w:w="3070" w:type="dxa"/></w:tcPr>        (2)
       <w:p><w:r><w:t>Tabellenkopf Spalte1</w:t></w:r></w:p>
                  <!-- en: table header column1 -->
     </w:tc>
     <w:tc>
      <w:tcPr><w:tcW w:w="3071" w:type="dxa"/></w:tcPr>
       <w:p><w:r><w:t>Tabellenkopf Spalte2</w:t></w:r></w:p>
                  <!-- table header column2 -->
     </w:tc>
     <w:tc>
      <w:tcPr><w:tcW w:w="3071" w:type="dxa"/></w:tcPr>
       <w:p><w:r><w:t>Tabellenkopf Spalte 3</w:t></w:r></w:p>
                  <!-- table header column3 -->
     </w:tc>
   </w:tr>
   <w:tr>
     <w:tc>
      <w:tcPr><w:tcW w:w="3070" w:type="dxa"/></w:tcPr>
       <w:p><w:r><w:t>Zelle1</w:t></w:r></w:p>
                 <!-- cell1 -->
     </w:tc>
     <w:tc>
       <w:tcPr>
         <w:tcW w:w="6142" w:type="dxa"/>                      (2)
         <w:gridSpan w:val="2"/>                               (5)
       </w:tcPr>
       <w:p><w:r><w:t>Zelle2 Spaltenüberspannung</w:t></w:r></w:p>
                    <!-- cell2 column spanning -->
     </w:tc>
   </w:tr>
   <w:tr>
     <w:tc>
      <w:tcPr>
         <w:tcW w:w="3070" w:type="dxa"/>
         <w:vmerge w:val="restart"/>                           (6)
       </w:tcPr>
       <w:p><w:r><w:t>Zelle3 Zeilenüberspannung</w:t></w:r></w:p>
                    <!-- cell3 row spanning -->
     </w:tc>
     <w:tc>
      <w:tcPr><w:tcW w:w="3071" w:type="dxa"/></w:tcPr>
       <w:p><w:r><w:t>Zelle4</w:t></w:r></w:p>
                    <!-- cell4 -->
     </w:tc>
     <w:tc>
      <w:tcPr><w:tcW w:w="3071" w:type="dxa"/></w:tcPr>
       <w:p><w:r><w:t>Zelle5</w:t></w:r></w:p>
                    <!-- cell5 -->
     </w:tc>
   </w:tr>
   <w:tr>
     <w:tc>
       <w:tcPr>
         <w:tcW w:w="3070" w:type="dxa"/>
         <w:vmerge/>                                          (6)
       </w:tcPr>        
       <w:p/>                                                 (6)
     </w:tc>
     <w:tc>
       <w:tcPr><w:tcW w:w="3071" w:type="dxa"/></w:tcPr>
       <w:p><w:r><w:t>Zelle6</w:t></w:r></w:p>
                    <!-- cell6 -->
     </w:tc>
     <w:tc>
       <w:tcPr><w:tcW w:w="3071" w:type="dxa"/></w:tcPr>
       <w:p><w:r><w:t>Zelle7</w:t></w:r></w:p>
                    <!-- cell7 -->
     </w:tc>
   </w:tr>
</w:tbl>

image - table with spannings

Figure: table with spannings

 

(1) With the help of <w:tblStyle>, reference can be made to a style for the respective table. The w:val attribute contains the Ref-ID of the used style.

(2) <w:tblW> defines the width of the table. The w:w attribute contains the width specification and the w:type attribute contains the unit of measurement. In this case, the unit of measurement is set to auto, with the consequence that the table adopts automatically the width of the print space and the value of w:w will be ignored. However, the w:type attribute can also have the values dxa (twips) and pct (percent).

The <w:tcW> element indicates the width of the table cell. Since in this example the table was automatically set, the specifications have been calculated by Word 2003. If the table was manually generated or with the help of a style­sheet, such a specification would often be a hindrance. Therefore, it could also be omitted. In this case, Word calculates the cell width on its own. The same applies for the <w:gridCol> element which determines the width of the column.

(3) <w:tblLook> is an element which seems to be dealing with the visualisation of the table borders. Its w:val attribute contains a hexadecimal value added up whose method of calculation and precise meaning are not clear.

(4) The <w:tblHeader> element defines the header of a table which is repeated if a page break occurs. In contrast to HTML tables which use the <thead> element in order to determine the table header, in WordML the table header is a property of a table row and therefore set in the <w:trPr> element. In WordML, there is no equivalant for a table footer which is repeated on each page when a page break occurs.

(5) The <w:gridSpan> element allows the spanning of columns, the w:val attribute indicates the number of the spanned columns.

(6) The <w:vmerge> element is used for the vertical spanning in tables. All cells with this element are spanned. The beginning of the spanning is indicated with the w:val attribute and the value restart. Each table cell, even though it is spanned and has no textual content, must contain a parapgraph (<w:p/>).

<w:tbl>                                               (1)
   <w:tblPr>                                          (2)
     <w:tblStyle w:val="Tabellengitternetz"/>         (3)
     <w:tblW w:w="0" w:type="auto"/>                  (4)
     <w:tblLook w:val="01E0"/>                        (5)
   </w:tblPr>
   <w:tblGrid>                                        (6)
     <w:gridCol w:w="4606"/>                          (6)
     <w:gridCol w:w="4606"/>                          (6)
   </w:tblGrid>
   <w:tr>                                             (7) 
     <w:tc>                                           (8)
       <w:tcPr>                                       (8)
         <w:tcW w:w="4606" w:type="dxa"/>             (8)
       </w:tcPr>
       <w:p><w:r><w:t>Zelle 1 <!-- en: cell 1 --> </w:t></w:r></w:p>
     </w:tc>
     <w:tc>
       <w:tcPr>
         <w:tcW w:w="4606" w:type="dxa"/>
       </w:tcPr>
       <w:p><w:r><w:t>Zelle 2</w:t></w:r></w:p>
     </w:tc>
   </w:tr>
   <w:tr>
     <w:tc>
       <w:tcPr>
         <w:tcW w:w="4606" w:type="dxa"/>
           <w:vmerge w:val="restart"/>               (9)
       </w:tcPr>
       <w:p><w:r><w:t>Zelle 3</w:t></w:r></w:p>
     </w:tc>
     <w:tc>
       <w:tcPr>
         <w:tcW w:w="4606" w:type="dxa"/>
       </w:tcPr>
       <w:p><w:r><w:t>Zelle 4</w:t></w:r></w:p>
     </w:tc>
   </w:tr>
   <w:tr>
     <w:tc>
       <w:tcPr>
         <w:tcW w:w="4606" w:type="dxa"/>
         <w:vmerge/>
       </w:tcPr>
       <w:p/>
     </w:tc>
     <w:tc>
       <w:tcPr>
         <w:tcW w:w="4606" w:type="dxa"/>
       </w:tcPr>
       <w:p><w:r><w:t>Zelle 5</w:r></w:p> 
     </w:tc>
   </w:tr>
   <w:tr>
     <w:tc>
       <w:tcPr>
         <w:tcW w:w="9212" w:type="dxa"/>           (10)
           <w:gridSpan w:val="2"/>                  (10)
       </w:tcPr>
       <w:p><w:r><w:t>Zelle 6</w:t></w:r></w:p>
     </w:tc>
   </w:tr>
</w:tbl>

(1) The <w:tbl> element encloses the entire table.

(2) The <w:tblPr> element contains the table properties.

(3) The <w:tblStyle> element refers with its w:val attribute to a style set up for tables.

(4) The <w:tblW> element determines the width of the table. In this example, the width is automatically determined (auto).

(5) The <w:tblLook> element contains information on the appearance of the table. The value is a hexadecimal value added up consisting of the »appearance« of the header, the last row, the last column and the column header.

(6) <w:tblGrid> corresponds to the HTML element <colgroup>, whereas the <w:gridCol> elements correspond to the HTML element <col>. The w:w attribute defines the width of the column. The unit is twips (twentieths of a point).

(7) The <w:tr> element defines the table row, in this example without the optional <w:trPr> element which determines the properties of the row.

(8) The <w:tc> element defines the table cell; the <w:tcPr> element defines the cell properties, in this example the width.

(9) The <w:vmerge> element with its w:val attribute indicates whether the cell is located at the beginning of a vertical spanning or not.

(10) In the event of the spanning of columns, these will also be defined in the cell properties <w:tcPr>. The <w:gridSpan/> element indicates with its w:val attribute the number of the columns to be spanned.

Borders and shading

With the help of an appropriate user interface, indications on borders and shading can be easily made. This user interface can be found in the menu bar Format -> Rahmen und Schattierung (borders and shading)...

image - borders and shading

Figure: borders and shading

Since there are a lot of combination possibilities for shading and borders in different colour variations, the easiest way of getting a correct WordML markup is to generate the borders and shading with the help of this interface and then to view the result in WordML.

The following example shows an extract from a German Football League table. The table header is separated by a double line from the table body. The rows are separated alternately by a grey background and the lines of separation indicate whether the respective team is going to win the title, is qualifying for the UEFA Champions League or the UEFA Europa League.

image - table with shadings

Figure: table with shadings

Since the WordML code is very long and most elements have already been discussed, in this example only extracts from the structures are indicated being important for shading and borders:

<w:tbl>
   <w:tblPr>
     <w:tblBorders>                                      (1)
       <w:top w:val="none" w:sz="0" wx:bdrwidth="0" w:space="0" w:color="auto"/>     (1)
       <w:left w:val="none" w:sz="0" wx:bdrwidth="0" w:space="0" w:color="auto"/>    (1)
       <w:bottom w:val="none" w:sz="0" wx:bdrwidth="0" w:space="0 w:color="auto"/>   (1)
       <w:right w:val="none" w:sz="0" wx:bdrwidth="0" w:space="0" w:color="auto"/>   (1)
       <w:insideH w:val="none" w:sz="0" wx:bdrwidth="0" w:space="0" w:color="auto"/> (1)
       <w:insideV w:val="none" w:sz="0" wx:bdrwidth="0" w:space="0" w:color="auto"/> (1)
     </w:tblBorders>
   </w:tblPr>
 <w:tr>...
   <w:tc>
     <w:tcPr>
       <w:tcBorders>                                     (2)
         <w:bottom w:val="double" w:sz="4" wx:bdrwidth="30" w:space="0" w:color="auto"/> (2)
       </w:tcBorders>
     </w:tcPr>
     <w:p>
       <w:pPr><w:rPr><w:b/><w:color w:val="FF0000"/></w:rPr></w:pPr>
       <w:r><w:t>Verein <!-- en: Club --> </w:t></w:r>
     </w:p>
   </w:tc>...
 <w:tr>...
   <w:tc>
     <w:tcPr>
       <w:tcBorders>                                     (2)
         <w:top w:val="double" w:sz="4" wx:bdrwidth="30" w:space="0" w:color="auto"/> (3)
         <w:bottom w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/> (2)
       </w:tcBorders>
       <w:shd w:val="clear" w:color="auto" w:fill="CCCCCC"/> (4)
     </w:tcPr>
     <w:p><w:r><w:t>FC Bayern München</w:t></w:r></w:p>
   </w:tc>...
</w:tr>

(1) <w:tblBorders> is the container element for the table borders. The element has no attributes but six child elements which determine the outer borders and to some extent also the inner borders:

  • <w:top> generates a line at the top border of the table.

  • <w:left> generates a line at the left border of the table.

  • <w:bottom> generates a line at the bottom border of the table.

  • <w:right> generates a line at the right border of the table.

  • <w:insideH> generates the inner horizontal lines of the table.

  • <w:insideV> generates the inner vertical lines of the table.

The border elements <w:top>, <w:left>, <w:bottom>, <w:right>, <w:insideH> and <w:insideV> can also be part of the border properties of the table cells and there priority is given to these elements compared to the general border properties of the table.

All border lines have amongst others the following attributes:

  • w:val is not optional and indicates the type of the border line. It may have almost 200 different values. The most important values are none, if no border line shall be set, single for a simple and double for a double border line.

  • w:sz is optional and indicates the border thickness. The value is indicated in 1/8 of a pt.

  • wx:bdrwidth is optional and used in order to define the border width of HTML tables when saving the WordML document in HTML format. In Word 2003, the attribute itself is not processed for the display.

  • w:space defines a space to the intersection of the border line and has the effect that the line is not completely solid. The values are indicated in 1/8 of a pt.

image - spaces between table cells

Figure: spaces between table cells

  • w:color expects a RGB-hex value for the determination of the border line colour.

(2) <w:tcBorders> determines the cell borders in a table. It may contain all elements permitted in <w:tblBorders> for the framing of a table cell and overwrites the borders determined in the <w:tblBorders> elements. In this example the table borders have been deactivated and in the individual rows reactivated and differently defined. In the table header, a bottom border line has been generated having a double line (w:val='double') and a line width (w:sz) of 4. The line width is indicated in 1/8 of a pt, this means for the example a line width of 1/2 pt.

(3) Although the double separation line between table header and body has already been defined in the table cells of the header, Word 2003 is repeating this indication in the table cells bordering on the header when saving the document as WordML. In this example, the indication is repeated with the <w:top> element. For the automated generation of WordML by means of a stylesheet, this indication does not need to be repeated. For Word 2003 the indications in one of the neighbouring cells are enough for the import. Of course, the user has to ensure that no inconsistent indications are made.

(4) <w:shd> generates a background shading in the table cell. With the help of the attributes w:val and w:color, different patterns can be generated in the background. If these patterns are not wanted, the values have to be the same like the ones in this example. The w:fill attribute indicates the colour of the background in the form of a RGB-hex value.

<< back next >>

 


Copyright © dpunkt.verlag GmbH 2007
Printing of the online version is permitted exclusively for private use. Otherwise this chapter from the book "Professionelle XML-Verarbeitung mit Word" is subject to the same provisions as those applicable for the hardcover edition: The work including all its components is protected by copyright. All rights reserved, including reproduction, translation, microfilming as well as storage and processing in electronic systems.

dpunkt.verlag GmbH, Ringstraße 19B, 69115 Heidelberg, fon 06221-14830, fax 06221-148399, hallo(at)dpunkt.de