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:




*& 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.



SOURCE XML input_xml

RESULT all_people = lt_person.

CATCH cx_root.

WRITE 'Problemo!'.


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



and here is the XSLT Transformation program (xslt_person):



&lt;xsl:transform xmlns:xsl=""

xmlns:sap="" version="1.0"&gt;

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

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

&lt;asx:abap xmlns:asx="" version="1.0"&gt;



&lt;xsl:apply-templates select="//People"/&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;xsl:value-of select="FirstName"/&gt;



&lt;xsl:value-of select="Surname"/&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;xsl:value-of select="HouseNo"/&gt;



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



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



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












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