Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Need help with xml mapping

Former Member
0 Likes
2,284

HI All,

i have sap internal table with this value and i want to mapp it to xml file like below,

Name	        value
User	                1234
DueDate 	              yyyyy

<User>1234</User>
<DueDate>yyyyy</DueDate>

the problem is when i use call transformation i get in the table

<name>1234</name>
<value>yyyyy</value>

how can i get the mapp that i need ?

Regards

JOy

Edited by: Joy Stpr on Aug 10, 2009 2:56 PM

Edited by: Joy Stpr on Aug 10, 2009 3:47 PM

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,158

Did your problem solved?if not check threads in PI-Section.

25 REPLIES 25
Read only

former_member194669
Active Contributor
0 Likes
2,158

Try this way


<xsl:transform version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <xsl:copy>
    <xsl:apply-templates></xsl:apply-templates>
  </xsl:copy>
</xsl:template>

<xsl:template match="xmldoc">
  <item>
    <USER><xsl:value-of select="user"></xsl:value-of></USER>
    <DUEDATE><xsl:value-of select="duedate"></xsl:value-of></DUEDATE>
  </item>
</xsl:template>

</xsl:transform>

a®

Read only

0 Likes
2,158

Hi a®s ,

i try to put the table on the transformation like this :

and i have error :

In "CALL TRANSFORMATION ... SOURCE XML xml", the source xml must be

compatible with one of these types: STRING, XSTRING, TABLE (with flat

row type), REF IF_IXML_NODE, CL_FX_READER

but it_data is table what i miss here

DATA: BEGIN OF ty_data,
      name TYPE string,
      value TYPE string,
END OF ty_data.

DATA: it_data LIKE  TABLE OF  ty_data,
      wa_data like LINE OF it_data.

data result TYPE string.
wa_data-name = 'user'.
wa_data-value = 'XXXXX' .

APPEND wa_data TO it_data.

wa_data-name = 'dudate'.
wa_data-value = 'yyyy' .

APPEND wa_data TO it_data.

CHECK sy-subrc = 0.



  CALL TRANSFORMATION zxml_to_abap2
  SOURCE XML it_data
  RESULT xmldoc = result.

Best Regards

Joy

Read only

0 Likes
2,158

Sorry I misunderstood your question. ( Previously i think it was from XML to internal table )

Here is the modified code for Internal table to XML

Here you don't need a XLST conversion program


report zars
  no standard page heading line-size 255.
data: xml_out type string .
data: begin of ty_data,
      name type string,
      value type string,
end of ty_data.

data: it_data like  table of  ty_data,
      wa_data like line of it_data.

data result type string.
wa_data-name = 'user'.
wa_data-value = 'XXXXX' .

append wa_data to it_data.

wa_data-name = 'dudate'.
wa_data-value = 'yyyy' .

append wa_data to it_data.

check sy-subrc = 0.
call transformation ('ID')
source tab = it_data[]
result xml xml_out.
break-point.

a®

Read only

0 Likes
2,158

Hi a®s ,

Thanks !!

but what i get is for simple transformaion ,

- <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
- <asx:values>
- <TAB>
- <item>
  <NAME>user</NAME> 
  <VALUE>XXXXX</VALUE> 
  </item>
- <item>
  <NAME>dudate</NAME> 
  <VALUE>yyyy</VALUE> 
  </item>
  </TAB>
  </asx:values>
  </asx:abap>

and i want :

- <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
- <asx:values>
- <TAB>
- <item>
  <user>XXXXX</user> 
  </item>
- <item>
  <dudate>yyyy</dudate> 
  </item>
  </TAB>
  </asx:values>
  </asx:abap>

how can i reach that?

Thanks for your time !!!!!

Best regards

Joy

Read only

0 Likes
2,158

You may need to better look into demo program SXSLTDEMO_FLIGHTS

a®

Read only

0 Likes
2,158

HI ,

i check this program and i don't find anything that look like what i want,

can u give me direction please?

