Skip to Content
author's profile photo Former Member
Former Member

Simple Transformation to output nil element & formatting

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?

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Jun 08, 2015 at 09: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>

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      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.

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.