将<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以将item
和标准化images
为一对多表。下面使用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;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句