Skip to content

Instantly share code, notes, and snippets.

@pedrokoblitz
Created April 8, 2016 18:22
Show Gist options
  • Save pedrokoblitz/d082a411bc0c95bcc2d6dff3b26a725e to your computer and use it in GitHub Desktop.
Save pedrokoblitz/d082a411bc0c95bcc2d6dff3b26a725e to your computer and use it in GitHub Desktop.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:apply-templates select="mysqldump/database"/>
</xsl:template>
<xsl:template name="repeat">
<xsl:param name="output" />
<xsl:param name="count" />
<xsl:if test="$count - 1 &gt;= 0">
<xsl:value-of select="$output" />
<xsl:if test="$count - 1 &gt; 0 ">,</xsl:if>
<xsl:call-template name="repeat">
<xsl:with-param name="output" select="$output" />
<xsl:with-param name="count" select="$count - 1" />
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="database">
<xsl:for-each select="table_structure">
<xsl:variable name="table">
<xsl:value-of select="@name"/>
</xsl:variable>
<xsl:variable name="field_list">
<xsl:for-each select="field"> `<xsl:value-of select="@Field"/>`<xsl:if test="position() != last()"><xsl:text>,</xsl:text></xsl:if> </xsl:for-each>
</xsl:variable>
<xsl:variable name="update_field_list">
<xsl:for-each select="field"> `<xsl:value-of select="@Field"/>`=?<xsl:if test="position() != last()"><xsl:text>,</xsl:text></xsl:if> </xsl:for-each>
</xsl:variable>
<xsl:variable name="placeholder_list">
<xsl:call-template name="repeat">
<xsl:with-param name="output" select="'?'" />
<xsl:with-param name="count" select="count(field)" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="basic_select">SELECT <xsl:value-of select="$field_list"/> FROM <xsl:value-of select="$table"/></xsl:variable>
<xsl:variable name="insert">INSERT INTO <xsl:value-of select="$table"/> (<xsl:value-of select="$field_list"/>) VALUES (<xsl:value-of select="$placeholder_list"/>)</xsl:variable>
<xsl:variable name="update">UPDATE <xsl:value-of select="$table"/> SET <xsl:value-of select="$update_field_list"/></xsl:variable>
<xsl:variable name="delete">DELETE FROM <xsl:value-of select="$table"/></xsl:variable>
<xsl:value-of select="$basic_select"/>;
<xsl:value-of select="$basic_select"/> WHERE id = ?;
<xsl:value-of select="$insert"/>;
<xsl:value-of select="$update"/>;
<xsl:value-of select="$delete"/>;
<xsl:value-of select="$delete"/> WHERE id = ?;
</xsl:for-each>
<xsl:text>&#xa;</xsl:text>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment