Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Issue with simple transformation code

0 Kudos
799

Hi Folks

I am working on developing a custom simple transformation, the requirement is convert the upload the file(unknown columns text file) into xml code.

here columns text has not been mentioned so while uploading this data using "upload" function module we can get into string table, so now I want to pass this data into transformation to convert into below format, can you please suggest me how can I do this.

<Template1>

<Name>first 10 characters in the file line1</Name>

<Address>character 11 to 40 in the file line1</Address>

<Salar>character 41 to 50 in the file line1</Salary>

.

.

.

Etc.,

</Template1>

Please suggest me how to split the file line 1 data based on my tags in the simple transformation.

Appreciate your help on this.


Regards,

Naresh.

1 ACCEPTED SOLUTION

Former Member
0 Kudos
231

Have a look at this snippet.

Substring function is used with offset/length to extract the substring , which is then populated into xml node.

ABAP code

  1. DATA: lt TYPE TABLE OF string,
  2.       xml TYPE string.
  3. APPEND '112123123412345' TO lt.
  4. APPEND '445456456745678' TO lt.
  5. CALL TRANSFORMATION ztest
  6.   SOURCE lt = lt
  7.   RESULT XML xml.
  8. cl_abap_browser=>show_xml( xml ).

XSLT code


<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sap="http://www.sap.com/sapxsl" version="1.0">

  <xsl:strip-space elements="*"/>

  <xsl:template match="/">

    <root>

      <xsl:apply-templates select="//*/item"/>

    </root>

  </xsl:template>

  <xsl:template match="item">

    <Template1>

      <one>

        <xsl:value-of select="substring(concat(., ''), 1, 1)"/>

      </one>

      <two>

        <xsl:value-of select="substring(concat(., ''), 2, 2)"/>

      </two>

      <three>

        <xsl:value-of select="substring(concat(., ''), 4, 3)"/>

      </three>

      <four>

        <xsl:value-of select="substring(concat(., ''), 7, 4)"/>

      </four>

      <five>

        <xsl:value-of select="substring(concat(., ''), 11, 5)"/>

      </five>

    </Template1>

  </xsl:template>

</xsl:transform>

Output xml


<?xml version="1.0" encoding="utf-8" ?>
<root>
   <Template1>
     <one>1</one>
     <two>12</two>
     <three>123</three>
     <four>1234</four>
     <five>12345</five>
   </Template1>
   <Template1>
     <one>4</one>
     <two>45</two>
     <three>456</three>
     <four>4567</four>
     <five>45678</five>
   </Template1>
</root>

/.

4 REPLIES 4

matt
Active Contributor
0 Kudos
231

The simplest way is to convert your data first into an internal table with fields for name, address, salary etc.

Then use:

DATA my_output_xml TYPE string.

CALL TRANSFORMATION id RESULT xml my_output_xml SOURCE data = internal_table.

0 Kudos
231

Hi Matthrew,

I was also thinking on this simple solution.

If I could make the entirething(spliting xml into internal table and convert them into xml tags format) with in the transformation so that I don't need to make any changes in the program side.

I hope I need to get expertise on this xml so that I can wrote my own code.

Anyhow thanks for your reply on this.

Regards,

naresh.

Former Member
0 Kudos
232

Have a look at this snippet.

Substring function is used with offset/length to extract the substring , which is then populated into xml node.

ABAP code

  1. DATA: lt TYPE TABLE OF string,
  2.       xml TYPE string.
  3. APPEND '112123123412345' TO lt.
  4. APPEND '445456456745678' TO lt.
  5. CALL TRANSFORMATION ztest
  6.   SOURCE lt = lt
  7.   RESULT XML xml.
  8. cl_abap_browser=>show_xml( xml ).

XSLT code


<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sap="http://www.sap.com/sapxsl" version="1.0">

  <xsl:strip-space elements="*"/>

  <xsl:template match="/">

    <root>

      <xsl:apply-templates select="//*/item"/>

    </root>

  </xsl:template>

  <xsl:template match="item">

    <Template1>

      <one>

        <xsl:value-of select="substring(concat(., ''), 1, 1)"/>

      </one>

      <two>

        <xsl:value-of select="substring(concat(., ''), 2, 2)"/>

      </two>

      <three>

        <xsl:value-of select="substring(concat(., ''), 4, 3)"/>

      </three>

      <four>

        <xsl:value-of select="substring(concat(., ''), 7, 4)"/>

      </four>

      <five>

        <xsl:value-of select="substring(concat(., ''), 11, 5)"/>

      </five>

    </Template1>

  </xsl:template>

</xsl:transform>

Output xml


<?xml version="1.0" encoding="utf-8" ?>
<root>
   <Template1>
     <one>1</one>
     <two>12</two>
     <three>123</three>
     <four>1234</four>
     <five>12345</five>
   </Template1>
   <Template1>
     <one>4</one>
     <two>45</two>
     <three>456</three>
     <four>4567</four>
     <five>45678</five>
   </Template1>
</root>

/.

0 Kudos
231

Hi Manish,

You are right!, thanks.

Regards,

Naresh.