Advertisement
rplantiko

Dynamical XSLT in ABAP

Oct 4th, 2011
1,357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ABAP 4.25 KB | None | 0 0
  1. * --- Test usage of a dynamically given non-workbench XSLT program
  2.  
  3. * Class CL_XSLT_PROCESSOR is able to receive its XSLT code at runtime
  4. * So an XSLT doesn't necessarily to be created in the repository
  5. * The transformation gets a symbolic name and is available under this name on this server
  6. * This is only a buffering, however.
  7. * Just to go sure, the source code should always be provided when the XSLT is to be executed.
  8.  
  9. report  zz_test_cl_xslt_processor.
  10.  
  11. data:
  12. * iXML master
  13.   go_xml type ref to if_ixml,
  14. * iXML stream factory
  15.   go_sf  type ref to if_ixml_stream_factory.
  16.  
  17. load-of-program.
  18.   go_xml = cl_ixml=>create( ).
  19.   go_sf  = go_xml->create_stream_factory( ).
  20.  
  21. start-of-selection.
  22.   perform start.
  23.  
  24. * --- Start
  25. form start.
  26.  
  27.   data: lo_source    type ref to if_ixml_document,
  28.         lo_result    type ref to if_ixml_document,
  29.         lo_processor type ref to cl_xslt_processor,
  30.         lv_p         type progname,
  31.         lo_ex        type ref to cx_xslt_exception.
  32.  
  33.   perform get_source changing lo_source.
  34.  
  35.   create object lo_processor.
  36.  
  37.   try.
  38.  
  39. * Set source
  40.       lo_processor->set_source_node( lo_source ).
  41. * Set result
  42.       lo_result = go_xml->create_document( ).
  43.       lo_processor->set_result_document( lo_result ).
  44.  
  45. * This could be time-critical, the creation of a dynamical XSLT prog?
  46.       perform set_transformation using lo_processor
  47.                                  changing lv_p.
  48.  
  49. * call xslt-proc
  50.       lo_processor->run( lv_p ).
  51.  
  52. * Display result
  53.       call function 'SDIXML_DOM_TO_SCREEN'
  54.         exporting
  55.           document    = lo_result
  56.           title       = 'Result of Transformation'
  57.         exceptions
  58.           no_document = 1
  59.           others      = 2.
  60.  
  61.  
  62.     catch cx_xslt_exception into lo_ex.
  63.       message lo_ex type 'I'.
  64.   endtry.
  65.  
  66. endform.                    "start
  67.  
  68.  
  69. * --- Set XSLT transformation from stream
  70. form set_transformation using io_processor type ref to cl_xslt_processor
  71.                         changing cv_p type progname.
  72.  
  73.   data: lo_trans     type ref to if_ixml_istream.
  74.  
  75. * sv_p contains temp. name of XSLT program after first call
  76.   statics: sv_p   type string.
  77.  
  78.   if sv_p is initial.
  79. * It seems that the name can be buffered on appserver level?
  80.     import progname to sv_p
  81.            from shared buffer indx(zx) id 'ZZ_TEST_XSLT_PROC'.
  82.     if sv_p is initial.
  83.       sv_p = 'X'.
  84.     endif.
  85.   endif.
  86.  
  87. * Provide the stream containing the XSLT document (as a stream)
  88.   perform get_transformation changing lo_trans.
  89.  
  90. * Set transformation
  91.   io_processor->set_source_stream( exporting stream = lo_trans
  92.                                    changing  p      = sv_p ).
  93.  
  94. * Buffer progname on server - seems to work
  95.   export progname from sv_p
  96.          to shared buffer indx(zx) id 'ZZ_TEST_XSLT_PROC'.
  97.  
  98. * string -> c move necessary, since xslt-proc-interface doesn't use
  99. * the generic type csequence for program name
  100.   cv_p = sv_p.
  101.  
  102. endform.                    "set_transformation
  103.  
  104.  
  105. * --- Parse a source given as string into an if_ixml_document
  106. form get_source changing co_src type ref to if_ixml_document.
  107.  
  108.   data: lv_s      type string,
  109.         lo_stream type ref to if_ixml_istream,
  110.         lo_parser type ref to if_ixml_parser.
  111.  
  112.   concatenate
  113. `<?xml version="1.0" encoding="iso-8859-1" ?>`
  114. `<root>2</root>`
  115.  into lv_s.
  116.  
  117. * Create and parse the input document as if_ixml_document
  118.   lo_stream   = go_sf->create_istream_string( lv_s ).
  119.   co_src      = go_xml->create_document( ).
  120.   lo_parser   = go_xml->create_parser( document       = co_src
  121.                                        istream        = lo_stream
  122.                                        stream_factory = go_sf ).
  123.   lo_parser->parse( ).
  124.  
  125.  
  126. endform.                    "get_source
  127.  
  128. * --- Put the transformation given as string into an if_ixml_istrean
  129. form get_transformation changing co_trans type ref to if_ixml_istream.
  130.  
  131.   data: lv_s   type string.
  132.  
  133.   concatenate
  134. `<xsl:transform version="1.0"`
  135. `  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">`
  136. `  <xsl:template match="root">`
  137. `    <GotNumber><xsl:value-of select="."/></GotNumber>`
  138. `  </xsl:template>`
  139. `</xsl:transform>`
  140.   into lv_s.
  141.  
  142.   co_trans = go_sf->create_istream_string( lv_s ).
  143.  
  144.  
  145. endform.                    "get_transformation
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement