dbdesc blog

database documentation

Adding a WordML XSLT Template to Dbdesc (IV)

Adding table details

Table details section contains the list of fields in a database table. It also contains the properties of each field. The best way to represent all this info is using a table.

As I mentioned in my last post, the best way to deal with tables is saving a Word document with just a simple table and then tweaking that code. So I saved a document with a 6 columns table:

<w:tbl>
<w:tblPr>
<w:tblStyle w:val="Tablaconcuadrcula"/>
<w:tblW w:w="0" w:type="auto"/>
<w:tblLook w:val="01E0"/>
</w:tblPr>
<w:tblGrid>
<w:gridCol w:w="1440"/>
<w:gridCol w:w="1440"/>
<w:gridCol w:w="1441"/>
<w:gridCol w:w="1441"/>
<w:gridCol w:w="1441"/>
<w:gridCol w:w="1441"/>
</w:tblGrid>
<w:tr>
<w:tc>
<w:tcPr>
<w:tcW w:w="1440" w:type="dxa"/>
</w:tcPr>
<w:p/>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1440" w:type="dxa"/>
</w:tcPr>
<w:p/>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1441" w:type="dxa"/>
</w:tcPr>
<w:p/>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1441" w:type="dxa"/>
</w:tcPr>
<w:p/>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1441" w:type="dxa"/>
</w:tcPr>
<w:p/>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1441" w:type="dxa"/>
</w:tcPr>
<w:p/>
</w:tc>
</w:tr>
</w:tbl>

As you can see is not that hard. Tables in WordML works like html tables. Each column is surrounded by <w:tc></w:tc> instead of <td></td>, rows are <w:tr></w:tr>, etc. If you want to add more rows just duplicate the code between <w:tr> and </w:tr>. Tags like <w:xxxPr> are used to add format properties to the appropriate object. For example <w:tcPr> is used to format a table column, <w:pPr> is used to format a text paragraph, etc.

I’ve changed the above code to get the header of the table:

<w:tbl>
<w:tblPr>
<w:tblStyle w:val="dbdescTableHeader" />
<w:tblW w:w="0" w:type="auto" />
<w:tblLook w:val="01E0" />
</w:tblPr>
<w:tblGrid>
<w:gridCol w:w="600" />
<w:gridCol w:w="1728" />
<w:gridCol w:w="1729" />
<w:gridCol w:w="1729" />
<w:gridCol w:w="1729" />
<w:gridCol w:w="1729" />
</w:tblGrid>
<w:tr>
<w:trPr>
<w:cantSplit />
<w:cnfStyle w:val="100000000000" />
</w:trPr>
<w:p></w:p>
<w:tc>
<w:tcPr>
<w:tcW w:w="400" w:type="dxa" />
</w:tcPr>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1728" w:type="auto" />
</w:tcPr>
<w:p>
<w:r>
<w:t>Field name </w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1250" w:type="auto" />
</w:tcPr>
<w:p>
<w:r>
<w:t>Data type</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1729" w:type="auto" />
</w:tcPr>
<w:p>
<w:r>
<w:t>Nullable</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1729" w:type="auto" />
</w:tcPr>
<w:p>
<w:r>
<w:t>Default value</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="3000" w:type="dxa" />
</w:tcPr>
<w:p>
<w:r>
<w:t>Description</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
<xsl:apply-templates select="Field" />
</w:tbl>

To add each field (and related info) I will use another XSL template:

<xsl:template match="Field">
<w:tr>
<w:trPr>
<w:cantSplit />
</w:trPr>
<w:tc>
<w:tcPr>
<w:tcW w:w="400" w:type="pct" />
</w:tcPr>
<w:p>
<w:r>
<w:t>
<xsl:choose>
<xsl:when test="PrimaryKey/COLUMN_NAME = COLUMN_NAME">PK
<xsl:if test="ForeignKey/COLUMN_NAME = COLUMN_NAME">FK</xsl:if>
</xsl:when>
<xsl:when test="ForeignKey/COLUMN_NAME = COLUMN_NAME">FK </xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="IndexedField/COLUMN_NAME = COLUMN_NAME">IX </xsl:when>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1728" w:type="auto" />
</w:tcPr>
<w:p>
<w:r>
<w:t>
<xsl:value-of select="COLUMN_NAME" />
</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1250" w:type="auto" />
</w:tcPr>
<w:p>
<w:r>
<w:t>
<xsl:value-of select="DATA_TYPE" />
(<xsl:value-of select="CHARACTER_MAXIMUM_LENGTH" />)
</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1729" w:type="auto" />
</w:tcPr>
<w:p>
<w:r>
<w:t>
<xsl:if test="IS_NULLABLE = 'YES' or (IS_NULLABLE = 1)">Yes</xsl:if>
</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1729" w:type="auto" />
</w:tcPr>
<w:p>
<w:r>
<w:t>
<xsl:value-of select="COLUMN_DEFAULT" />
</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="3000" w:type="dxa" />
</w:tcPr>
<w:p>
<w:r>
<w:t>
<xsl:value-of select="COLUMN_DESCRIPTION" />
</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
</xsl:template>

Screenshot In the image you can view the results.

If you are not using a XML editor, get one! It makes a diference. I’m using Visual Studio but there’s a lot of them out there.

Another advice is to go step by step. Add a small piece of code and try to open the file in Word. If you miss any tag in the source code, Word will just throw an error dialog with the line of the error and sometimes can be tricky to know what the problem is.

I’m pretty happy with this template. It’s much easier to play with WordML than with the RTF code (although is not as portable).

Comments