2010-01-05 66 views
1

我想用xslt将大型xml文件转换为sql语句。例如,我有作者标签。拆分XML标签并转换为SQL

<author>Evans, Jim; Henderson, Mike; Coyier, Alan</author> 

我有一个last_name和first_name的列,所以Evans,Henderson和Coyier应该去last_name等等。

如何从标签中选取它们并将其放入sql语句中!

在此先感谢!

回答

0

你可以用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">&apos;</xsl:with-param> 
      <xsl:with-param name="replace">&apos;&apos;</xsl:with-param> 
     </xsl:call-template>' 
     , 
     '<xsl:call-template name="replace"> 
      <xsl:with-param name="text" select="$last-name"/> 
      <xsl:with-param name="search">&apos;</xsl:with-param> 
      <xsl:with-param name="replace">&apos;&apos;</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/

+0

非常感谢,罗兰 - 完美的解决方案! – flhe 2010-01-06 11:47:41