-----Original Message-----
From: Ade Odusote <aodusote(_at_)delfour(_dot_)com>
Sent: Mon, 16 Aug 2004 10:34:09 -0400
To: <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Subject: [xsl] Passing parameter into xsl:for-each
I have an XML data dictionary which I am trying to use to create a SQL DDL
statement.
The XML is below:
I second Andrew Welch's remark on the combination of push/pull. Once you are
able to wrap your brain around push processing (admittedly hard to do for a
person with a procedural programming background), you will find that they make
XLST programming much easier.
The structure of your XML is ambiguous when it comes to deciding which data
type to apply. For example, what is the data type of
dictionary/columns/column[(_at_)name="COMP_CODE"]?
I assumed that it is VARCHAR2, but it could be VARCHAR or CHAR. Is
RENW_STOR_PROF_CODE fixed or a float? Is VAR_LINE_ROOT_FLAG a byte or a char?
I made some assumptions on data typing, changed the output to "text", and
switched to a push process to make this first cut approximation. See if you can
take it from there.
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes" encoding="UTF-8" />
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="tables">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="table">
create table <xsl:value-of select="@name" />(
<xsl:apply-templates select="column" />
);
</xsl:template>
<xsl:template match="column">
<xsl:variable name="this-col" select="@name" />
<xsl:variable name="d-type">
<xsl:choose>
<xsl:when
test="/dictionary/columns/column[(_at_)name=$this-col][@datatype='number']">
FLOAT(<xsl:value-of
select="/dictionary/columns/column[(_at_)name=$this-col]/@length"
/>,<xsl:value-of
select="/dictionary/columns/column[(_at_)name=$this-col]/@decimals"
/>)</xsl:when>
<xsl:when
test="/dictionary/columns/column[(_at_)name=$this-col][@datatype='date']">
DATE</xsl:when>
<xsl:otherwise><xsl:value-of
select="/dictionary/columns/column[(_at_)name=$this-col]/@datatype" /> VARCHAR2
<xsl:value-of select="/dictionary/columns/column[(_at_)name=$this-col]/@length"
/></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="@name" /> <xsl:value-of select="$d-type" />
<xsl:if test="following-sibling::column">,</xsl:if>
</xsl:template>
<xsl:template match="columns" />
<xsl:template match="columns/column" />
</xsl:stylesheet>
--
Charles Knell
cknell(_at_)onebox(_dot_)com - email