2010 Feb 03 2:24 PM
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 = '<Friends>' &&
'<People>' &&
'<FirstName>Homer</FirstName>' &&
'<Surname>Simpson</Surname>' &&
'<Address>' &&
'<HouseNo>123</HouseNo>' &&
'<Street>Evergreen Terrace</Street>' &&
'<City>Springfield</City>' &&
'<PhoneNo>011212321</PhoneNo>' &&
'</Address>' &&
'<Address>' &&
'<HouseNo>7G</HouseNo>' &&
'<Street>Neuclear Power Plant</Street>' &&
'<City>Spring Field</City>' &&
'<PhoneNo>911</PhoneNo>' &&
'</Address>' &&
'</People>' &&
'<People>' &&
'<FirstName>Bart</FirstName>' &&
'<Surname>Simpson</Surname>' &&
'<Address>' &&
'<HouseNo>123x</HouseNo>' &&
'<Street>Evergreen Terracex</Street>' &&
'<City>Springfieldx</City>' &&
'<PhoneNo>011212321x</PhoneNo>' &&
'</Address>' &&
'</People>' &&
'</Friends>' .
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>
<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>
<ALL_PEOPLE>
<xsl:apply-templates select="//People"/>
</ALL_PEOPLE>
</asx:values>
</asx:abap>
</xsl:template>
<xsl:template match="People">
<ALLMYFRIENDS> <!This element name is not relevent... needed to just group the loop>
<FIRSTNAME>
<xsl:value-of select="FirstName"/>
</FIRSTNAME>
<SURNAME>
<xsl:value-of select="Surname"/>
</SURNAME>
<ADDRESSES>
<xsl:for-each select="Address">
<ADDRESS> <!This element name is not relevent... needed to just group the loop>
<HOUSE_NO>
<xsl:value-of select="HouseNo"/>
</HOUSE_NO>
<STREET_NAME>
<xsl:value-of select="Street"/>
</STREET_NAME>
<CITY_NAME>
<xsl:value-of select="City"/>
</CITY_NAME>
<PHONE_NO>
<xsl:value-of select="PhoneNo"/>
</PHONE_NO>
</ADDRESS>
</xsl:for-each>
</ADDRESSES>
</ALLMYFRIENDS>
</xsl:template>
</xsl:transform>
</PRE>
<HR>
HTH,
Jayanta.
2010 Apr 29 2:43 PM
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
2010 Aug 12 6:09 PM
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
2010 Aug 13 6:01 AM
you can write a blog for the same if it is already not availalble.
2010 Dec 29 7:53 PM
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