Skip to Content
0
Former Member
Feb 24, 2009 at 06:38 PM

XSLT Transformation not working when called in Program (works in xslt_tool)

345 Views

Hi,

I have a XSLT transformation which works when I test it directly (from TCode xslt_tool) but when I call it from a program it does not return any value to the internal table.

I have posted all the codes below. If I copy the XML to a note pad and save it as a XML file, then this works when I test it via XSLT_TOOL directly.

Please provide me some tips,..

The actutal XML file:

<xml_api_reply version="1">
<weather module_id="0" tab_id="0">
<forecast_information>
<city data="Jackson Heights, NY"/>
<postal_code data="11372"/>
<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2009-02-19"/>
<current_date_time data="2009-02-19 15:19:41 +0000"/>
<unit_system data="US"/>
</forecast_information>
<current_conditions>
<condition data="Mostly Cloudy"/>
<temp_f data="50"/>
<temp_c data="10"/>
<humidity data="Humidity: 63%"/>
<icon data="/images/weather/mostly_cloudy.gif"/>
<wind_condition data="Wind: W at 8 mph"/>
</current_conditions><forecast_conditions>
<day_of_week data="Thu"/>
<low data="25"/>
<high data="47"/>
<icon data="/images/weather/chance_of_snow.gif"/>
<condition data="Chance of Snow Showers"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="Fri"/>
<low data="25"/>
<high data="34"/>
<icon data="/images/weather/mostly_sunny.gif"/>
<condition data="Mostly Sunny"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="Sat"/>
<low data="27"/>
<high data="38"/>
<icon data="/images/weather/mostly_sunny.gif"/>
<condition data="Mostly Sunny"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="Sun"/>
<low data="27"/>
<high data="40"/>
<icon data="/images/weather/chance_of_rain.gif"/>
<condition data="Chance of Rain"/>
</forecast_conditions>
</weather>
</xml_api_reply>

ABAP Program:

REPORT  z_xml_to_abap4                          .
TYPE-POOLS abap.
TYPES: BEGIN OF ts_forecast,
       day TYPE string,
       low TYPE string,
       high TYPE string,
       icon TYPE string,
       condition TYPE string,
       END OF ts_forecast.

DATA: gt_forecast TYPE STANDARD TABLE OF ts_forecast,
      gs_forecast TYPE ts_forecast.

DATA: gt_itab       TYPE STANDARD TABLE OF char2048.
DATA: gs_itab LIKE LINE OF gt_itab.

DATA: gt_result_xml TYPE abap_trans_resbind_tab,
      gs_result_xml TYPE abap_trans_resbind.

DATA: gs_rif_ex     TYPE REF TO cx_root,
      gs_var_text   TYPE string.


gs_itab = '<xml_api_reply version="1">'.
APPEND gs_itab TO gt_itab.

gs_itab = '<weather module_id="0" tab_id="0">'.
APPEND gs_itab TO gt_itab.

gs_itab = '<forecast_information>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<city data="Jackson Heights, NY"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<postal_code data="11372"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<latitude_e6 data=""/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<longitude_e6 data=""/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<forecast_date data="2009-02-19"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<current_date_time data="2009-02-19 15:19:41 +0000"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<unit_system data="US"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '</forecast_information>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<current_conditions>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<condition data="Mostly Cloudy"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<temp_f data="50"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<temp_c data="10"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<humidity data="Humidity: 63%"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<icon data="/images/weather/mostly_cloudy.gif"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<wind_condition data="Wind: W at 8 mph"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '</current_conditions><forecast_conditions>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<day_of_week data="Thu"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<low data="25"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<high data="47"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<icon data="/images/weather/chance_of_snow.gif"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<condition data="Chance of Snow Showers"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '</forecast_conditions>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<forecast_conditions>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<day_of_week data="Fri"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<low data="25"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<high data="34"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<icon data="/images/weather/mostly_sunny.gif"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<condition data="Mostly Sunny"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '</forecast_conditions>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<forecast_conditions>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<day_of_week data="Sat"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<low data="27"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<high data="38"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<icon data="/images/weather/mostly_sunny.gif"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<condition data="Mostly Sunny"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '</forecast_conditions>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<forecast_conditions>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<day_of_week data="Sun"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<low data="27"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<high data="40"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<icon data="/images/weather/chance_of_rain.gif"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '<condition data="Chance of Rain"/>'.
APPEND gs_itab TO gt_itab.

gs_itab = '</forecast_conditions>'.
APPEND gs_itab TO gt_itab.

gs_itab = '</weather>'.
APPEND gs_itab TO gt_itab.

gs_itab = '</xml_api_reply>'.
APPEND gs_itab TO gt_itab.


GET REFERENCE OF gt_forecast INTO gs_result_xml-value.
gs_result_xml-name = 'IFORECAST'.
APPEND gs_result_xml TO gt_result_xml.

TRY.

    CALL TRANSFORMATION z_xml_to_abap2
    SOURCE XML gt_itab
    RESULT (gt_result_xml).

  CATCH cx_root INTO gs_rif_ex.

    gs_var_text = gs_rif_ex->get_text( ).
    MESSAGE gs_var_text TYPE 'E'.

ENDTRY.

LOOP AT gt_forecast INTO gs_forecast.
  WRITE:/ gs_forecast-low.
  WRITE:/ gs_forecast-high.
  WRITE:/ gs_forecast-icon.
ENDLOOP.

XSLT Transformation:

<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>
        <IFORECAST>
          <xsl:apply-templates select="//forecast_conditions"/>
        </IFORECAST>
      </asx:values>
    </asx:abap>
  </xsl:template>

  <xsl:template match="forecast_conditions">
    <item>
      <day>
        <xsl:value-of select="//day_of_week/@data"/>
      </day>
      <low>
        <xsl:value-of select="//low/@data"/>
      </low>
      <high>
        <xsl:value-of select="//high/@data"/>
      </high>
      <icon>
        <xsl:value-of select="//icon/@data"/>
      </icon>
      <condition>
        <xsl:value-of select="//condition/@data"/>
      </condition>
    </item>
  </xsl:template>

</xsl:transform>

Thanks,

CD