06-08-2015 7:09 AM
Hi
I have a business requirement to format my XML output in the following
Expected XML |
---|
<?xml version="1.0"?> <PO xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <HEADER> <EXCEPTIONS xsi:nil="true"/> </HEADER> <ITEMS> <ITEM> <LINE_NUMBER>00001</LINE_NUMBER> ...... <EXCEPTIONS xsi:nil="true"/> </ITEM> </ITEMS> </PO> |
However using my current output is as below:
Current XML |
---|
<?xml version="1.0" encoding="utf-16"?> <PO><HEADER><EXCEPTIONS/></HEADER><ITEMS><ITEM><EXCEPTIONS/></ITEM></ITEMS></PO> |
The problem with the current XML is that
1. there is no nil tag (<EXCEPTIONS xsi:nil="true"/>) .
2. The formatted XML is not formatted: all output appears as 1 line.
Below is my Simple Transformation:
Simple Transformation |
---|
<?sap.transform simple?> <tt:transform xmlns:tt="http://www.sap.com/transformation-templates" xmlns:ddic="http://www.sap.com/abapxml/types/dictionary" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:def="http://www.sap.com/abapxml/types/defined" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <tt:root name="PO" type="ddic:<Z_PO>"/> <tt:template> <PO> <HEADER> <EXCEPTIONS> <tt:loop ref=".PO.HEADER.EXCEPTIONS"> <EXCEPTION> <EXCEPTION_CODE tt:value-ref="EXCEPTION_CODE"/> <EXCEPTION_LONG_DESC tt:value-ref="EXCEPTION_LONG_DESC"/> </EXCEPTION> </tt:loop> </EXCEPTIONS> </HEADER> <ITEMS> <tt:loop ref=".PO.ITEMS"> <ITEM> <LINE_NUMBER tt:value-ref="LINE_NUMBER"/> <EXCEPTIONS> <tt:loop ref="EXCEPTIONS"> <EXCEPTION xsi:nil="true"> <EXCEPTION_CODE xsi:nil="true" tt:value-ref="EXCEPTION_CODE"/> <EXCEPTION_LONG_DESC xsi:nil="true" tt:value-ref="EXCEPTION_LONG_DESC"/> </EXCEPTION> </tt:loop> </EXCEPTIONS> </ITEM> </tt:loop> </ITEMS> </PO> </tt:template> </tt:transform> |
And in my program, this is the way I called:
Header 1 |
---|
CALL TRANSFORMATION z_po OPTIONS xml_header = 'full' SOURCE po = ls_po RESULT XML lv_ack_output_str. |
Is there anything which I need to get the desired output?
06-08-2015 10:53 AM
Hi Francis,
You can put an if condition to check if the exception table is initial. or may be simply try this.
<HEADER>
<tt:s-cond check="not-initial(PO.HEADER.EXCEPTIONS)">
<EXCEPTIONS>
<tt:loop ref=".PO.HEADER.EXCEPTIONS">
<EXCEPTION>
<EXCEPTION_CODE tt:value-ref="EXCEPTION_CODE"/>
<EXCEPTION_LONG_DESC tt:value-ref="EXCEPTION_LONG_DESC"/>
</EXCEPTION>
</tt:loop>
</EXCEPTIONS>
</tt:s-cond>
<tt:s-cond check="initial(PO.HEADER.EXCEPTIONS)">
<EXCEPTIONS xsi:nil="true"/>
</tt:s-cond>
</HEADER>
06-08-2015 10:53 AM
Hi Francis,
You can put an if condition to check if the exception table is initial. or may be simply try this.
<HEADER>
<tt:s-cond check="not-initial(PO.HEADER.EXCEPTIONS)">
<EXCEPTIONS>
<tt:loop ref=".PO.HEADER.EXCEPTIONS">
<EXCEPTION>
<EXCEPTION_CODE tt:value-ref="EXCEPTION_CODE"/>
<EXCEPTION_LONG_DESC tt:value-ref="EXCEPTION_LONG_DESC"/>
</EXCEPTION>
</tt:loop>
</EXCEPTIONS>
</tt:s-cond>
<tt:s-cond check="initial(PO.HEADER.EXCEPTIONS)">
<EXCEPTIONS xsi:nil="true"/>
</tt:s-cond>
</HEADER>
06-12-2015 3:50 AM
Hi Rudra
Thanks for the response.
As all the element tags in the transformation are optional, is there a way to do global declaration such that each of the tag will have the <...... xsi:nil="true"/> content inside?
06-12-2015 9:40 AM
Hi Francis,
I am sorry, I am not aware of any quick way. I am a technical guy and I always use conditions and it has served me quite well so far. As long as it works without sacrificing the performance and readability I don't bother that much.
You can search over the internet for a quick way if you can find anything. I will be also happy to know if there is any.
R
09-21-2015 7:26 AM
Hi Rudra
I have since adopted the solution since that there is actually no easy way to have a one time declaration to inject the element tag <...... xsi:nil="true"/> into the Simple Transformation. I have found out that, this is not possible way to do in Simple Transformation but there is a way to do it in XSLT Transformation only.