tags:

views:

168

answers:

1

I am currently trying to generate the creation SQL for my tables based off of a Visio diagram. I am doing this using the approach found here.

http://www.dougboude.com/blog/1/2008/11/SQL-Forward-Engineering-with-Visio-2003-Professional.cfm

I am attempting to modify the xslt file found there to better model the syntax that we use in our office. Unfortunately, I cannot get the part that involves passing the table name into the template for the table columns to work. The template gets called, but it seems to ignore my parameters.

<xsl:template match="Entity" mode="table">
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = '<xsl:value-of select="@PhysicalName"/>')
<br />
CREATE TABLE dbo.[<xsl:value-of select="@PhysicalName"/>]
(
<br />
<xsl:for-each select="EntityAttributes/EntityAttribute">
 <span style="padding-left: 20px;">
  <xsl:apply-templates select="../../../../Attributes/Attribute[@AttributeID = current()/@EntityAttributeID]" mode="table">
   <xsl:with-param name="EntityName" select="@PhysicalName" />
  </xsl:apply-templates>
 </span>
 <xsl:if test="count(../../EntityAttributes/EntityAttribute) != position()">,</xsl:if>
 <br />
</xsl:for-each>
)
<br />
GO
<p />

<xsl:apply-templates select="EntityAnnotations/EntityAnnotation[@AnnotationType='Primary Key']" mode="pk"/>
<xsl:apply-templates select="EntityAnnotations/EntityAnnotation[@AnnotationType='Alternate Key']" mode="ak"/>
<xsl:apply-templates select="EntityAnnotations/EntityAnnotation[@AnnotationType='Index']" mode="idx"/>
</xsl:template>

<!-- Create column for each EntityAttribute -->
<xsl:template match="Attribute" mode="table">
 <xsl:param name="EntityName"></xsl:param>
 <xsl:variable name="nullability">
  <xsl:choose>
   <xsl:when test='@AllowNulls = "false"'>NOT NULL CONSTRAINT DF_<xsl:value-of select="$EntityName" />_<xsl:value-of select="@PhysicalName"/>
  </xsl:when>
   <xsl:otherwise> NULL</xsl:otherwise>
  </xsl:choose>
 </xsl:variable>
 <xsl:variable name="incremental">
  <xsl:choose>
   <xsl:when test='@PhysicalDatatype = "int identity"'> INT IDENTITY(1,1)</xsl:when>
   <xsl:otherwise><xsl:value-of select="@PhysicalDatatype"/></xsl:otherwise>
  </xsl:choose>
 </xsl:variable>

[<xsl:value-of select="@PhysicalName"/>] <span style="text-transform:uppercase;">   <xsl:value-of select="$incremental"/></span>  <xsl:value-of select="$nullability"/>

</xsl:template>
+2  A: 

The parameter is not ignored, but I guess it is empty. You call:

<xsl:with-param name="EntityName" select="@PhysicalName" />

where @PhysicalName must be an attribute of EntityAttributes/EntityAttribute element from the for-each. The fact that you use @PhysicalName earlier in

CREATE TABLE dbo.[<xsl:value-of select="@PhysicalName"/>]

makes me think in reality it is an attribute of the Entity element the template matches. You need to store its value in a variable first (before the for-each):

<xsl:variable name="PhysicalName" select="@PhysicalName" />

and then use it like this:

<xsl:with-param name="EntityName" select="$PhysicalName" />
<!-- -------------------------------------^  -->

The for-each resets the context node with every iteration, I guess this is where it goes wrong for you.

Tomalak