2014-05-20 34 views
1

我一直在學習SQL Server(痛苦而緩慢),並最終得到了該死的東西來做我想做的事情,但有一個XML導入過程讓我絆倒向上。我有一個複雜的XML文件,如採樣(在此XML的詳細信息可以在這裏找到:http://wiki.eve-id.net/APIv2_Corp_AssetList_XML):在SSIS中將複雜XML轉換爲可用格式

<?xml version='1.0' encoding='UTF-8'?> 
<eveapi version="2"> 
    <currentTime>2010-12-19 07:15:16</currentTime> 
    <result> 
    <rowset name="assets" key="itemID" columns="itemID,locationID,typeID,quantity,flag,singleton"> 
     <row itemID="961254083" locationID="30001161" typeID="17177" quantity="1" flag="0" singleton="1" /> 
     <row itemID="961256074" locationID="30001161" typeID="27672" quantity="1" flag="0" singleton="1" /> 
     <row itemID="1270658107" locationID="30002583" typeID="17176" quantity="1" flag="0" singleton="1" /> 
     <row itemID="1000474513775" locationID="30002583" typeID="17407" quantity="1" flag="0" singleton="1"> 
     <rowset name="contents" key="itemID" columns="itemID,typeID,quantity,flag,singleton"> 
      <row itemID="1000515794105" typeID="255" quantity="1" flag="27" singleton="1" /> 
     </rowset> 
     </row> 
     <row itemID="1000474513607" locationID="30002583" typeID="17406" quantity="1" flag="0" singleton="1"> 
     <rowset name="contents" key="itemID" columns="itemID,typeID,quantity,flag,singleton"> 
      <row itemID="1000515772705" typeID="17686" quantity="1" flag="27" singleton="1" /> 
     </rowset> 
     </row> 
    </rowset> 
    </result> 
    <cachedUntil>2010-12-19 23:40:24</cachedUntil> 
</eveapi> 

因此,很自然SSIS snarks我,當我嘗試導入此使用XML源代碼的工具,因爲它是'複雜'的XML(嘟gr)。理想情況下,我希望發生的事情是能夠將這件事情變成SSIS數據流,以便將其轉儲到我的SQL數據庫中的表中。時髦的東西來與額外的嵌套行,這些是嵌套在其他項目內的項目,我想保留嵌套信息。理想情況下,我想要做的是創建一個包含父項的itemID的新列,如果它位於頂層,則爲0或null。這也需要重複父項的位置ID。因此,最終的輸出將是一個包含以下行的表:itemID,locationID,typeID,quantity,flag,singleton,parentID。鑑於上面的例子中輸入,輸出應該是這個樣子(假設.csv格式):

itemID, locationID, typeID, quantity, flag, singleton, parentID 
961254083, 30001161, 17177, 1, 0, 1, 0 
961256074, 30001161, 27672, 1, 0, 1, 0 
1270658107, 30002583, 17176, 1, 0, 1, 0 
1000474513775, 30002583, 17407, 1, 0, 1, 0 
1000515794105, 30002583, 255, 1, 27, 1, 1000474513775 
1000474513607, 30002583, 17406, 1, 0, 1, 0 
1000515772705, 30002583, 17686, 1, 27, 1, 1000474513607 

從我一直在使用Google(谷歌AAH的精彩功率)這是可能使用XSLT,但是在我剛剛瞭解到如何使用SSIS並在兩週前以這種方式使用XML文件時,我對此類知識的瞭解並不多。我希望獲得一些幫助,可以構建一個可以執行此轉換的XSLT文件,或者其他可能更好的方法。

另外需要注意的一點是:無論是哪種解決方案,都不能涉及任何手動輸入。我這樣做是爲了從人工輸入系統中遷移出來,所以任何手動輸入都會完全破壞這樣做的目的。

非常感謝!

+0

XSLT是一個好主意。你可以發佈你的預期產出的例子嗎? – helderdarocha

+0

**如果**我正確理解了您的XML源文檔,則需要將它導入兩次,分成兩個不同的表格 - 因此您需要兩個XSLT樣式表,每個導入一個。 –

+0

嗨Helderdarocha。因此,最終我需要它以表格格式輸出,其中包含以下列:itemID,locationID,typeID,數量,標誌,單身人士,parentID。所有這些都是原始XML,除了parentID,對於嵌套項目,它將爲0或null,併爲嵌套項目引用父項目。 –

回答

0

您的CSV風格輸出實際上非常簡單直接,可以通過XSLT獲取。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text" encoding="utf-8" /> 

    <xsl:template match="/"> 
    <xsl:text>itemID, locationID, typeID, quantity, flag, singleton, parentID&#xA;</xsl:text> 
    <xsl:apply-templates select="//row" /> 
    </xsl:template> 

    <xsl:template match="row"> 
    <xsl:variable name="p" select="ancestor::row[1]" /> 
    <xsl:value-of select="concat(@itemID, ', ')" /> 
    <xsl:value-of select="concat(@locationID|$p/@locationID, ', ')" /> 
    <xsl:value-of select="concat(@typeID, ', ')" /> 
    <xsl:value-of select="concat(@quantity, ', ')" /> 
    <xsl:value-of select="concat(@flag, ', ')" /> 
    <xsl:value-of select="concat(@singleton, ', ')" /> 
    <xsl:value-of select="concat($p/@itemID, '&#xA;')" /> 
    </xsl:template> 
</xsl:stylesheet> 

產生

 
itemID, locationID, typeID, quantity, flag, singleton, parentID 
961254083, 30001161, 17177, 1, 0, 1, 
961256074, 30001161, 27672, 1, 0, 1, 
1270658107, 30002583, 17176, 1, 0, 1, 
1000474513775, 30002583, 17407, 1, 0, 1, 
1000515794105, 30002583, 255, 1, 27, 1, 1000474513775 
1000474513607, 30002583, 17406, 1, 0, 1, 
1000515772705, 30002583, 17686, 1, 27, 1, 1000474513607 

的一件事沒什麼看頭的XSLT程序是此行

<xsl:value-of select="concat(@locationID|$p/@locationID, ', ')" /> 

這裏@locationID|$p/@locationID選擇工會當前<row>@locationID和的容器<row>。這個XPath最多可以返回兩個屬性節點,但這裏的基本假設是它們是互斥的。

這是一個小竅門,使線路在兩種情況下工作,同時避免條件。但是,如果我的基本假設是錯誤的,表達式的結果也是錯誤的。

如果您在理解解決方案時遇到困難,請閱讀<xsl:apply-templates>的工作原理。

+0

Woot!完美的作品,感謝Tomalak! –

+0

但你有沒有理解解決方案,這是個問題? – Tomalak

+2

它的片斷,是的。在處理文件時,我意識到在完整的xml中存在多個嵌套層次(這是以上樣本的幾倍)。對你的xslt做了一些調整,使它可以追趕到4層嵌套。我認爲它不會比這更深,但如果是這樣,我會進一步調整它。此外,由於只有頂層嵌套才具有locationID,並且嵌套項目永遠不會位於與其父對象不同的位置,因此union函數可以很好地工作。我隨時瞭解( - : –