【问题标题】:Split XML tags and transform to SQL拆分 XML 标记并转换为 SQL
【发布时间】:2010-01-05 09:51:19
【问题描述】:

我想用 xslt 将一个大的 xml 文件转换为 sql 语句。例如我有作者标签。

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

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

如何从标签中挑选出来放入sql语句中!

提前致谢!

【问题讨论】:

  • 对您来说“大”是什么意思?通常的 XSLT 处理器基于 DOM,因此对您的内存有严重的限制。

标签: sql xml string split tags


【解决方案1】:

您可以使用 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 转换并将其插入数据库,我可以推荐使用像kettle 这样的工具。 pentaho 数据集成。它有许多可用于处理数据的步骤,以及 30 多个数据库的开箱即用连接。它是免费的,并且易于安装。在这里得到它: http://sourceforge.net/projects/pentaho/files/Data%20Integration/

【讨论】:

  • 非常感谢,Roland - 完美的解决方案!
猜你喜欢
  • 2020-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-16
  • 2023-04-03
  • 2014-03-08
  • 2016-11-10
  • 1970-01-01
相关资源
最近更新 更多