Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries [Thread Prev] >Thread Next - Re: Treat last element differently Treat last element differentlyTo: NULL Date: 2/5/2005 1:07:00 AM Hello all,
I am trying to use XML templates with XSL to simplify my database
management. I define a table structure in XML then via a series of XSL
files I can generate SQL to create the table and common views and stored
procedures. I cannot work out a clean reliable way to translate a series
of elements to a comma separated list and prevent the comma being
appended to the final element.
Here is a basic example of the XML:
<table name="debtorOrders">
<field name="dirty" type="bit" nullable="false" />
<field name="deleted" type="bit" nullable="false">
<constraint type="default" value="0" />
</field>
<field name="gen" type="binary" size="8" nullable="true" />
<field name="number" type="int" nullable="false">
<constraint type="primaryKey" />
</field>
</table>
Here is the current XSL I am using (excuse wrapping):
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="text" encoding="iso-8859-1" indent="no" />
<xsl:template match="table">CREATE TABLE dbo.tb_<xsl:value-of
select="@name"/> (<xsl:apply-templates />)
GO</xsl:template>
<xsl:template match="field">
<xsl:value-of select="@name"/>
<xsl:text> </xsl:text>
<xsl:value-of select="@type"/>
<xsl:if test="@size">(<xsl:value-of select="@size"/>)</xsl:if>
<xsl:text> </xsl:text>
<xsl:choose>
<xsl:when test="@nullable='false'">NOT NULL</xsl:when>
<xsl:otherwise>NULL</xsl:otherwise></xsl:choose>
<xsl:apply-templates />
<xsl:text>,</xsl:text>
</xsl:template>
<xsl:template match="constraint">CONSTRAINT<xsl:choose>
<xsl:when test="@type='default'"> df_<xsl:value-of
select="../../@name" />_<xsl:value-of select="../@name" /> DEFAULT
(<xsl:value-of select="@value" />)</xsl:when>
<xsl:when test="@type='primaryKey'"> pk_<xsl:value-of
select="../../@name" />_<xsl:value-of select="../@name" /> PRIMARY
KEY</xsl:when>
<xsl:when test="@type='foreignKey'"> fk_<xsl:value-of
select="../../@name" />_<xsl:value-of select="../@name" /> FOREIGN KEY
REFERENCES dbo.tb_<xsl:value-of select="@foreignTableName" />
(<xsl:value-of select="@foreignFieldName" />)</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
And lastly here is the desired output:
CREATE TABLE dbo.tb_debtorOrders (
dirty bit NOT NULL,
deleted bit NOT NULL
CONSTRAINT df_debtorOrders_deleted DEFAULT (0)
,
gen binary(8) NULL,
lastRevision lastRevisionType NOT NULL,
revisionHost revisionHostType NOT NULL,
revisionUser revisionUserType NOT NULL,
number int NOT NULL
CONSTRAINT pk_debtorOrders_number PRIMARY KEY
)
GO
Unfortunately my XSL obviously places a comma near the end of the output
after "PRIMARY KEY" and before ") GO" but I need to prevent it from
happening. Is there a simple <xsl:if> I can use to only output a comma
if the <field> element is not the last?
Thank you for your help,
- Jason
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
