2009-09-14 89 views
0

我目前正在尝试基于Visio图生成我的表的创建SQL。我正在使用这里找到的方法来做这件事。与-param不工作在应用模板

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

我试图修改XSLT文件中发现有更好地模式,我们在我们的办公室中使用的语法。不幸的是,我无法得到涉及将表格名称传递到模板中以使表格列工作的部分。该模板被调用,但它似乎忽略了我的参数。

<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

参数不会被忽略,但我猜它是空的。您致电:

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

其中@PhysicalName必须从换每个EntityAttributes/EntityAttribute元素的属性。您在

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

使用@PhysicalName较早的事实让我觉得在现实中却是Entity元素的模板相匹配的属性。首先,您需要保存它以一个变量值(之前的for-each):

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

,然后用它是这样的:

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

的换每个复位与每次迭代上下文节点,我猜这是你的错。