![]() |
![]() | ![]() | ![]() | Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Re: Can you transform XML into a SQL INSERT statement >Thread Next - Re: Can you transform XML into a SQL INSERT statement Re: Can you transform XML into a SQL INSERT statementTo: NULL Date: 6/26/2008 1:37:00 AM Try this: <?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"/> <xsl:template match="/"> <xsl:apply-templates select="subDetail"/> </xsl:template> <xsl:template match="subDetail"> <xsl:text>insert </xsl:text> <xsl:value-of select="'table_name'"/><!-- What's the name of the destination table? --> <xsl:text> ( </xsl:text> <xsl:apply-templates select="detail[1]" mode="columnList"/> <xsl:text> )</xsl:text> <xsl:apply-templates select="detail" mode="selectStatement"/> </xsl:template> <xsl:template match="detail" mode="columnList"> <xsl:apply-templates select="*" mode="columnList"/> </xsl:template> <xsl:template match="*[parent::detail]" mode="columnList"> <xsl:if test="position() != 1"> <xsl:text> ,</xsl:text> </xsl:if> <xsl:value-of select="local-name()"/> </xsl:template> <xsl:template match="detail" mode="selectStatement"> <xsl:if test="position() != 1"> <xsl:text> union all</xsl:text> </xsl:if> <xsl:text> select </xsl:text> <xsl:apply-templates select="*" mode="selectStatement"/> </xsl:template> <xsl:template match="*[parent::detail]" mode="selectStatement"> <xsl:if test="position() != 1"> <xsl:text> ,</xsl:text> </xsl:if> <xsl:choose> <xsl:when test="not(number(.))"> <xsl:text>'</xsl:text> <xsl:value-of select="."/> <xsl:text>'</xsl:text> </xsl:when> <xsl:otherwise> <xsl:value-of select="."/> </xsl:otherwise> </xsl:choose> <xsl:text> as </xsl:text> <xsl:value-of select="local-name()"/> </xsl:template> </xsl:stylesheet> Note that you have duplicate colum names in there (Currency listed twice). ML --- Matija Lah, SQL Server MVP http://milambda.blogspot.com/ | ![]() | ![]() | ![]() |
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | |||||
|
