In this weblog I show you a program that scans XSLT programs and Simple Transformations for certain commands. I present you a simple use case: In NW 7.02 Simple Transformations can call ABAP classes by the tt:call-method command. But how does it work? With this program you can scan existing transformations for examples. This is the input:
All XSLT and ST programs are scanned for an element tt:call-method – this is an element call-method in an XML namespace http://www.sap.com/transformation-templates .
The output is as follows:
In fact there are ST programs that contain the command like DEMO_ST_WITH_METHOD_CALL. There are also programs that have syntax errors like SEC_WSSP_POLICY2 which contain severe errors. IMHO SAP should use the program to ensure the quality of transformation in their software.
How does the program work? At first the transformations are selected and loaded using the class cl_o2_api_xsltdesc. Then the XSLT program zscan_transformation is used to scan the program – the result is a boolean flag. Then the result is given out resp. the message that an error occurs:
DATA lr_xslt TYPE REF TO cl_o2_api_xsltdesc.
DATA ls_trafo TYPE o2xslttext.
DATA lt_trafo TYPE STANDARD TABLE OF o2xslttext.
DATA lv_source TYPE string.
DATA lv_found TYPE abap_bool.
SELECT-OPTIONS s_trafo FOR ls_trafo-xsltdesc.
PARAMETERS p_name TYPE string LOWER CASE.
PARAMETERS p_namesp TYPE string LOWER CASE.
SELECT DISTINCT xsltdesc FROM o2xslttext
INTO TABLE lt_trafo WHERE xsltdesc IN s_trafo.
LOOP AT lt_trafo INTO ls_trafo.
CALL METHOD cl_o2_api_xsltdesc=>load
p_xslt_desc = ls_trafo-xsltdesc
p_obj = lr_xslt
error_occured = 1
not_existing = 2
permission_failure = 3
version_not_found = 4
OTHERS = 5.
IF sy-subrc = 0.
lv_source = lr_xslt->get_source_string( ).
CALL TRANSFORMATION zscan_transformation
PARAMETERS name = p_name
namespace = p_namesp
SOURCE XML lv_source
RESULT result = lv_found.
IF lv_found = abap_true.
WRITE :/ 'Found in', ls_trafo-xsltdesc.
WRITE :/ 'Error in ', ls_trafo-xsltdesc.
WRITE :/ 'Could not load: ', ls_trafo-xsltdesc.
How does the transformation ZSCAN_TRANSFORMATION work? It is an XSLT program that gets an XML document (in fact the code of an XSLT or ST program) as input together with two parameters: a name of an XML element and a namespace (in fact an URI).
The result is a boolean flag given back in asXML format – the typical way to bridge the gap between XML and ABAP. The result is X if and only if the number of elements of given name and namespace is greater than zero, i.e. there is at least one element. This is evaluated in an XPath expression in the following transformation:
test="count(//node()[namespace-uri()=$NAMESPACE and local-name()=$NAME]) > 0">
Why is this useful? You can find examples for ST commands (ST is a proprietary language) and SAP proprietary XSLT commands. A list of those commands (and of course an explanation together with details) can be found in my SAP Press book about XML in ABAP.
As an easy exercise I suggest you to write a program that scans for XPath functions which are within certain attributes of an XSLT program. You can also write programs that scan comments or other parts of the transformation which can be useful maintenance of transformations.