Blogs

Tobias Trapp

How to Scan ST & XSLT Programs for Specific Commands
Tobias Trapp Active Contributor Platinum: 2,500+ points SAP Mentor
Business Card
Company: AOK Systems GmbH
Posted on Jan. 13, 2012 03:36 PM in ABAP

Subscribe.Subscribe
Print. Print
Permalink Permalink
Share

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.

Tobias Trapp  Active Contributor Platinum: 2,500+ points SAP Mentor is SAP Mentor and Software Architect for AOK Systems GmbH. He can be found as @ttrapp on Twitter.


Comment on this article
Comment on this weblog
Showing messages 1 through 4 of 4.

Titles Only Main Topics Oldest First

  • Great blog!
    2012-01-13 22:08:23 Peter Langner Business Card [Reply]

    Hi Tobias,


    Good idea to write such a program. I think that is very use full.


    I also read your book about ABAP and XML. It is really a treasure chest regarding its subject. It's a pity, that there is no English translation...


    Peter

    • Great blog!
      2012-01-15 11:49:27 Tobias Trapp Business Card [Reply]

      Hi Peter,


      I'm glad that you liked this book and my blog.


      What do you think of the XML infrastructure of AS ABAP? What is missing? What could be improved? Or do you think it is OK like it is? What about tooling and supported standards?


      Cheers,
      Tobias

      • Great blog!
        2012-01-23 19:22:14 James Wood Business Card [Reply]

        I agree with Peter's sentiments; a very useful tool. As far as what could be improved, I'd personally like to see a more generic XML data binding framework like JAXB introduced. This works very well for proxies and the like, but a more generic toolset would prove useful in its own right I think.


        It would also be nice to see the XSLT runtime enhanced to support XSLT 2.0.


        Finally, if I'm getting really stingy, I'd love to see an XQuery engine brought into the fold. :-)

        • Great blog!
          2012-01-25 03:08:26 Tobias Trapp Business Card [Reply]

          In fact the XSLT processor in ABAP has some XSLT 2.0 features - in fact I blogged about it.


          JAXB would be cool - perhaps as a community project?


          Do you have use cases for XQuery? IMHO it would be very useful if HANA gets used for semistructured data like XML or RDF - think of electronic records for example in healthcare. Do you have business cases in mind?


          Today it is strong for structured data has lots of potential in unstructrued data because of TREX-components.


          Best Regards,
          Tobias


Showing messages 1 through 4 of 4.