This is something that i can do via transformation ?

Regards

Joy

Edited by: Joy Stpr on Aug 10, 2009 9:16 PM

Read only

Former Member
0 Likes
2,159

Did your problem solved?if not check threads in PI-Section.

Read only

0 Likes
2,158

Check this

Program Code


report zars
  no standard page heading line-size 255.
data: xml_out type string .
data: begin of ty_data,
      name type string,
      value type string,
end of ty_data.

types: begin of ty_user,
       user type sy-uname,
       duedate type sy-datum,
       end   of ty_user.

data: itab type standard table of ty_user,
      la_tab like line of itab,
      xmlstr type xstring.

la_tab-user = 'TEST1'.
la_tab-duedate = '200908101'.
append la_tab to itab.
clear la_tab.

la_tab-user = 'TEST2'.
la_tab-duedate = '200909101'.
append la_tab to itab.
clear la_tab.

data : itab1 type  swbhtmltable.

data: it_data like  table of  ty_data,
      wa_data like line of it_data.

data result type string.

call transformation zaRs
source table = itab
result xml xml_out.

call function 'SWA_STRING_TO_TABLE'
  exporting
    character_string = xml_out
  importing
    character_table  = itab1.

call function 'GUI_DOWNLOAD'
  exporting
    filetype = 'BIN'
    filename = 'c:xx.xml'
  tables
    data_tab = itab1.

XLST Program


<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates">

  <tt:root name="table"></tt:root>

  <tt:template>
    <table>
      <tt:loop ref=".table">
        <item>
          <USER>
            <tt:value ref="USER"></tt:value>
          </USER>
        </item>
        <item>
          <DUEDATE>
            <tt:value ref="DUEDATE"></tt:value>
          </DUEDATE>
        </item>
      </tt:loop>
    </table>

  </tt:template>

</tt:transform>

Result XML File


<?xml version="1.0" encoding="iso-8859-1" ?> 
- <table>
- <item>
  <USER>TEST1</USER> 
  </item>
- <item>
  <DUEDATE>2009-08-10</DUEDATE> 
  </item>
- <item>
  <USER>TEST2</USER> 
  </item>
- <item>
  <DUEDATE>2009-09-10</DUEDATE> 
  </item>
  </table>

a®

Read only

0 Likes
2,158

HI

Thanks it's work like u write ,but my requirement is a little bit differnt

the problem is that the table i get is in type (name ,value ) like it_data.

data: begin of ty_data,
      name type string,
      value type string,
end of ty_data.
 
data: it_data like  table of  ty_data,
      wa_data like line of it_data.
 
data result type string.
wa_data-name = 'user'.
wa_data-value = 'XXXXX' .

append wa_data to it_data.

In your code u do refer to it_data .

I need that name like user be in the tag and the value be inside the tag like:

<user>XXXXX</user>

I think that this is a little bit tricky and for that I opened this message .

Do u think that I can do that ?

Maybe i need to do manipulation on the table before the transformation ?

Best Regards

Joy

Edited by: Joy Stpr on Aug 11, 2009 10:12 AM

Edited by: Joy Stpr on Aug 11, 2009 10:51 AM

Read only

0 Likes
2,158

Hello Joy,

In this case you can use first column of you table to fill fieldcatalog table and create dynamic internal table.

Then using "assign component...." you can fill that dynamic table with the data from second column from the first internal table.

Finally pass the dynamic internal table to transformation. You can use identical transformation so you don't need to generate transformation template for same.

Hope this helps!

Thanks,

Augustin.

Read only

0 Likes
2,158

Try this XSLT transformation: It may helpful to you:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:sap="http://www.sap.com/sapxsl" version="1.0">
  <xsl:output indent="no" method="xml" omit-xml-declaration="yes|no"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="ITAB">
    <xsl:for-each select="*">
      <xsl:variable name="user_tag">
        <xsl:value-of select="NAME"/>
      </xsl:variable>
      <xsl:element name="{$user_tag}">
        <xsl:value-of select="VALUE"/>
      </xsl:element>
    </xsl:for-each>
  </xsl:template>

