2014-03-12 36 views
1

我正在使用SSIS中的XSLT將XML導入任務用於非規範化文件。以下是XML內容。用於反規範化XML的XSLT

<?xml version="1.0" encoding="UTF-8"?> 
<ReceiptConfirmation> 
    <File> 
     <FileNumber>11520</FileNumber> 
     <FileCreationDate>2014-02-11</FileCreationDate> 
     <FileCreationTime>20:50:23</FileCreationTime> 
    </File> 
    <Receipts> 
     <Receipt> 
     <InboundID>2136</InboundID> 
     <ReceiptID>R00706</ReceiptID> 
     <Vendor>DayMen</Vendor> 
     <ReceiptDate>2013-12-11</ReceiptDate> 
     <ReceiptTime>11:32:21</ReceiptTime> 
     <ReceiptType>RMA</ReceiptType> 
     <PartReceipts> 
      <PartReceipt> 
       <PartRef>JB00128-CEN</PartRef> 
       <QtyExpected>2</QtyExpected> 
       <QtyReceived>0</QtyReceived> 
       <StockStatus>Inspection</StockStatus> 
      </PartReceipt> 
      <PartReceipt> 
       <PartRef>JB01259-BWW</PartRef> 
       <QtyExpected>2</QtyExpected> 
       <QtyReceived>2</QtyReceived> 
       <StockStatus>Inspection</StockStatus> 
      </PartReceipt> 
      <PartReceipt> 
       <PartRef>JB01257-BWW</PartRef> 
       <QtyExpected>1</QtyExpected> 
       <QtyReceived>1</QtyReceived> 
       <StockStatus>Inspection</StockStatus> 
      </PartReceipt> 
      <PartReceipt> 
       <PartRef>JB01112-BWW</PartRef> 
       <QtyExpected>3</QtyExpected> 
       <QtyReceived>3</QtyReceived> 
       <StockStatus>Inspection</StockStatus> 
      </PartReceipt> 
     </PartReceipts> 
     </Receipt> 
    </Receipts> 
</ReceiptConfirmation> 

我使用XSLT是

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" indent="yes"/> 
<xsl:template match="/"> 
<File> 
<xsl:for-each select="ReceiptConfirmation/Receipts"> 
<xsl:for-each select="ReceiptConfirmation/Receipts/Receipt/PartReceipt"> 
    <ReceiptConfirmation> 
    <FileNumber><xsl:value-of select="ReceiptConfirmation/File/FileNumber"/>   
</FileNumber> 
    <FileCreationDate><xsl:value-of select="ReceiptConfirmation/File/FileCreationDate"/>  
</FileCreationDate> 
    <FileCreationTime><xsl:value-of select="ReceiptConfirmation/File/FileCreationTime"/>   
</FileCreationTime> 
    <InboundID><xsl:value-of select="ReceiptConfirmation/Receipts/Receipt/InboundID"/>  
</InboundID> 
    <ReceiptID><xsl:value-of select="ReceiptConfirmation/Receipts/Receipt/ReceiptID"/>   
</ReceiptID> 
    <Vendor><xsl:value-of select="ReceiptConfirmation/Receipts/Receipt/Vendor"/>  
</Vendor> 
     <ReceiptDate><xsl:value-of    
select="ReceiptConfirmation/Receipts/Receipt/ReceiptDate"/></ReceiptDate> 
<ReceiptTime><xsl:value-of  
select="ReceiptConfirmation/Receipts/Receipt/ReceiptTime"/></ReceiptTime> 
<ReceiptType><xsl:value-of  
select="ReceiptConfirmation/Receipts/Receipt/ReceiptType"/></ReceiptType> 
<PartRef><xsl:value-of 
select="ReceiptConfirmation/Receipts/Receipt/PartReceipts/PartReceipt/PartRef"/> 
</PartRef> 
<QtyExpected><xsl:value-of   
select="ReceiptConfirmation/Receipts/Receipt/PartReceipts/PartReceipt/QtyExpected"/> 
</QtyExpected> 
<QtyReceived><xsl:value-of select="ReceiptConfirmation/Receipts/Receipt/PartReceipts/PartReceipt/QtyExpected"/> 
</QtyReceived> 
<StockStatus><xsl:value-of 
select="ReceiptConfirmation/Receipts/Receipt/PartReceipts/PartReceipt/QtyExpected"/>  
</StockStatus> 
</ReceiptConfirmation> 

</xsl:for-each> 
</xsl:for-each> 
</File> 
</xsl:template> 
</xsl:stylesheet> 

我需要兩個節點之間建立循環和使用XSLT得到的結果一些幫助。

這是我所期待的。

<?xml version="1.0" encoding="utf-8"?> 
<ReceiptConfirmation> 
<Receipt> 
<FileNumber>11520</FileNumber> 
    <FileCreationDate>2014-02-11</FileCreationDate> 
    <FileCreationTime>20:50:23</FileCreationTime> 
    <InboundID>2136</InboundID> 
    <ReceiptID>R00706</ReceiptID> 
    <Vendor>DayMen</Vendor> 
    <ReceiptDate>2013-12-11</ReceiptDate> 
    <ReceiptTime>11:32:21</ReceiptTime> 
    <ReceiptType>RMA</ReceiptType> 
    <PartRef>JB00128-CEN</PartRef> 
    <QtyExpected>2</QtyExpected> 
    <QtyReceived>2</QtyReceived> 
    <StockStatus>Inspection</StockStatus> 
</Receipt> 
<Receipt> 
    <FileNumber>11520</FileNumber> 
    <FileCreationDate>2014-02-11</FileCreationDate> 
    <FileCreationTime>20:50:23</FileCreationTime> 
    <InboundID>2136</InboundID> 
    <ReceiptID>R00706</ReceiptID> 
    <Vendor>DayMen</Vendor> 
    <ReceiptDate>2013-12-11</ReceiptDate> 
    <ReceiptTime>11:32:21</ReceiptTime> 
    <ReceiptType>RMA</ReceiptType> 
    <PartRef>JB00129-CEN</PartRef> 
    <QtyExpected>2</QtyExpected> 
    <QtyReceived>2</QtyReceived> 
    <StockStatus>Inspection</StockStatus> 
</Receipt> 
<Receipt> 
    <FileNumber>11520</FileNumber> 
    <FileCreationDate>2014-02-11</FileCreationDate> 
    <FileCreationTime>20:50:23</FileCreationTime> 
    <InboundID>2136</InboundID> 
    <ReceiptID>R00706</ReceiptID> 
    <Vendor>DayMen</Vendor> 
    <ReceiptDate>2013-12-11</ReceiptDate> 
    <ReceiptTime>11:32:21</ReceiptTime> 
    <ReceiptType>RMA</ReceiptType> 
    <PartRef>JB00130-CEN</PartRef> 
    <QtyExpected>2</QtyExpected> 
    <QtyReceived>2</QtyReceived> 
    <StockStatus>inStock</StockStatus> 
</Receipt> 
</ReceiptConfirmation> 
+0

感謝................ – user3410172

+0

您可以顯示你在這種情況下期待的輸出?如果你稍微縮進XML,稍微容易閱讀,它也會有所幫助。謝謝! –

+0

感謝您的回覆,其實我是新來的stackoverflow .... – user3410172

回答

1

我認爲(或者猜測)這會做你想要什麼:提前

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

<xsl:template match="/"> 
    <ReceiptConfirmation> 
     <xsl:for-each select="ReceiptConfirmation/Receipts/Receipt/PartReceipts/PartReceipt"> 
      <Receipt> 
       <xsl:copy-of select="ancestor::ReceiptConfirmation/File/*"/> 
       <xsl:copy-of select="ancestor::Receipt/*[not(*)]"/> 
       <xsl:copy-of select="*"/> 
      </Receipt> 
     </xsl:for-each> 
    </ReceiptConfirmation> 
</xsl:template> 

</xsl:stylesheet> 
+0

嗨michael.hor257k,感謝您節省我的一天...... – user3410172

+0

@ user3410172請注意,這可能是一個很好的XSLT解決方案,但它不是一個好的數據庫解決方案(在多個記錄中存儲相同的數據)。 –

+0

我只使用XSLT來非規範化XML文件。將數據提供給數據庫我正在使用openrow進行批量插入 – user3410172