拆分XML标记并转换为SQL
我想用xslt将一个大型xml文件转换为sql语句。例如,我有作者标签拆分XML标记并转换为SQL,sql,xml,string,split,tags,Sql,Xml,String,Split,Tags,我想用xslt将一个大型xml文件转换为sql语句。例如,我有作者标签 <author>Evans, Jim; Henderson, Mike; Coyier, Alan</author> 伊万斯,吉姆;亨德森,迈克;科伊尔,艾伦 我有一个关于姓和名的专栏,所以埃文斯、亨德森和科伊尔应该转到姓等等 我如何才能从标记中选择它们并将其放入sql语句中 提前谢谢 您可以使用xslt来完成这项工作,但它不是很漂亮,因为您需要解析lastname/firstname对,然后自己
<author>Evans, Jim; Henderson, Mike; Coyier, Alan</author>
伊万斯,吉姆;亨德森,迈克;科伊尔,艾伦
我有一个关于姓和名的专栏,所以埃文斯、亨德森和科伊尔应该转到姓等等
我如何才能从标记中选择它们并将其放入sql语句中
提前谢谢 您可以使用xslt来完成这项工作,但它不是很漂亮,因为您需要解析lastname/firstname对,然后自己解析lastname-firstname。这是通过递归实现的 在同一xslt中,您可以从它生成SQL语句,但这也不是无痛的,因为您必须转义任何文本字符串分隔符,例如,
O'Hanlon
必须成为SQL字符串文本'O''Hanlon'
同样,这是通过递归实现的
这是一个功能齐全的示例:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<!-- match the eelement to extract data from -->
<xsl:template match="/author">
<xsl:call-template name="authors">
<xsl:with-param name="authors" select="text()"/>
</xsl:call-template>
</xsl:template>
<!-- recursively extract individual authors -->
<xsl:template name="authors">
<xsl:param name="authors"/>
<xsl:variable name="author" select="substring-before($authors,';')"/>
<xsl:choose>
<xsl:when test="string-length($author)=0">
<xsl:call-template name="author">
<xsl:with-param name="author" select="$authors"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="author">
<xsl:with-param name="author" select="$author"/>
</xsl:call-template>
<xsl:call-template name="authors">
<xsl:with-param name="authors" select="substring-after($authors,';')"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- extract firstname, lastname, escape single quote, and generate SQL -->
<xsl:template name="author">
<xsl:param name="author"/>
<xsl:variable name="last-name" select="normalize-space(substring-before($author, ','))"/>
<xsl:variable name="first-name" select="normalize-space(substring-after($author, ','))"/>
INSERT INTO author (first_name, last_name) VALUES (
'<xsl:call-template name="replace">
<xsl:with-param name="text" select="$first-name"/>
<xsl:with-param name="search">'</xsl:with-param>
<xsl:with-param name="replace">''</xsl:with-param>
</xsl:call-template>'
,
'<xsl:call-template name="replace">
<xsl:with-param name="text" select="$last-name"/>
<xsl:with-param name="search">'</xsl:with-param>
<xsl:with-param name="replace">''</xsl:with-param>
</xsl:call-template>'
);
</xsl:template>
<!-- recursive search and replace -->
<xsl:template name="replace">
<xsl:param name="text"/>
<xsl:param name="search"/>
<xsl:param name="replace"/>
<xsl:value-of select="$text"/>
<xsl:variable name="tail">
<xsl:if test="contains($text, $search)">
<xsl:call-template name="replace">
<xsl:with-param name="text" select="substring-after($text, $search)"/>
<xsl:with-param name="search" select="$search"/>
<xsl:with-param name="replace" select="$replace"/>
</xsl:call-template>
</xsl:if>
</xsl:variable>
<xsl:value-of select="concat(substring-before($text, $search), $tail)"/>
</xsl:template>
</xsl:stylesheet>
如果您需要大量使用XML并将其插入到数据库中,我建议您使用类似Ketter(又名Ketter)的工具。pentaho数据集成。它有许多步骤可用于处理数据,并可用于30多个数据库的开箱即用连接。它是免费的,并且易于安装。在这里获取:
您可以使用xslt来完成这项工作,但它不是很漂亮,因为您需要解析lastname/firstname对,然后自己解析lastname-firstname。这是通过递归实现的 在同一xslt中,您可以从它生成SQL语句,但这也不是无痛的,因为您必须转义任何文本字符串分隔符,例如,
O'Hanlon
必须成为SQL字符串文本'O''Hanlon'
同样,这是通过递归实现的
这是一个功能齐全的示例:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<!-- match the eelement to extract data from -->
<xsl:template match="/author">
<xsl:call-template name="authors">
<xsl:with-param name="authors" select="text()"/>
</xsl:call-template>
</xsl:template>
<!-- recursively extract individual authors -->
<xsl:template name="authors">
<xsl:param name="authors"/>
<xsl:variable name="author" select="substring-before($authors,';')"/>
<xsl:choose>
<xsl:when test="string-length($author)=0">
<xsl:call-template name="author">
<xsl:with-param name="author" select="$authors"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="author">
<xsl:with-param name="author" select="$author"/>
</xsl:call-template>
<xsl:call-template name="authors">
<xsl:with-param name="authors" select="substring-after($authors,';')"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- extract firstname, lastname, escape single quote, and generate SQL -->
<xsl:template name="author">
<xsl:param name="author"/>
<xsl:variable name="last-name" select="normalize-space(substring-before($author, ','))"/>
<xsl:variable name="first-name" select="normalize-space(substring-after($author, ','))"/>
INSERT INTO author (first_name, last_name) VALUES (
'<xsl:call-template name="replace">
<xsl:with-param name="text" select="$first-name"/>
<xsl:with-param name="search">'</xsl:with-param>
<xsl:with-param name="replace">''</xsl:with-param>
</xsl:call-template>'
,
'<xsl:call-template name="replace">
<xsl:with-param name="text" select="$last-name"/>
<xsl:with-param name="search">'</xsl:with-param>
<xsl:with-param name="replace">''</xsl:with-param>
</xsl:call-template>'
);
</xsl:template>
<!-- recursive search and replace -->
<xsl:template name="replace">
<xsl:param name="text"/>
<xsl:param name="search"/>
<xsl:param name="replace"/>
<xsl:value-of select="$text"/>
<xsl:variable name="tail">
<xsl:if test="contains($text, $search)">
<xsl:call-template name="replace">
<xsl:with-param name="text" select="substring-after($text, $search)"/>
<xsl:with-param name="search" select="$search"/>
<xsl:with-param name="replace" select="$replace"/>
</xsl:call-template>
</xsl:if>
</xsl:variable>
<xsl:value-of select="concat(substring-before($text, $search), $tail)"/>
</xsl:template>
</xsl:stylesheet>
如果您需要大量使用XML并将其插入到数据库中,我建议您使用类似Ketter(又名Ketter)的工具。pentaho数据集成。它有许多步骤可用于处理数据,并可用于30多个数据库的开箱即用连接。它是免费的,并且易于安装。在这里获取:
非常感谢,罗兰-完美的解决方案!非常感谢,罗兰-完美的解决方案!“大”对你来说意味着什么?通常的XSLT处理器都有严重的内存限制,因为它们基于DOM?通常的XSLT处理器有严重的内存限制,因为它们基于DOM。
INSERT INTO author (first_name, last_name) VALUES ( 'Jim' , 'Evans' );
INSERT INTO author (first_name, last_name) VALUES ( 'Mike' , 'Henderson' );
INSERT INTO author (first_name, last_name) VALUES ( 'Alan' , 'Coyier' );