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: 

XSLT samples for XML->ABAP mapping

Former Member
0 Kudos

Hi all,

Does anyone have a XSLT samples for XML->ABAP mapping ?

regards

3 REPLIES 3

Former Member
0 Kudos

hi, XSLT is not a part of SAP.

So if you want to convert XML to other format, and don't familiar with XSLT, there is some tools can help you.

Stylus, it is a good tools for XML develope, include XSLT. With it, you can easily design the convert rule in a graphic way, and you can get the equivalent XSLT instantly.

And when you get the XSLT, you can copy it into SAP server, create a transformation XXX

though the CALL TRANSFORMATION XXX, you can convert XML into abap world much easily.

hope it will be helpful.

0 Kudos

first create XSLT program by copy pasting the below given code and give the program name as "Y_TEST"

<b>XSLT code</b>


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:strip-space elements="*"/>

  <xsl:output indent="yes"/>


  <xsl:template match="NewDataSet">
    <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
      <asx:values>
        <OUTTAB>
          <xsl:for-each select="Table">

            <OUTTAB1>
              <AIRPORTCODE>
                <xsl:value-of select="AirportCode"/>
              </AIRPORTCODE>
              <CITYOFAIRPORTNAME>
                <xsl:value-of select="CityOrAirportName"/>
              </CITYOFAIRPORTNAME>
              <COUNTRY>
                <xsl:value-of select="Country"/>
              </COUNTRY>
              <COUNTRYABBRIVATION>
                <xsl:value-of select="CountryAbbrviation"/>
              </COUNTRYABBRIVATION>
              <COUNTRYCODE>
                <xsl:value-of select="CountryCode"/>
              </COUNTRYCODE>
              <GMTOFFSET>
                <xsl:value-of select="GMTOffset"/>
              </GMTOFFSET>
              <RUNWAYLENGTHFEET>
                <xsl:value-of select="RunwayLengthFeet"/>
              </RUNWAYLENGTHFEET>
              <RUNWAYELEVATIONFEET>
                <xsl:value-of select="RunwayElevationFeet"/>
              </RUNWAYELEVATIONFEET>
              <LATITUDEDEGREE>
                <xsl:value-of select="LatitudeDegree"/>
              </LATITUDEDEGREE>
              <LATITUDEMINUTE>
                <xsl:value-of select="LatitudeMinute"/>
              </LATITUDEMINUTE>
              <LATITUDESECOND>
                <xsl:value-of select="LatitudeSecond"/>
              </LATITUDESECOND>
              <LATITUDENPEERS>
                <xsl:value-of select="LatitudeNpeerS"/>
              </LATITUDENPEERS>
              <LONGITUDEDEGREE>
                <xsl:value-of select="LongitudeDegree"/>
              </LONGITUDEDEGREE>
              <LONGITUDEMINUTE>
                <xsl:value-of select="LongitudeMinute"/>
              </LONGITUDEMINUTE>
              <LONGITUDESECONDS>
                <xsl:value-of select="LongitudeSeconds"/>
              </LONGITUDESECONDS>
              <LONGITUDEEPERW>
                <xsl:value-of select="LongitudeEperW"/>
              </LONGITUDEEPERW>
            </OUTTAB1>
          </xsl:for-each>
        </OUTTAB>

      </asx:values>
    </asx:abap>



  </xsl:template>

</xsl:stylesheet>

<b>just create a type 1 program and paste the below given code.</b>

report y_consume_webservice .


data: wf_user type string .
data: wf_password type string .

types: begin of outtab1 ,
   airportcode(6)  ,
   cityofairportname(50),
   country(30)  ,
   countryabbrivation(10),
   countrycode(6)  ,
   gmtoffset(10)  ,
   runwaylengthfeet(15),
   runwayelevationfeet(15),
   latitudedegree(10)  ,
   latitudeminute(10)  ,
   latitudesecond(10)  ,
   latitudenpeers(10)  ,
   longitudedegree(10)  ,
   longitudeminute(10)  ,
   longitudeseconds(10)  ,
   longitudeeperw(10) ,
   end of outtab1 .

data: outtab type  table of outtab1.
data: wf_o like line of outtab .

data: g_okcode like sy-ucomm .
data: my_container   type ref to cl_gui_custom_container .
data: g_dock type ref to cl_gui_docking_container .

