I am generating MySQL’s create table statements. My source XML looks like:
<table name="TabName">
<column> ... </column> <!-- multiple columns -->
<primary-key> ... </primary-key>
<foreign-key> ... </foreign-key> <!-- multiple FKs -->
<key>... </key> <!-- multiple indexes -->
</table>
Transformatin is done like
<xsl:for-each select="column"> blabla ,</xsl:for-each>
<xsl:if test="primary-key"> blabla, </xsl:if>
<xsl:for-each select="key"> blabla, </xsl:for-each>
<xsl:for-each select="foreign-key"> blabla, </xsl:for-each>
Please notice that I append comma at the end of every statement. Following SQL output then looks like:
CREATE TABLE`categories` (
`CategoryID` tinyint(5) unsigned NOT NULL,
`CategoryName` varchar(15) NOT NULL,
`Description` mediumtext NOT NULL,
`Picture` varchar(50) NOT NULL,
PRIMARY KEY (`CategoryID`),) --here is bad column
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
How can I remove that comma when doing XSLT transformation? Thanks
For fixed strings that contain commas, look at the
substring-before-lastfunction I implemented as an answer to another question.If you create the comma yourself in XSLT, just avoid creating if it would be wrong.