Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
ttrapp
Active Contributor
0 Kudos

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:

REPORT  zscan_transformation.

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.

START-OF-SELECTION.

  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
      EXPORTING
        p_xslt_desc        = ls_trafo-xsltdesc
      IMPORTING
        p_obj              = lr_xslt
      EXCEPTIONS
        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( ).
      TRY.
          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.
          ENDIF.
        CATCH cx_xslt_runtime_error.
          WRITE 😕 'Error in ', ls_trafo-xsltdesc.
      ENDTRY.
    ELSE.
      WRITE 😕 'Could not load: ', ls_trafo-xsltdesc.
    ENDIF.

  ENDLOOP.

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:

<xsl:transform xmlns:xsl=http://www.w3.org/1999/XSL/Transform 
  xmlns:asx="http://www.sap.com/abapxml" version="1.0">
  <xsl:param name="NAME"/>
  <xsl:param name="NAMESPACE"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <asx:abap>
      <asx:values>
        <RESULT>
          <xsl:if
test="count(//node()[namespace-uri()=$NAMESPACE and local-name()=$NAME]) > 0">
            <xsl:text>X</xsl:text>
          </xsl:if>
        </RESULT>
      </asx:values>
    </asx:abap>
  </xsl:template>

</xsl:transform>

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.

3 Comments