dbdesc blog

database documentation

Adding a WordML XSLT Template to Dbdesc (II)

In my last post I decided to add a Word 2003 (WordML) template for dbdesc. The goal for this post is to get a first version of that template that builds a list of tables of a database.

The best way to start building the template is to write the simplest working WordML document, so this is the Hello Word document extracted from Brian Jones blog:

<?xml version="1.0"?>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
<w:body>
  <w:p>
    <w:r>
      <w:t>Hello World</w:t>
    </w:r>
   </w:p>
</w:body>
</w:wordDocument>

This code can be opened by Word 2003 without problems. However if you name the file HelloWorld.xml (WordML uses ‘.xml’) and you try to open it by double-clicking you will get a browser window (or your default xml viewer) showing the xml code. In order to allow Windows identify the document as a Word 2003 file you must add these lines:

<?xml version="1.0"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
<w:body>
  <w:p>
    <w:r>
      <w:t>Hello World</w:t>
    </w:r>
  </w:p>
</w:body>
</w:wordDocument>

Ok, now we are going to convert that document in a XSLT template to allow dbdesc parse it.

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">

<xsl:template match="dbdesc">
<xsl:processing-instruction name="mso-application">
  <xsl:text>progid="Word.Document"</xsl:text>
</xsl:processing-instruction>
  <w:wordDocument>
    <w:body>
       <w:p>
         <w:r>
           <w:t>Hello World</w:t>
         </w:r>
       </w:p>
    </w:body>
  </w:wordDocument>
</xsl:template>
</xsl:stylesheet>

Now you should be able to use it with dbdesc. Save that file like xml_WordML.xslt and run dbdesc:

dbdesc MSDE -E -d northwind -i xml_WordML.xslt

dbdesc will generate the file output.xml with that content. Now we are ready to start playing with the database info.

Let’s add a new template to match table nodes:

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">

<xsl:template match="dbdesc">
<xsl:processing-instruction name="mso-application">
  <xsl:text>progid="Word.Document"</xsl:text>
</xsl:processing-instruction>
<w:wordDocument>
  <w:body>
    <xsl:apply-templates select="Table" />
  </w:body>
</w:wordDocument>
</xsl:template>

<xsl:template match="Table">
  <w:p>
    <w:r>
      <w:t>
        <xsl:value-of select="TABLE_NAME" />
      </w:t>
    </w:r>
  </w:p>
</xsl:template>

</xsl:stylesheet>

And that’s all for now. You should get a Word 2003 document with the list of tables of Northwind database. Of course you can use this template to extract the list of tables of any MSDE, SQL Server, Access or Firebird database.

Comments