Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

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

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.

Not what you were looking for? View more on this topic or Ask a question