Application Development and Automation 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: 
Read only

transform xml to ABAP

Former Member
0 Likes
918

Hi Experts,

I have an source xml file as shown below.i need the value from tag <DISPLAY_KEY>row2col2</DISPLAY_KEY>

for every <element index = 0> and <element index = 1> as they refer to rows and cols. Can someone please help getting the data in to internal table?

- <RESULT_SET state="DATA_AVAILABLE">
- <AXES>
- <AXIS name="ROWS">
- <TUPLES size="7">
- <TUPLE>
- <ELEMENT index="0"> -- *row1 col1*  
<DISPLAY_LEVEL>0</DISPLAY_LEVEL> 
  <DRILL_STATE>LEAF</DRILL_STATE> 
- <MEMBER name="row1col1" type="MEMBER" text="row1col1">
- <pre><code>
  <DISPLAY_KEY>row1col1</DISPLAY_KEY> 
  </code></pre>
  </MEMBER>
  </ELEMENT>
- <ELEMENT index="1"> -- *row1 col2*  
<DISPLAY_LEVEL>0</DISPLAY_LEVEL> 
  <DRILL_STATE>LEAF</DRILL_STATE> 
- <MEMBER name="row1col2" type="MEMBER" text="row1col2">
- <pre><code>
  <DISPLAY_KEY>row1col2</DISPLAY_KEY> 
  </code></pre>
  </MEMBER>
  </ELEMENT>
  </TUPLE>
- <TUPLE>
- <ELEMENT index="0">. -- *row2 col1*  
<DRILL_STATE>LEAF</DRILL_STATE> 
- <MEMBER name="row2col1" type="MEMBER" text="row2col1">
- <pre><code>
  <DISPLAY_KEY>row2col1</DISPLAY_KEY> 
  </code></pre>
  </MEMBER>
  </ELEMENT>
- <ELEMENT index="1"> -- *row2 col2*  
 <DISPLAY_LEVEL>0</DISPLAY_LEVEL> 
  <DRILL_STATE>LEAF</DRILL_STATE> 
- <MEMBER name="row2col2" type="MEMBER" text="row2col2">
- <pre><code>
  <DISPLAY_KEY>row2col2</DISPLAY_KEY> 
  </code></pre>
  </MEMBER>
  </ELEMENT>
  </TUPLE>

Edited by: Ananda Theerthan on May 6, 2011 11:59 AM

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
831

Hi Chen,

I did exactly what you said but column values are populating at rows(one below the other).If Element index = 0, then col1, if Element index = 1 then col 2.How do I get this output?Can you please help?

6 REPLIES 6
Read only

Former Member
0 Likes
831

Hi Ananda,

You can try the below code,


TYPES: BEGIN OF t_data,
         display_key TYPE char20,
       END OF t_data.

DATA: input_xml TYPE string,	" Pass the XML string to this
      lt_data   TYPE STANDARD TABLE OF t_data.


TRY.
    CALL TRANSFORMATION zxslt_get_data " Create the XSLT prog using the code given further below
    SOURCE XML input_xml 
    RESULT result_set = lt_data.
  CATCH cx_root.
    WRITE 'Problemo!'.
ENDTRY.

IF sy-subrc EQ 0.

ENDIF.

Code for the XSLT program, create this using Tcode: STRANS


<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="/">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<RESULT_SET>
<xsl:apply-templates select="//TUPLES"/>
</RESULT_SET>
</asx:values>
</asx:abap>
</xsl:template>
<xsl:template match="TUPLES">
<xsl:for-each select="TUPLE">
<DISP>
<DISPLAY_KEY>
<xsl:value-of select="ELEMENT/MEMBER/PRESENTATIONS/DISPLAY_KEY"/>
</DISPLAY_KEY>
</DISP>
</xsl:for-each>
</xsl:template>
</xsl:transform>

Also, i think the XML posted by you was not consistent, below is how i modified the XML content


<?xml version="1.0" encoding="UTF-8"?>
<RESULT_SET state="DATA_AVAILABLE">
<AXES>
<AXIS name="ROWS">
<TUPLES size="7">
<TUPLE>
<ELEMENT index="0">
<DISPLAY_LEVEL>0</DISPLAY_LEVEL>
<DRILL_STATE>LEAF</DRILL_STATE>
<MEMBER name="row1col1" type="MEMBER" text="row1col1">
<pre><code>
<DISPLAY_KEY>row1col1</DISPLAY_KEY>
</code></pre>
</MEMBER>
</ELEMENT>
</TUPLE>
<TUPLE>
<ELEMENT index="1">
<DISPLAY_LEVEL>0</DISPLAY_LEVEL>
<DRILL_STATE>LEAF</DRILL_STATE>
<MEMBER name="row1col2" type="MEMBER" text="row1col2">
<pre><code>
<DISPLAY_KEY>row1col2</DISPLAY_KEY>
</code></pre>
</MEMBER>
</ELEMENT>
</TUPLE>
<TUPLE>
<ELEMENT index="0">
<DRILL_STATE>LEAF</DRILL_STATE>
<MEMBER name="row2col1" type="MEMBER" text="row2col1">
<pre><code>
<DISPLAY_KEY>row2col1</DISPLAY_KEY>
</code></pre>
</MEMBER>
</ELEMENT>
</TUPLE>
<TUPLE>
<ELEMENT index="1">
<DISPLAY_LEVEL>0</DISPLAY_LEVEL>
<DRILL_STATE>LEAF</DRILL_STATE>
<MEMBER name="row2col2" type="MEMBER" text="row2col2">
<pre><code>
<DISPLAY_KEY>row2col2</DISPLAY_KEY>
</code></pre>
</MEMBER>
</ELEMENT>
</TUPLE>
</TUPLES>
</AXIS>
</AXES>
</RESULT_SET>

All the best!!!

Regards,

Chen

Read only

0 Likes
831

Hi Chen,

thanks for the help.Your code works great except that it fetches values only from the first element.I think the loop needs to be done for <ELEMENT> as well.Can you please let me know how to acheive this?

Please find below the XML string.

- <RESULT_SET state="DATA_AVAILABLE">
- <AXES>
- <AXIS name="ROWS">
- <TUPLES size="7">
- <TUPLE>
- <ELEMENT index="0">
  <DISPLAY_LEVEL>0</DISPLAY_LEVEL> 
  <DRILL_STATE>LEAF</DRILL_STATE> 
- <MEMBER name="i1" type="MEMBER" text="i1">
- <pre><code>
  <DISPLAY_KEY>r1</DISPLAY_KEY> 
  </code></pre>
  </MEMBER>
  </ELEMENT>
- <ELEMENT index="1">
  <DISPLAY_LEVEL>0</DISPLAY_LEVEL> 
  <DRILL_STATE>LEAF</DRILL_STATE> 
- <MEMBER name="c1" type="MEMBER" text="c1">
- <pre><code>
  <DISPLAY_KEY>c1</DISPLAY_KEY> 
  </code></pre>
  </MEMBER>
  </ELEMENT>
  </TUPLE>
- <TUPLE>
- <ELEMENT index="0">
  <DISPLAY_LEVEL>0</DISPLAY_LEVEL> 
  <DRILL_STATE>LEAF</DRILL_STATE> 
- <MEMBER name="r2" type="MEMBER" text="r2">
- <pre><code>
  <DISPLAY_KEY>r2</DISPLAY_KEY> 
  </code></pre>
  </MEMBER>
  </ELEMENT>
- <ELEMENT index="1">
  <DISPLAY_LEVEL>0</DISPLAY_LEVEL> 
  <DRILL_STATE>LEAF</DRILL_STATE> 
- <MEMBER name="c2" type="MEMBER" text="c2">
- <pre><code>
  <DISPLAY_KEY>c2</DISPLAY_KEY> 
  </code></pre>
  </MEMBER>
  </ELEMENT>
  </TUPLE>
  </TUPLES>
  </AXIS>
</AXES></RESULT_SET>

Regards,

Anand

Edited by: Ananda Theerthan on May 9, 2011 8:53 AM

Edited by: Ananda Theerthan on May 9, 2011 8:54 AM

Read only

0 Likes
831

Hi Ananda,

You just need to add another loop at the "ELEMENT" node, and change the access path for the DISPLAY_KEY value select.


<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="/">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<RESULT_SET>
<xsl:apply-templates select="//TUPLES"/>
</RESULT_SET>
</asx:values>
</asx:abap>
</xsl:template>
<xsl:template match="TUPLES">
<xsl:for-each select="TUPLE">
<xsl:for-each select="ELEMENT"> " Additional loop for Node Element
<DISP>
<DISPLAY_KEY>
<xsl:value-of select="MEMBER/PRESENTATIONS/DISPLAY_KEY"/> "Changed access path
</DISPLAY_KEY>
</DISP>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:transform>

Regards,

Chen

Read only

0 Likes
831

Thanks Chen.That piece of code solved my problem.

Read only

Former Member
0 Likes
832

Hi Chen,

I did exactly what you said but column values are populating at rows(one below the other).If Element index = 0, then col1, if Element index = 1 then col 2.How do I get this output?Can you please help?

Read only

0 Likes
831

Hi Ananda,

Change the local type structure as below,


TYPES: BEGIN OF t_data,
         display_key_0 TYPE char20,
         display_key_1 TYPE char20,
       END OF t_data.

try with the below XSLT program, this should work,


<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="/">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<RESULT_SET>
<xsl:apply-templates select="//TUPLES"/>
</RESULT_SET>
</asx:values>
</asx:abap>
</xsl:template>
<xsl:template match="TUPLES">
<xsl:for-each select="TUPLE">
<DISP>
<xsl:for-each select="ELEMENT">
<xsl:choose>
<xsl:when test="@index = 0">
<DISPLAY_KEY_0>
<xsl:value-of select="MEMBER/PRESENTATIONS/DISPLAY_KEY"/>
</DISPLAY_KEY_0>
</xsl:when>
<xsl:when test="@index = 1">
<DISPLAY_KEY_1>
<xsl:value-of select="MEMBER/PRESENTATIONS/DISPLAY_KEY"/>
</DISPLAY_KEY_1>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</DISP>
</xsl:for-each>
</xsl:template>
</xsl:transform>

Regards,

Chen

Edited by: Chen K V on May 9, 2011 4:19 PM