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: 

how to read attributes of XML file via XSLT program in abap

shilpaurandoor
Explorer
0 Kudos

Hi Experts,

I have an XML file as below and i need to read attribute values and fetch it to an internal table.

but the values are not fetched and the internal table remains empty. Please let me know if there is anything wrong with the code . Thank you so much in advance!

xml file

<?xml version="1.0" encoding="iso-8859-1" ?>
<CUSTOMERS>
  <PERSON customer_id="1" first_name="Jan" last_name="krohn">
  </PERSON>
  <PERSON customer_id="2" first_name="Jan1" last_name="krohn1">
  </PERSON>
</CUSTOMERS>

XSLT program

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 <xsl:output encoding="iso-8859-1" indent="yes" method="xml" version="1.0"/>
 <xsl:strip-space elements="*"/>
 <xsl:template match="/">
 <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
 <asx:values>
 <IPERSON>
 <xsl:for-each select="/CUSTOMERS/PERSON">
 <item>
 <CUST_ID>
 <xsl:value-of select="@customer_id"/>
 </CUST_ID>
 <FIRSTNAME>
 <xsl:value-of select="first_name"/>
 </FIRSTNAME>
 <LASTNAME>
 <xsl:value-of select="last_name"/>
 </LASTNAME>
 </item>
 </xsl:for-each>
 </IPERSON></asx:values>
 </asx:abap>
 </xsl:template>
</xsl:transform>

abap call :

TYPES: BEGIN OF ts_person,
 cust_id(4) TYPE c,
 firstname(20) TYPE c,
 lastname(20) TYPE c,
* ONE_STRING TYPE CHAR50,
 END OF ts_person.
DATA : it_data TYPE STANDARD TABLE OF ts_person, wa_data TYPE ts_person.
DATA: gt_result_xml TYPE abap_trans_resbind_tab, gs_result_xml TYPE abap_trans_resbind.
GET REFERENCE OF it_data INTO gs_result_xml-value. gs_result_xml-name = 'IPERSON'. APPEND gs_result_xml TO gt_result_xml.
CALL TRANSFORMATION ZXSLT_1 " xslt file above SOURCE XML it_xml RESULT (gt_result_xml).

2 REPLIES 2

TMSingh_SAP
Participant
0 Kudos

Hi Shilpa,

This looks like mismatch in Source XML => IT_XML and Result =>GT_RESULT_XML.Can you check this ?

Thanks,

Mohan

DoanManhQuynh
Active Contributor
0 Kudos

your code working fine, all data stored in gt_result_xml-value. one little problem is in transform you missing @ for first_name and last_name so for current code it only fetch customer_id.

if you are trying to get data from xml to it_data instead o gt_result_xml, you can set it as result parameter, or with current code, you have to deference gt_result_xml-value using field symbol:

CALL TRANSFORMATION ZXSLT_1
SOURCE XML xml
RESULT IPERSON = it_data.