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

1.7k views Asked by At

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).
1

There are 1 answers

0
Anton Shchyrov On BEST ANSWER

You missing @ sign for name of attributes first_name and last_name

<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>