cancel
Showing results for 
Search instead for 
Did you mean: 

Help with XSLT mapping

Former Member
0 Kudos

Hi

I have a file that produces a flat xml as below

01 header                         => invoice 1

02 vendor data

03 vendor bank data

04 gl line data

01 header                          => invoice 2

and so on.

I am struggling with creating an xsl template that will convert the above xml into a parent-child relationship as below

01 header

===> 02 vendor data

===> 03 vendor bank data

===> 04 gl line data

01 header

Basically I want to insert all 02,03,04 nodes that follow a 01 node as children of the 01 node, until the next 01 node, and so on till the end of file.

Please can experts provide me the appropriate xsl template to process the file as desired.

Accepted Solutions (0)

Answers (2)

Answers (2)

former_member201264
Active Contributor
0 Kudos

Hi Gary,

can you send your source XML so that it is easy to understand.

Regards,

Sreeni.

Former Member
0 Kudos

Hi Gary,

Firstly you should use the File Adapter with Content Conversion to parse the lines in your file into a structure like this:

<Recordset>

     <Record>

          <RecordType>01</RecordType> <-- This is the header

          <Field1>Foo</Field1>

          <FieldN>Foo</FieldN>

     </Record>

     <Record>

          <RecordType>02</RecordType> <-- This is the vendor data

          <Field1>Foo</Field1>

          <FieldN>Foo</FieldN>

     </Record>

     etc

</Recordset>

Your XSL would then need to process each RecordType in the correct sequence to produce your nesting. Using XPath Axes like below would work however it assumes that you have exactly 3 RecordTypes in-between the Header record:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

          <xsl:template match="/">

                     <Document>

               <xsl:for-each select="//Record[RecordType='01']">

                    <Header>

                         <xsl:for-each select="following-sibling::*[RecordType!='01']">

                                                       <xsl:if test="position() &lt;= '3'">

                                                            <xsl:choose>

                                                                                     <xsl:when test="RecordType = '02'">

                                                                                                    <Vendor><xsl:value-of select="./FieldB"/></Vendor>

                                                                                     </xsl:when>                                  

                                                                                     <xsl:when test="RecordType = '03'">

                                                                                                    <VendorBank><xsl:value-of select="./FieldC"/></VendorBank>

                                                                                     </xsl:when>

                                                                                     <xsl:when test="RecordType = '04'">

                                                                                                    <GL><xsl:value-of select="./FieldC"/></GL>

                                                                                     </xsl:when>                                  

                                                            </xsl:choose>

                                                       </xsl:if>

                         </xsl:for-each>

                    </Header>

               </xsl:for-each>

               </Document>

          </xsl:template>

 

</xsl:stylesheet>

Hope this helps you.

Nick