Posted: Sat Sep 13, 2008 5:42 pm Post subject: XML XSLT with ABAP
Author: Steffen Fröhlich
XML XSLT with ABAP
Create ABAP coding
At first we create the internal table TYPES and DATA definition, we want to fill with the XML data. I have declared the table "it_airplus" like the structure from XML file definition for a better overview, because it is a long XML Definition (see the XSD file in the sample ZIP container by airplus.com)
Code:
*the declaration
TYPES: BEGIN OF t_sum_vat_sum,
a_rate(5),
net_value(15),
vat_value(15),
END OF t_sum_vat_sum.
TYPES: BEGIN OF t_sum_total_sale,
a_currency(3),
net_total(15),
vat_total(15),
vat_sum TYPE REF TO t_sum_vat_sum,
END OF t_sum_total_sale.
TYPES: BEGIN OF t_sum_total_bill,
net_total(15),
vat_total(15),
vat_sum TYPE t_sum_vat_sum,
add_ins_val(15),
total_bill_amount(15),
END OF t_sum_total_bill.TYPES: BEGIN OF t_ap_summary,
a_num_inv_det(5),
total_sale_values TYPE t_sum_total_sale,
total_bill_values TYPE t_sum_total_bill,
END OF t_ap_summary.TYPES: BEGIN OF t_ap,
head TYPE t_ap_head,
details TYPE t_ap_details,
summary TYPE t_ap_summary,
END OF t_ap.DATA: it_airplus TYPE STANDARD TABLE OF t_ap
...
...
*call the transformation
CALL TRANSFORMATION ZFI_AIRPLUS
SOURCE xml l_xml_x1
RESULT xml_output = it_airplus
.
Create XSLT program
There are two options to create a XSLT program:
Tcode: SE80 -> create/choose packet -> right click on it | Create -> Others -> XSL Transformation
Tcode: XSLT_TOOL
For a quick overview you can watch at the SXSLTDEMO* programs.
In this example we already use the three XSLT options explained later.
As you can see we define a XSL and ASX (ABAP) tags to handle the ABAP and XML variables/tags. After "<asx:values>" we create the XML_OUTPUT tag, but in this case we also could speak about a container. In fact the XML_OUTPUT tag comprise the structure of the internal table "it_airplus" which where moved by the CALL TRANSFORMATION in the report.
Read data from XML file via XSLT program
Report to read out the whole samle XML file provided by Airplus.
Code:
REPORT ZFI_AIRPLUS.
TYPE-POOLS: abap, ixml.
TYPES: BEGIN OF t_xml_line,
data(256) TYPE x,
END OF t_xml_line.
*Typdefinition für Airplus
*READ THE DOCUMENTATION "LASG_XML_INVOICE_BTM"!!!
*VARs beginning with "a_*" are ATTRIBUTES
TYPES: BEGIN OF t_inv_number,
number(30),
extension(3),
sequence(1),
END OF t_inv_number.
TYPES: BEGIN OF t_inv_part_address,
addressline(50),
post_code(6),
city(12),
state_code(6),
country_code(6),
country(12),
END OF t_inv_part_address.
TYPES: BEGIN OF t_inv_part_payee,
partycode(10),
address TYPE t_inv_part_address,
END OF t_inv_part_payee.
TYPES: BEGIN OF t_inv_part_invoicee,
billing_level(50),
partycode(10),
address TYPE t_inv_part_address,
END OF t_inv_part_invoicee.
TYPES: BEGIN OF t_inv_parties,
payee TYPE t_inv_part_payee,
invoicee TYPE t_inv_part_invoicee,
END OF t_inv_parties.
TYPES: BEGIN OF t_ap_head,
a_language(5),
a_direct_debit_qual(3),
inv_date(8),
inv_number TYPE t_inv_number,
credit_debit_qualifier(1),
credit_debit_label(10),
billing_currency(3),
base_currency(3),
due_date(8),
inv_parties TYPE t_inv_parties,
inv_qualifier(4),
END OF t_ap_head.
*---------------------------------------->
TYPES: BEGIN OF t_det_customer,
cardnumber(50),
name(50),
END OF t_det_customer.
TYPES: BEGIN OF t_det_serv_prov,
code(15),
companyname(50),
street(50),
place(50),
tax_code(50),
END OF t_det_serv_prov.
TYPES: BEGIN OF t_det_cust_dat,
personal_id(50),
dep_code(30),
cost_center(30),
acc_unit(30),
acc_number(30),
file_date(8),
proj_number(30),
order_number(30),
action_code(15),
destination(30),
txn_ref(15),
cust_ref(30),
dom_tag(5),
END OF t_det_cust_dat.
TYPES: BEGIN OF t_det_values_sum,
a_rate(5),
vat_value(15),
net_value(15),
END OF t_det_values_sum.
TYPES: BEGIN OF t_det_values,
a_sale_to_base_rate,
currency(3),
net_value(15),
vat_value(15),
gross_value(15),
add_ins_val(15),
line_gross_val(15),
vat_sum TYPE t_det_values_sum,
END OF t_det_values.
TYPES: BEGIN OF t_desc_sal_prov,
agency(30),
branch(15),
END OF t_desc_sal_prov.
TYPES: BEGIN OF t_desc_db_travel,
start_stat(50),
dest_stat(50),
db_class(15),
adults(2),
children(2),
END OF t_desc_db_travel.
TYPES: BEGIN OF t_desc_ht_book,
start_date(8),
end_date(8),
amount(2),
END OF t_desc_ht_book.
TYPES: BEGIN OF t_desc_serv_desc,
date(8),
doc_number(30),
voucher_number(30),
cc_code(15),
class(10),
txn_type_detail(3),
etix_qual(1),
flight_tax(15),
a_add_info_line(5),
add_info(50),
db_travel_data TYPE t_desc_db_travel,
ht_book_data TYPE t_desc_ht_book,
END OF t_desc_serv_desc.
TYPES: BEGIN OF t_det_det_desc_line,
sales_prov TYPE t_desc_sal_prov,
sale_val TYPE t_det_values,
bill_val TYPE t_det_values,
serv_desc TYPE t_desc_serv_desc,
END OF t_det_det_desc_line.
TYPES: BEGIN OF t_det_det_desc,
detail_desc TYPE t_det_det_desc_line,
END OF t_det_det_desc.
TYPES: BEGIN OF t_ap_detail,
a_line(5),
a_typ(30),
customer TYPE t_det_customer,
service_prov TYPE t_det_serv_prov,
customer_dat TYPE t_det_cust_dat,
sales_date(8),
proce_date(8),
onl_ord_ref(50),
sale_values TYPE t_det_values,
bill_values TYPE t_det_values,
det_descrip TYPE t_det_det_desc,
END OF t_ap_detail.
TYPES: BEGIN OF t_ap_details,
inv_detail TYPE t_ap_detail,
END OF t_ap_details.
TYPES: BEGIN OF t_sum_vat_sum,
a_rate(5),
net_value(15),
vat_value(15),
END OF t_sum_vat_sum.
TYPES: BEGIN OF t_sum_total_sale,
a_currency(3),
net_total(15),
vat_total(15),
vat_sum TYPE t_sum_vat_sum,
END OF t_sum_total_sale.
TYPES: BEGIN OF t_sum_total_bill,
net_total(15),
vat_total(15),
vat_sum TYPE t_sum_vat_sum,
add_ins_val(15),
total_bill_amount(15),
END OF t_sum_total_bill.
TYPES: BEGIN OF t_ap_summary,
a_num_inv_det(5),
total_sale_values TYPE t_sum_total_sale,
total_bill_values TYPE t_sum_total_bill,
END OF t_ap_summary.
*-------------------------------------------------->
TYPES: BEGIN OF t_ap,
head TYPE t_ap_head,
details TYPE t_ap_details,
summary TYPE t_ap_summary,
END OF t_ap.
DATA: l_ixml TYPE REF TO if_ixml,
l_streamfactory TYPE REF TO if_ixml_stream_factory,
l_parser TYPE REF TO if_ixml_parser,
l_istream TYPE REF TO if_ixml_istream,
l_document TYPE REF TO if_ixml_document,
l_node TYPE REF TO if_ixml_node,
l_xmldata TYPE string.
DATA: l_xml_table TYPE TABLE OF t_xml_line,
l_xml_line TYPE t_xml_line,
l_xml_table_size TYPE i.
DATA: l_filename TYPE string.
DATA: l_xml_x1 TYPE xstring.
*airplus tables
DATA: it_airplus TYPE STANDARD TABLE OF t_ap,
wa_airplus TYPE t_ap
.
*Errorvariables
DATA: xslt_err TYPE REF TO cx_xslt_exception,
err_string TYPE string
.
PARAMETERS: pa_file TYPE char1024 DEFAULT 'c:\HOTEL_TEST.XML'.
START-OF-SELECTION.
* Creating the main iXML factory
l_ixml = cl_ixml=>create( ).
* Creating a stream factory
l_streamfactory = l_ixml->create_stream_factory( ).
* Validate a document
IF pa_val EQ 'X'.
l_parser->set_validating( mode = if_ixml_parser=>co_validate ).
ENDIF.
* Process the document
IF l_parser->is_dom_generating( ) EQ 'X'.
* here we use the CALL TRANSFORMATION method which calls
* the XSLT program "ZFI_AIRPLUS_XSLT"
TRY.
CALL TRANSFORMATION ZFI_AIRPLUS_XSLT
SOURCE xml l_xml_x1
RESULT xml_output = it_airplus
.
* catch any error, very helpful if the XSLT isn't correct
CATCH cx_xslt_exception INTO xslt_err.
err_string = xslt_err->get_text( ).
WRITE: / 'Transformation error: ', err_string.
EXIT.
ENDTRY.* setting a breakpoint to watch the workarea
* by the internal table "it_airplus"
break-point.
LOOP AT it_airplus INTO wa_airplus.
ENDLOOP.
XSLT options
Here are the relevant XSLT elements in order of you could need it in your own program. For a good XSLT reference look at W3Schools.com - reference. In a XSLT program it is also possible to to write comments with the standard HTML commentfunction "<!- your comment here ->".
<xsl:value-of>
to catch the value from the element/attribute and move it to the ABAP variable
create a variable for access to the XML element. It is just a nice shortcut. Note that you can close this XSL element it self, look at the slash before the >
XML sample file
We take the sample file from Airplus. You can load the sample ZIP container by Airplus at: airplus.com -> products -> pay -> airplus electronic billing -> XML.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.