Ayon Biswas
Ayon Biswas

Reputation: 11

I need to transform a complex xml to fixed length flat file.Please help me

I have the following XML,I need to transform it to fixed length flat file

<IDataXMLCoder version="1.0">
  <record javaclass="com.wm.data.ISMemDataImpl">
    <value name="fileName">H_MonsantoNSCINVOIC</value>
    <idatacodable name="iDocList" javaclass="com.wm.adapter.sap.idoc.IDataDocumentList">
      <array name="iDocs" type="idatacodable" depth="1">
        <idatacodable javaclass="com.wm.adapter.sap.idoc.IDataDocument">
          <record name="iDocControl" javaclass="com.wm.data.ISMemDataImpl">
            <value name="TABNAM">EDI_DC40</value>
            <value name="MANDT">100</value>
            <value name="DOCNUM">0000000308010466</value>
            <value name="DOCREL">700</value>
            <value name="STATUS">30</value>
            <value name="DIRECT">1</value>
            <value name="OUTMOD">2</value>
            <value name="EXPRSS"></value>
            <value name="TEST"></value>
            <value name="IDOCTYP">INVOIC02</value>
            <value name="CIMTYP"></value>
            <value name="MESTYP">INVOIC</value>
            <value name="MESCOD">MON</value>
            <value name="MESFCT">BE</value>
            <value name="STD"></value>
            <value name="STDVRS"></value>
            <value name="STDMES">INVOIC</value>
            <value name="SNDPOR">SAPQ12</value>
            <value name="SNDPRT">LS</value>
            <value name="SNDPFC"></value>
            <value name="SNDPRN">DE_Q12_100</value>
            <value name="SNDSAD"></value>
            <value name="SNDLAD"></value>
            <value name="RCVPOR">WM_MONSANT</value>
            <value name="RCVPRT">LS</value>
            <value name="RCVPFC">LS</value>
            <value name="RCVPRN">MONSANTOBE</value>
            <value name="RCVSAD"></value>
            <value name="RCVLAD"></value>
            <value name="CREDAT">2011-06-22</value>
            <value name="CRETIM">06:23:57</value>
            <value name="REFINT"></value>
            <value name="REFGRP"></value>
            <value name="REFMES"></value>
            <value name="ARCKEY"></value>
            <value name="SERIAL">20110428112753</value>
          </record>
          <array name="segments" type="idatacodable" depth="1">
            <idatacodable javaclass="com.wm.adapter.sap.idoc.IDataSegment">
              <value name="name">E1EDK01</value>
              <record name="fields" javaclass="com.wm.data.ISMemDataImpl">
                <value name="CURCY">EUR</value>
                <value name="HWAER">EUR</value>
                <value name="WKURS">1.00000</value>
                <value name="ZTERM">F600</value>
                <value name="KUNDEUINR">ESA38362760</value>
                <value name="EIGENUINR">ESA08046799</value>
                <value name="BSART">INVO</value>
                <value name="BELNR">0098046324</value>
                <value name="NTGEW">360.000</value>
                <value name="BRGEW">371.880</value>
                <value name="GEWEI">KGM</value>
                <value name="RECIPNT_NO">0001605141</value>
                <value name="FKTYP">L</value>
              </record>
            </idatacodable>          </array>
        </idatacodable>
      </array>
    </idatacodable>
    <value name="$rfcname">IDOC_INBOUND_ASYNCHRONOUS</value>
    <value name="serverName">DE_Q12_100</value>
    <value name="$call">true</value>
    <value name="$encoding">ISO-8859-1</value>
    <value name="$tid">0AAFC4410C104E018A7D069D</value>
    <value name="$action">1</value>
    <value name="sender">DE_Q12_100</value>
    <value name="receiver">MONSANTOBE</value>
    <value name="msgType">INVOIC</value>
    <record name="transportParams" javaclass="com.wm.util.Values">
    </record>
  </record>
</IDataXMLCoder>

this is an example this type of record fields are there in the original input for 200 times and some of the value name are equal and some are different. Please guide me so that I can try for the whole input file

the output will look like

EDI_DC40  1000000000308010466700 3012  INVOIC02                                                    INVOIC                        MONBE        INVOICSAPQ12    LS  DE_Q12_100                                                                                           WM_MONSANTLSLSMONSANTOBE                                                                                           2011-06-2206:23:57                                                                                                                20110428112753      
E2EDK01005                    100000000030801046600000100000001    EUREUR1.00000     F600             ESA38362760         ESA08046799         INVO0098046324                         360.000           371.880           KGM                                                       0001605141                                                                    L              

there are two column only.please refer the input file each value is having fixed length,you can set any value,I will change after that.

Please help.....

Upvotes: 1

Views: 3326

Answers (3)

Vincent De Smet
Vincent De Smet

Reputation: 4979

This might help: http://www.stylusstudio.com/SSDN/default.asp?action=9&read=3179&fid=47 comes with a very handy xsl transformation file that has padding templates: http://www.stylusstudio.com/SSDN/upload/csv-to-fixed.xsl

Upvotes: 0

Babelabout
Babelabout

Reputation: 465

Here is an XSLT 1.0 "template" script that does the job ;-)

The way I (have started to) put the needed number of padding spaces for each field is a little tedious, but it works ;-) Indeed, if I would have to write the entire XSLT, I would probably write an XSLT to write the XSLT ;-)

If you need more help in order to “finalise” the script, just let me know…

On the other hand, if someone has another way to add the needed number of padding spaces, just let us know! Writing an XSLT is often an iterative exercise, this one is just my 1st working iteration ;-)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" encoding="ISO-8859-1"/>
  <xsl:template match="/">
    <!-- EDI_DC40 -->
    <xsl:for-each select="IDataXMLCoder/record/idatacodable/array/idatacodable/record/value">
      <xsl:value-of select="text()"/>
      <xsl:choose>
        <!-- TABNAM (10) -->
        <xsl:when test="@name = 'TABNAM'">
          <xsl:choose>
            <xsl:when test="string-length(text()) = 0">
              <xsl:text>          </xsl:text>
            </xsl:when>
            <xsl:when test="string-length(text()) = 1">
              <xsl:text>         </xsl:text>
            </xsl:when>
            <xsl:when test="string-length(text()) = 2">
              <xsl:text>        </xsl:text>
            </xsl:when>
            <xsl:when test="string-length(text()) = 3">
              <xsl:text>       </xsl:text>
            </xsl:when>
            <xsl:when test="string-length(text()) = 4">
              <xsl:text>      </xsl:text>
            </xsl:when>
            <xsl:when test="string-length(text()) = 5">
              <xsl:text>     </xsl:text>
            </xsl:when>
            <xsl:when test="string-length(text()) = 6">
              <xsl:text>    </xsl:text>
            </xsl:when>
            <xsl:when test="string-length(text()) = 7">
              <xsl:text>   </xsl:text>
            </xsl:when>
            <xsl:when test="string-length(text()) = 8">
              <xsl:text>  </xsl:text>
            </xsl:when>
            <xsl:when test="string-length(text()) = 9">
              <xsl:text> </xsl:text>
            </xsl:when>
            <xsl:when test="string-length(text()) &gt; 10">
              <xsl:message terminate="yes">ERROR: The maximum length of "TABNAM" is 10 characters.</xsl:message>
            </xsl:when>
            <xsl:otherwise/>
          </xsl:choose>
        </xsl:when>
        <!-- MANDT (3)-->
        <xsl:when test="MANDT">
          <xsl:choose>
            <xsl:when test="string-length(text()) = 0">
              <xsl:text>   </xsl:text>
            </xsl:when>
            <xsl:when test="string-length(text()) = 1">
              <xsl:text>  </xsl:text>
            </xsl:when>
            <xsl:when test="string-length(text()) = 2">
              <xsl:text> </xsl:text>
            </xsl:when>
            <xsl:when test="string-length(text()) &gt; 3">
              <xsl:message terminate="yes">ERROR: The maximum length of "MANDT" is 3 characters.</xsl:message>
            </xsl:when>
            <xsl:otherwise/>
          </xsl:choose>
        </xsl:when>
        <!-- DOCNUM (16) -->
        <!-- DOCREL (4) -->
        <!-- STATUS (4) -->
        <!-- etc. -->
      </xsl:choose>
    </xsl:for-each>
    <xsl:text>&#x0D;&#x0A;</xsl:text>
    <!-- E1EDK01 -->
    <xsl:for-each select="IDataXMLCoder/record/idatacodable/array/idatacodable/array/idatacodable/record/value">
      <xsl:value-of select="text()"/>
      <xsl:choose>
        <!-- ACTION (3) -->
        <xsl:when test="@name = 'ACTION'">
          <xsl:choose>
            <xsl:when test="string-length(text()) = 0">
              <xsl:text>   </xsl:text>
            </xsl:when>
            <xsl:when test="string-length(text()) = 1">
              <xsl:text>  </xsl:text>
            </xsl:when>
            <xsl:when test="string-length(text()) = 2">
              <xsl:text> </xsl:text>
            </xsl:when>
            <xsl:when test="string-length(text()) &gt; 3">
              <xsl:message terminate="yes">ERROR: The maximum length of "ACTION" is 3 characters.</xsl:message>
            </xsl:when>
            <xsl:otherwise/>
          </xsl:choose>
        </xsl:when>
        <!-- KZABS (1)-->
        <xsl:when test="KZABS">
          <xsl:choose>
            <xsl:when test="string-length(text()) = 0">
              <xsl:text> </xsl:text>
            </xsl:when>
            <xsl:when test="string-length(text()) &gt; 1">
              <xsl:message terminate="yes">ERROR: The maximum length of "KZABS" is 1 character.</xsl:message>
            </xsl:when>
            <xsl:otherwise/>
          </xsl:choose>
        </xsl:when>
        <!-- CURCY (3) -->
        <!-- HWAER (3) -->
        <!-- WKURS (12) -->
        <!-- etc. -->
      </xsl:choose>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Let me know if it helps...

Upvotes: 1

HiperiX
HiperiX

Reputation: 407

That is not a normal file... is an EDIfact invoice. Maybe this can help... The best thing you could do is attempt to XSLT the XML file...

Upvotes: 1

Related Questions