Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Treat last element differently

From: Jason <jason@-----.-----.-----.--->
To: 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


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent