2016-11-11 17 views
1

將多個重複標記「<image></image>」與數據加載到一個<images>表格單元的問題。MYSQL - LOAD XML多個重複(重複)標記作爲一個字符串

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> 

代碼

現在只加載從<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 [email protected], [email protected], [email protected], date = str_to_date(@date, '%Y-%m'), [email protected]; 

最後<image>標籤如何存儲所有複製<image>標籤爲圖像VARCHAR或TEXT

+0

這是公平的,因爲夠你只有一個圖像列,但你有幾個圖像! Y原始數據未規範化。所以這將是棘手的 – e4c5

回答

0

考慮改變你的XML與XSLTitemimages標準化爲一對多表格。下面使用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 [email protected], [email protected], [email protected], 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 [email protected], [email protected]; 
+0

感謝Parfait,偉大的職位。有用。但是我還有一個問題,就是使用XSLT,我的XML文件高達1GB。我把我的文件剪成50大小,仍然有「內存分配失敗」(但是對於小文件,它的作用就像一個魅力。 – DASOFT

+0

是的,XSLT在較小的文件上效果很好,但是在更大的文件處理,一些XSLT 2.0和3.0有流處理過程,但是這些處理需要外部擴展,考慮切割更大的文件並使用PHP循環以導入到MySQL。 – Parfait

+0

通常,你需要[RAM大約5倍](http://stackoverflow.com/問題/ 3101048/xslt-transformation-on-large-xml-files-with-c-sharp)XSLT文件的大小。 – Parfait