</xsl:transform>

and invoke this XSLT transformation like;

call transformation <transformation name>
  source
  ITAB = it_data
  result xml lv_xml_string.

and use API's to convert lv_xml_string variable to string. This variable lv_xml_string is in type of XSTRING data type.

Regards,

Siva.

Read only

0 Likes
2,158

Hi Siva,

i try exactly like u tell and i got this string <?xml version="1.0" encoding="utf-16"?>#userXXXXXdudateyyyy

in xml_out .

DATA: BEGIN OF ty_data,
      name TYPE string,
      value TYPE string,
END OF ty_data.

DATA: itab LIKE  TABLE OF  ty_data,
      wa_data LIKE LINE OF itab.

DATA result TYPE string.
wa_data-name = 'user'.
wa_data-value = 'XXXXX' .

APPEND wa_data TO itab.

wa_data-name = 'dudate'.
wa_data-value = 'yyyy' .

APPEND wa_data TO itab.

*CHECK sy-subrc = 0.
DATA: xml_out TYPE string .
CALL TRANSFORMATION zxml_to_abap2
SOURCE tab = itab[]
RESULT XML xml_out.

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:sap="http://www.sap.com/sapxsl" version="1.0">
  <xsl:output indent="no" method="xml" omit-xml-declaration="yes|no"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>
 
  <xsl:template match="ITAB">
    <xsl:for-each select="*">
      <xsl:variable name="user_tag">
        <xsl:value-of select="NAME"/>
      </xsl:variable>
      <xsl:element name="{$user_tag}">
        <xsl:value-of select="VALUE"/>
      </xsl:element>
    </xsl:for-each>
  </xsl:template>
 
</xsl:transform>

what i miss here?

Regards

Joy

Read only

0 Likes
2,158

Invocation part is wrong:

CALL TRANSFORMATION zxml_to_abap2
SOURCE tab = itab[]
RESULT XML xml_out.

Change this to;

CALL TRANSFORMATION zxml_to_abap2
SOURCE ITAB = itab[]
RESULT XML xml_out.

Difference is SOURCE parameter name should be ITAB because this is the one which is used in transformation.

Regards,

Siva.

Edited by: Siva Satya Prasad Yerra on Aug 11, 2009 6:00 PM

Read only

0 Likes
2,158

HI Siva,

Thanks it is working great !

one last thing ,when i get the string i get # :

uFEFF<?xml version="1.0" encoding="utf-16"?>#<user>XXXXX</user><dudate>yyyy</dudate>

Why and there is a way to omit it ?

Best Regards

Joy

Read only

0 Likes
2,158

If you don't want <?xml version="1.0" encoding="utf-16"?> in output XML then set following attribute in transformation:

omit-xml-declaration="yes"

With this your output will be

uFEFF<user>XXXXX</user><dudate>yyyy</dudate>

Regards,

Siva.

Read only

0 Likes
2,158

This # is a new line character which is coming because of omit-xml-declaration="no" in XML transformation. I'm not sure whether we can remove this. I tried with this:

<xsl:output indent="no" method="xml" omit-xml-declaration="no"/>

Still it displays # charcter. Actually this will come when you use

indent="yes"

in above tag.

Regards,

Siva.

Read only

0 Likes
2,158

HI Siva,

i need <?xml version="1.0" encoding="utf-16"?>

i get :

uFEFF<?xml version="1.0" encoding="utf-16"?>#<user>XXXXX</user><dudate>yyyy</dudate>

i want

<?xml version="1.0" encoding="utf-16"?><user>XXXXX</user><dudate>yyyy</dudate>

i just want to omit # that come after: utf-16"?> .

character which symbolizes

By the way there is a way to debug it ?

when i press F5 i the debugger skip the transformation

Regards

Joy

Edited by: Joy Stpr on Aug 11, 2009 5:30 PM

