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: 

ABAP XSLT transformation - XML to deep structure/nested standard table

Former Member
0 Kudos
690

Hi all,

I was struggling with this topic recently and couldn't find a single working example or description of a possible solution. So now that I've sorted it out, I did a quick example to elustrate how it works. Here is the code with XML embeded in it and the XSLT follows:

<HR>

<PRE>

&----


*& Report Z_XML2ABAP

*&

&----


*& Author: Jayanta Roy

*& Date: 03/02/2010

&----


REPORT z_xml2abap.

DATA input_xml TYPE string.

TYPES: BEGIN OF t_address,

house_no TYPE string,

street_name TYPE string,

city_name TYPE string,

phone_no TYPE string,

END OF t_address.

TYPES: t_addresses TYPE STANDARD TABLE OF t_address with NON-UNIQUE KEY house_no.

TYPES: BEGIN OF t_person,

firstname TYPE string,

surname TYPE string,

addresses TYPE t_addresses,

END OF t_person.

input_xml = '&lt;Friends&gt;' &&

'&lt;People&gt;' &&

'&lt;FirstName&gt;Homer&lt;/FirstName&gt;' &&

'&lt;Surname&gt;Simpson&lt;/Surname&gt;' &&

'&lt;Address&gt;' &&

'&lt;HouseNo&gt;123&lt;/HouseNo&gt;' &&

'&lt;Street&gt;Evergreen Terrace&lt;/Street&gt;' &&

'&lt;City&gt;Springfield&lt;/City&gt;' &&

'&lt;PhoneNo&gt;011212321&lt;/PhoneNo&gt;' &&

'&lt;/Address&gt;' &&

'&lt;Address&gt;' &&

'&lt;HouseNo&gt;7G&lt;/HouseNo&gt;' &&

'&lt;Street&gt;Neuclear Power Plant&lt;/Street&gt;' &&

'&lt;City&gt;Spring Field&lt;/City&gt;' &&

'&lt;PhoneNo&gt;911&lt;/PhoneNo&gt;' &&

'&lt;/Address&gt;' &&

'&lt;/People&gt;' &&

'&lt;People&gt;' &&

'&lt;FirstName&gt;Bart&lt;/FirstName&gt;' &&

'&lt;Surname&gt;Simpson&lt;/Surname&gt;' &&

'&lt;Address&gt;' &&

'&lt;HouseNo&gt;123x&lt;/HouseNo&gt;' &&

'&lt;Street&gt;Evergreen Terracex&lt;/Street&gt;' &&

'&lt;City&gt;Springfieldx&lt;/City&gt;' &&

'&lt;PhoneNo&gt;011212321x&lt;/PhoneNo&gt;' &&

'&lt;/Address&gt;' &&

'&lt;/People&gt;' &&

'&lt;/Friends&gt;' .

DATA lt_person TYPE STANDARD TABLE OF t_person.

TRY.

CALL TRANSFORMATION xslt_person

SOURCE XML input_xml

RESULT all_people = lt_person.

CATCH cx_root.

WRITE 'Problemo!'.

ENDTRY.

WRITE 'Now, debug the program to see the values read from the XML'.

</PRE>

<HR>

and here is the XSLT Transformation program (xslt_person):

<HR>

<PRE>

&lt;xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:sap="http://www.sap.com/sapxsl" version="1.0"&gt;

&lt;xsl:strip-space elements="*"/&gt;

&lt;xsl:template match="/"&gt;

&lt;asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0"&gt;

&lt;asx:values&gt;

&lt;ALL_PEOPLE&gt;

&lt;xsl:apply-templates select="//People"/&gt;

&lt;/ALL_PEOPLE&gt;

&lt;/asx:values&gt;

&lt;/asx:abap&gt;

&lt;/xsl:template&gt;

&lt;xsl:template match="People"&gt;

&lt;ALLMYFRIENDS&gt; &lt;!This element name is not relevent... needed to just group the loop&gt;

&lt;FIRSTNAME&gt;

&lt;xsl:value-of select="FirstName"/&gt;

&lt;/FIRSTNAME&gt;

&lt;SURNAME&gt;

&lt;xsl:value-of select="Surname"/&gt;

&lt;/SURNAME&gt;

&lt;ADDRESSES&gt;

&lt;xsl:for-each select="Address"&gt;

&lt;ADDRESS&gt; &lt;!This element name is not relevent... needed to just group the loop&gt;

&lt;HOUSE_NO&gt;

&lt;xsl:value-of select="HouseNo"/&gt;

&lt;/HOUSE_NO&gt;

&lt;STREET_NAME&gt;

&lt;xsl:value-of select="Street"/&gt;

&lt;/STREET_NAME&gt;

&lt;CITY_NAME&gt;

&lt;xsl:value-of select="City"/&gt;

&lt;/CITY_NAME&gt;

&lt;PHONE_NO&gt;

&lt;xsl:value-of select="PhoneNo"/&gt;

&lt;/PHONE_NO&gt;

&lt;/ADDRESS&gt;

&lt;/xsl:for-each&gt;

&lt;/ADDRESSES&gt;

&lt;/ALLMYFRIENDS&gt;

&lt;/xsl:template&gt;

&lt;/xsl:transform&gt;

</PRE>

<HR>

HTH,

Jayanta.

4 REPLIES 4

Former Member
0 Kudos
138

Great stuff Jayanta! Have been searching through blogs and threads for quite a while now to find a solution for this problem. Thanks alot for posting your solution!

Best regards,

Ivo

Former Member
0 Kudos
138

Brilliant solution - Thanks very much for taking the trouble to share it. I was told i would have to have multiple templates and multiple abap tables to cope with deep structures. This is a much neater option.

Sheila

0 Kudos
138

you can write a blog for the same if it is already not availalble.

antony_john
Explorer
0 Kudos
138

thanks a LOT Jayanta..

I was looking for an XSLT example for some time.. this one atleast got me started in the right direction..

THANKS