繁体   English   中英

拆分XML标记并转换为SQL

[英]Split XML tags and transform to SQL

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

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

我有一列用于last_name和first_name,因此Evans,Henderson和Coyier应该转到last_name,依此类推。

我如何从标记中挑选它们并将其放入sql语句中!

提前致谢!

您可以使用xslt来做到这一点,但是它不是很漂亮,因为您需要解析姓氏/名字对,然后解析姓氏-您自己的名字。 这是通过递归完成的。

在同一个xslt中,您可以从中生成SQL语句,但这又不是'O''Hanlon' ,因为您必须转义任何文字字符串分隔符,例如, 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处理并将其插入数据库中,我建议您使用Kettle之类的工具。 pentaho数据集成。 它有许多步骤可用于处理数据,并具有30多个数据库的现成连接性。 它是免费的,并且易于安装。 在这里获取它: http : //sourceforge.net/projects/pentaho/files/Data%20Integration/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM