XSLT for denormalizing XML

I am working with XML import task to denormalised file using XSLT in SSIS. Following is the XML contents.

<?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>

The xslt I am using is

<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> 

I need some help to setting up loop between two nodes and getting the result using xslt.

This what I am expecting.

<?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>

Answers


I think (or guess) this will do what you want:

<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> 

Need Your Help

Convert .DAT File to ASCII in Matlab

matlab ascii fread

I have a few .dat files from a data acquisition system that I am trying to read in Matlab.