Read only

0 Likes
2,158

Try to change you XLST this way and check

Try to remove the indent options completely from XLST and try


<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:sap="http://www.sap.com/sapxsl" version="1.0">
  <xsl:output encoding="UTF-16" standalone="yes"></xsl:output>    " <<<<<<  
  <xsl:template match="/">
    <xsl:apply-templates></xsl:apply-templates>
  </xsl:template>
 
  <xsl:template match="ITAB">
    <xsl:for-each select="*">
      <xsl:variable name="user_tag">
        <xsl:value-of select="NAME"></xsl:value-of>
      </xsl:variable>
      <xsl:element name="{$user_tag}">
        <xsl:value-of select="VALUE"></xsl:value-of>
      </xsl:element>
    </xsl:for-each>
  </xsl:template>
 
</xsl:transform>

a®

Read only

0 Likes
2,158

HI,

Can u please explain what is the problem ?

Thanks

Edited by: Chris Teb on Aug 11, 2009 9:40 PM

Read only

0 Likes
2,158

HI a®s ,

Before i get :

uFEFF<?xml version="1.0" encoding="utf-16"?>#<user>XXXXX</user><dudate>yyyy</dudate>

and now i get :

The # is remain ,any other idea please ?

Thanks & Best Regards

Joy

Read only

0 Likes
2,158

Save the xml into your desktop and try to open it thru IE (Internet explorer ) and check

1. is only a indent

a®

Read only

0 Likes
2,158

Hello,

This is non-printable char comes after transformaing data in some of the SAP versions.

The only way is to locate that char using some offset and replace with space.

for example if 10 position of xml_string is having '#' then

xml_string+10(1) = ''. will remove that character.

Check if this helps,

Thanks,

Augustin.

Read only

0 Likes
2,158

HI Siva

the # is not an issue thanks forget about it.

i have problem with the ending of the file ,

it is not close well ,instead of tree like this :

u can see it on the debuger -> view xml browser or in xml spy software .

this is the solution for not dynamics field and value and your solution is for dynamic xslt

<?xml version="1.0" encoding="utf-16" ?> 
- <table>
- <item>
  <USER>TEST1</USER> 
  </item>
- <item>
  <DUEDATE>2009-08-10</DUEDATE> 
  </item>
- <item>
  <USER>TEST2</USER> 
  </item>
- <item>
  <DUEDATE>2009-09-10</DUEDATE> 
  </item>
  </table>

this is what i get :

<?xml version="1.0" encoding="utf-16"?>#<user>XXXXX</user><dudate>yyyy</dudate>

and the tree is not close well .

for the XSLT i use your code since i need dynamic transform .

<xsl:template match="ITAB">
    <xsl:for-each select="*">
      <xsl:variable name="user_tag">
        <xsl:value-of select="NAME"/>
      </xsl:variable>
      <xsl:element name="{$user_tag}">
        <xsl:value-of select="VALUE"/>
      </xsl:element>
    </xsl:for-each>
  </xsl:template>

Any idea .

Regards

Best Joy

Edited by: Joy Stpr on Aug 12, 2009 10:07 AM

Read only

0 Likes
2,158

With this XSLT we are getting more than one root node <user> and <dudate>. Actuall XML should have only one root element.

Only one top level element is allowed in an XML document.

So, change the XSLT like this to get one root node.

<xsl:template match="ITAB">
  <xsl:element name="Provide the Root Node Nam"> " This can be anything which suits your requirement.
    <xsl:for-each select="*">
      <xsl:variable name="user_tag">
        <xsl:value-of select="NAME"/>
      </xsl:variable>
      <xsl:element name="{$user_tag}">
        <xsl:value-of select="VALUE"/>
      </xsl:element>
    </xsl:for-each>
     </xsl:element>
  </xsl:template>

Regards,

Siva.

Read only

0 Likes
2,158

HI Siva ,

Thank you very much !!!!

You are an expert .

Best Regards

Joy