data: mygrid type ref to cl_gui_alv_grid .
data: wf_field_cat type lvc_t_fcat .
data: wf_field_cat_wa like line of wf_field_cat ,
      wf_is_layout type lvc_s_layo .
data: wf_fld_cat type slis_t_fieldcat_alv .
data: wf_fld_cat_wa like line of wf_fld_cat .
data: wf_repid like sy-repid .
data: int_tab_name type slis_tabname .
data: xslt_err type ref to cx_xslt_exception .
constants:
* encoding for download of XML files
encoding     type string value 'utf-8' .

data: rlength type i,
      txlen type string  .

data: http_client type ref to if_http_client .

data: wf_string type string .
data: wf_string1 type string .
data: wf_proxy type string ,
      wf_port type string .

selection-screen: begin of block a with frame .
parameters: uri2(132) type c lower case .

selection-screen skip 1.
parameters: user(50) lower case,
            password(50) lower case ,
            p_proxy(100) lower case default 'proxy.xxx.com' ,
            p_port(4) default '80'.

selection-screen: end of block a .

at selection-screen output.

  loop at screen.
    if screen-name = 'PASSWORD'.
      screen-invisible = '1'.
      modify screen.
    endif.
  endloop.

start-of-selection .


clear wf_string .

concatenate
'<?xml version="1.0" encoding="utf-8"?>'

'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'
'<soap:Body>'
'<GetAirportInformationByCountry xmlns="http://www.webserviceX.NET">'

'<country>' uri2 '</country>'

'</GetAirportInformationByCountry>'

'</soap:Body>'

'</soap:Envelope>'

into wf_string .

clear :rlength , txlen .
rlength = strlen( wf_string ) .
move: rlength to txlen .




clear: wf_proxy, wf_port .
move: p_proxy to wf_proxy ,
      p_port to wf_port .

call method cl_http_client=>create
  exporting
    host          = 'www.webservicex.net'
    service       = '80'
    scheme        = '1'
    proxy_host    =  wf_proxy
    proxy_service =  wf_port
  importing
    client        = http_client.

http_client->propertytype_logon_popup = http_client->co_disabled.

wf_user = user .
wf_password = password .

call method http_client->authenticate
  exporting
    proxy_authentication = 'X'
    username             = wf_user
    password             = wf_password.


call method http_client->request->set_header_field
  exporting
    name  = '~request_method'
    value = 'POST'.

call method http_client->request->set_header_field
  exporting
    name  = '~server_protocol'
    value = 'HTTP/1.1'.

call method http_client->request->set_header_field
  exporting
    name  = '~request_uri'
    value = '/airport.asmx'.

call method http_client->request->set_header_field
  exporting
    name  = 'Content-Type'
    value = 'text/xml; charset=utf-8'.

call method http_client->request->set_header_field
  exporting
    name  = 'Content-Length'
    value = txlen.

call method http_client->request->set_header_field
  exporting
    name  = 'SOAPAction'
    value = 'http://www.webserviceX.NET/GetAirportInformationByCountry'.

call method http_client->request->set_cdata
  exporting
    data   = wf_string
    offset = 0
    length = rlength.

call method http_client->send
  exceptions
    http_communication_failure = 1
    http_invalid_state         = 2.

call method http_client->receive
  exceptions
    http_communication_failure = 1
    http_invalid_state         = 2
    http_processing_failed     = 3.

clear wf_string1 .
wf_string1 = http_client->response->get_cdata( ).

replace all occurrences of
    '<' in wf_string1 with '<' .

replace all occurrences of
 '>' in wf_string1 with '>' .

replace all occurrences of
 'xmlns=' in wf_string1 with 'xmlns:xsl=' .

try .
    call transformation (`Y_TEST`)
            source xml wf_string1
            result     outtab = outtab.
  catch cx_xslt_exception into xslt_err.

    data: s type string.
    s = xslt_err->get_text( ).
    write: ': ', s.
    stop.

endtry .

break-point .

Try this and give me your feedback.

Regards

Raja

0 Kudos

Hi Boris,

look in packages SXSLT_DEMO and SST_DEMO (if available in your system). Try the transactions SSTDEMO2 or SXSLTDEMO1. Watch what's happening in the debugger (breakpoint at CALL TRANSFORMATION).

Cheers - Karsten