你可以用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嗡嗡聲並將其插入到數據庫中,我可以推薦使用諸如水壺等工具。 pentaho數據集成。它可以使用許多步驟來處理數據,併爲超過30個數據庫提供開箱即用的連接。它是免費的,並且易於安裝。得到它: http://sourceforge.net/projects/pentaho/files/Data%20Integration/
非常感謝,羅蘭 - 完美的解決方案! – flhe 2010-01-06 11:47:41