您可以使用 xslt 来完成,但它不是很漂亮,因为您需要自己解析出姓/名对,然后解析出姓 - 名。这是通过递归完成的。
在同一个 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 version="1.0"?>
<?xml-stylesheet type="text/xsl" href="author.xslt"?>
<author>Evans, Jim; Henderson, Mike; Coyier, Alan</author>
这给了我这个输出:
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' );
如果您需要进行大量 XML 转换并将其插入数据库,我可以推荐使用像kettle 这样的工具。 pentaho 数据集成。它有许多可用于处理数据的步骤,以及 30 多个数据库的开箱即用连接。它是免费的,并且易于安装。在这里得到它:
http://sourceforge.net/projects/pentaho/files/Data%20Integration/