2012 Jul 17 8:19 PM
Hi,
How to transform complex ABAP internal tables to recursive nodes in XML using XSLT?
Eg: ABAP data in internal table is like:
| LEVEL | COMPONENT |
| 0 | P1 |
| 1 | C1 |
| 2 | C11 |
| 3 | C111 |
| 2 | C12 |
| 1 | C2 |
| 2 | C21 |
| 2 | C22 |
| 3 | C221 |
| 3 | C222 |
To be transformed to XML. Note - The number of levels is not fixed. There can be multiple children at different levels. A child at level 1 can be a parent for another child at level 2 and so on.
XML to look like
<P1>
<C1>
<C11>
<C111>
<C12>
<C2>
<C21>
<C22>
<C221>
<C222>
Any pointers are apprciated!
Regards
Deepthi
Hi,
How to transform complex ABAP internal tables to recursive nodes in XML using XSLT?
Eg: ABAP data in internal table is like:
| LEVEL | COMPONENT |
| 0 | P1 |
| 1 | C1 |
| 2 | C11 |
| 3 | C111 |
| 2 | C12 |
| 1 | C2 |
| 2 | C21 |
| 2 | C22 |
| 3 | C221 |
| 3 | C222 |
To be transformed to XML. Note - The number of levels is not fixed. There can be multiple children at different levels. A child at level 1 can be a parent for another child at level 2 and so on.
XML to look like
<P1>
<C1>
<C11>
<C111>
<C12>
<C2>
<C21>
<C22>
<C221>
<C222>
Any pointers are apprciated!
Regards
Deepthi
2012 Jul 18 5:19 AM
Check this link
http://wiki.sdn.sap.com/wiki/display/ABAP/XML+XSLT+with+ABAP
2012 Jul 19 10:03 PM
The following transformation does the job.
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<ROOT>
<xsl:apply-templates select="//item[LEVEL = 0]"/>
</ROOT>
</xsl:template>
<xsl:template match="item">
<xsl:variable name="level" select="LEVEL"/>
<xsl:variable name="line_id" select="generate-id()"/>
<xsl:variable name="children">
<xsl:apply-templates select="following-sibling::item[LEVEL = $level+1][preceding-sibling::item[LEVEL = $level][1]/generate-id() = $line_id]"/>
</xsl:variable>
<xsl:element name="{NAME}">
<xsl:if test="$children/*">
<xsl:copy-of select="$children"/>
</xsl:if>
</xsl:element>
</xsl:template>
</xsl:transform>
The test program below gives the expected output:
<?xml version="1.0" encoding="utf-8"?>
<ROOT>
<P1>
<C1>
<C11>
<C111/>
<C112/>
</C11>
<C12/>
</C1>
<C2>
<C21/>
<C22>
<C221/>
<C222/>
</C22>
</C2>
</P1>
</ROOT>
*&---------------------------------------------------------------------*
*& Report Z_TEST_LEVEL_STRUC
*&---------------------------------------------------------------------*
report z_test_level_struc.
types: begin of ty_node,
level type i,
name type string,
end of ty_node,
ty_nodes type standard table of ty_node
with non-unique default key.
start-of-selection.
perform start.
* ---
form start.
data: lt_nodes type ty_nodes,
lo_doc type ref to if_ixml_document.
perform fill_nodes changing lt_nodes.
lo_doc ?= cl_ixml=>create( )->create_document( ).
call transformation z_test_level_struc
source levels = lt_nodes
result xml lo_doc.
call function 'SDIXML_DOM_TO_SCREEN'
exporting
document = lo_doc
exceptions
no_document = 1
others = 2.
if sy-subrc ne 0.
message id sy-msgid type 'I' number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
endform. "start
* ---
form fill_nodes changing et_nodes type ty_nodes.
data: ls_node type ty_node.
define _add_node.
ls_node-level = &1.
ls_node-name = &2.
append ls_node to et_nodes.
end-of-definition.
clear et_nodes.
_add_node :
0 'P1' ,
1 'C1' ,
2 'C11' ,
3 'C111' ,
3 'C112' ,
2 'C12' ,
1 'C2' ,
2 'C21' ,
2 'C22' ,
3 'C221' ,
3 'C222'.
endform. "fill_nodes
2012 Jul 20 7:13 AM
... or, slightly simplified, but functionally identical:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<ROOT>
<xsl:apply-templates select="//LEVELS/*[LEVEL = 0]"/>
</ROOT>
</xsl:template>
<xsl:template match="LEVELS/*">
<xsl:variable name="level" select="LEVEL"/>
<xsl:variable name="id" select="generate-id()"/>
<xsl:element name="{NAME}">
<xsl:apply-templates
select="following-sibling::*[LEVEL = $level+1][preceding-sibling::*[LEVEL = $level][1]/generate-id() = $id]"/>
</xsl:element>
</xsl:template>
</xsl:transform>
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |