1

我有以下 XML,我需要将其转换为固定长度的平面文件

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

这是一个示例,这种类型的记录字段在原始输入中存在 200 次,并且一些值名称是相同的,有些是不同的。请指导我,以便我可以尝试整个输入文件

输出看起来像

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              

只有两列。请参考输入文件每个值都有固定长度,你可以设置任何值,之后我会改变。

请帮忙.....

4

3 回答 3

1

那不是一个普通的文件……是一张 EDIfact 发票。也许会有所帮助...您可以做的最好的事情是尝试对 XML 文件进行 XSLT...

于 2011-06-24T11:04:24.917 回答
1

这是一个完成这项工作的 XSLT 1.0“模板”脚本;-)

我(已经开始)为每个字段放置所需数量的填充空间的方式有点乏味,但它确实有效;-) 事实上,如果我必须编写整个 XSLT,我可能会编写一个 XSLT 来编写XSLT ;-)

如果您需要更多帮助来“完成”脚本,请告诉我……</p>

另一方面,如果有人有其他方法来添加所需数量的填充空间,请告诉我们!编写 XSLT 通常是一个迭代练习,这只是我的第一次工作迭代;-)

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

让我知道它是否有帮助...

于 2011-07-06T06:58:24.460 回答
0

这可能会有所帮助: http ://www.stylusstudio.com/SSDN/default.asp?action=9&read=3179&fid= 47 带有一个非常方便的带有填充模板的 xsl 转换文件:http: //www.stylusstudio.com/ SSDN/上传/csv-to-fixed.xsl

于 2012-09-20T02:30:02.513 回答