MYSQL - 将 XML 多个重复(复制)标签作为一个字符串加载

标签 mysql sql xml load

将带有数据的多个重复标签“<image></image>”加载到一个 <images> 时出现问题表格单元格。

XML

<posts>
   <item>
      <id>1</id>
      <type>post</type>
      <url>www.url.com/1</url>
      <date>2016-06-15</date>
      <image>some url/1xxx.jpg</image>
      <image>some url/1yyy.jpg</image>
      <image>some url/1zzz.jpg</image>
   </item>
   <item>
      <id>2</id>
      <type>post</type>
      <url>www.url.com/2</url>
      <date>2016-06-12</date>
      <image>some url/2xxx.jpg</image>
      <image>some url/2yyy.jpg</image>
      <image>some url/2zzz.jpg</image>
      <image>some url/2www.jpg</image>
   </item>
   <item>
      <id>3</id>
      <type>post</type>
      <url>www.url.com/3</url>
      <date>2016-06-12</date>
      <image>some url/3fff.jpg</image>
   </item>
</posts>

代码

现在它只加载最后一个 <image>来自 <item> 的标签

LOAD XML local infile 'D:\\demo.xml' 
REPLACE 
INTO TABLE posts CHARACTER SET UTF8
ROWS IDENTIFIED BY '<item>'
(@id, @type, @url, @date, @image)
SET id=@id, type=@type, url=@url, date = str_to_date(@date, '%Y-%m'), images=@image;

如何存储所有重复项<image>标记为图像 VARCHAR 或 TEXT

最佳答案

考虑使用 XSLT 转换您的 XML规范化 itemimages成一对多的表。下面使用 PHP 来运行 XSLT,但大多数通用语言都可以运行 XSLT 1.0 脚本,包括 PHP、Perl、Python、Java、C#、VB。具体来说,转换将打破<image> <item> 中的标签保持对应<id>并维护两组标签上传到两个MySQL数据库表。

XSLT 脚本(另存为 .xsl 文件)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/posts">
    <xsl:copy>
      <xsl:apply-templates select="item"/>
      <xsl:apply-templates select="item/image"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="item">
    <xsl:copy>
      <xsl:copy-of select="*[local-name()!='image']"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="item/image">
    <images>
      <itemid><xsl:value-of select="ancestor::item/id"/></itemid>
      <xsl:copy-of select="."/>
    </images>
  </xsl:template>

</xsl:stylesheet>

PHP 脚本

<?php

$cd = dirname(__FILE__);

// LOAD XML AND XSL FILES
$xml = new DOMDocument('1.0', 'UTF-8');
$xml->load('Original.xml');

$xslfile = new DOMDocument('1.0', 'UTF-8');
$xslfile->load('XSLT_Script.xsl');

// TRANSFORM XML with XSLT
$proc = new XSLTProcessor;
$proc->importStyleSheet($xslfile); 
$newXml = $proc->transformToXML($xml);

// SAVE TO FILE
file_put_contents('Output.xml', $newXml);

?>

输出 (图像包含项目 ID)

<?xml version="1.0"?>
<posts>
  <item>
    <id>1</id>
    <type>post</type>
    <url>www.url.com/1</url>
    <date>2016-06-15</date>
  </item>
  <item>
    <id>2</id>
    <type>post</type>
    <url>www.url.com/2</url>
    <date>2016-06-12</date>
  </item>
  <item>
    <id>3</id>
    <type>post</type>
    <url>www.url.com/3</url>
    <date>2016-06-12</date>
  </item>
  <images>
    <itemid>1</itemid>
    <image>some url/1xxx.jpg</image>
  </images>
  <images>
    <itemid>1</itemid>
    <image>some url/1yyy.jpg</image>
  </images>
  <images>
    <itemid>1</itemid>
    <image>some url/1zzz.jpg</image>
  </images>
  <images>
    <itemid>2</itemid>
    <image>some url/2xxx.jpg</image>
  </images>
  <images>
    <itemid>2</itemid>
    <image>some url/2yyy.jpg</image>
  </images>
  <images>
    <itemid>2</itemid>
    <image>some url/2zzz.jpg</image>
  </images>
  <images>
    <itemid>2</itemid>
    <image>some url/2www.jpg</image>
  </images>
  <images>
    <itemid>3</itemid>
    <image>some url/3fff.jpg</image>
  </images>
</posts>

SQL (要上传两个表)

-- POSTS TABLE
LOAD XML local infile 'C:\\Path\\To\\Output.xml' 
REPLACE 
INTO TABLE posts CHARACTER SET UTF8
ROWS IDENTIFIED BY '<item>'
(@id, @type, @url, @date)
SET id=@id, type=@type, url=@url, date=str_to_date(@date, '%Y-%m');

-- IMAGES TABLE    
LOAD XML local infile 'C:\\Path\\To\\Output.xml' 
REPLACE 
INTO TABLE images CHARACTER SET UTF8
ROWS IDENTIFIED BY '<images>'
(@itemid, @image)
SET itemid=@itemid, image=@image;

关于MYSQL - 将 XML 多个重复(复制)标签作为一个字符串加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40551940/

相关文章:

xml - 确定 XML 节点存在

javascript - 如果 xml 节点中没有值,如何让我的小部件不崩溃?

mysql - SQL 看不到别名

c# - MySql 表中行的平均值

MySQL w/MAMP - 退出 MAMP 后符号链接(symbolic link)消失

mysql - 内部连接子查询以从数据透视表中过滤记录

php - 检查金额是否存在

mysql - 在 MySQl 中使用别名时函数无效

sql - 查找时间戳之间的间隙

mysql - 从 mysql 数据库中选择一些内容并按计数(其中)排序