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

XML node

Former Member
0 Likes
1,323

Hi all,

I create an xml-file from ABAP data through a simple transformation.

I have read info about "Transformation Editor" in the SAP Library and the examples given in the help all use hardcoded XML-nodes like <XMLNODE>....</XMLNODE>. At the moment I also have all XML-nodes hard coded in my transformation.

My question: How do I get a value from the root (some id) into the name of the XML-node?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,162

hi, I'm not familiar with ST, but write a equivalent XSLT, like following:

[code]

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

<xsl:strip-space elements="*"/>

<xsl:template match="PARA">

<PARA>

<xsl:variable name="segment" select="VOLGNR/name()"/>

<xsl:variable name="tail">

<xsl:value-of select="VOLGNR"/>

</xsl:variable>

<xsl:element name="{concat($segment, '_', $tail)}">

<xsl:value-of select="VOLGNR"/>

</xsl:element>

<xsl:variable name="segment2" select="ARTNR/name()"/>

<xsl:variable name="tail2">

<xsl:value-of select="ARTNR"/>

</xsl:variable>

<xsl:element name="{concat($segment2, '_', $tail2)}">

<xsl:value-of select="ARTNR"/>

</xsl:element>

</PARA>

</xsl:template>

<xsl:template match="test1">

</xsl:template>

</xsl:transform>

[/code]

ABAP CODE :

[code]

TYPES: BEGIN OF ty_test,

volgnr TYPE I,

artnr TYPE I,

END OF ty_test.

DATA: xml_string TYPE string.

DATA ls_test TYPE ty_test.

ls_test-volgnr = 1.ls_test-artnr = 13.

CALL TRANSFORMATION ztest

SOURCE para = ls_test

RESULT XML xml_string.

IF sy-subrc <> 0.

WRITE sy-subrc.

ELSE.

WRITE xml_string.

ENDIF.

[/code]

You can have a try, I think the result is what you need.

hope it will be helpful

thanks

6 REPLIES 6
Read only

Former Member
0 Likes
1,162

hi, you can use 'name()' to get the name of XML node.

<xsl:variable name="segment" select="name()"/>

the above XSLT code define a var named 'segment', and set its value with the name of current node.

'$segment' , you can use this statement to reference the var you defined in XSLT.

hope it will be helpful

thanks

Read only

Former Member
0 Likes
1,162

Well actually I would like to do it the other way around, I would like to dynamicly name the node of the xml-file I am creating.

The name must be an id which comes from a record of the internal table I am converting.

for example here: http://www.xml.com/pub/a/2000/10/04/linking/ they put the isbn code to the node of an book entry in the xml file.

Message was edited by: Fred Sterk

Read only

Former Member
0 Likes
1,162

hi, the way I offer you can create a node with dynamic name.

assume you have already set the value to 'segment', you can do like this:

<xsl:element name="{$segment}"/>

segment is a var you defined, and you can set its value dynmamically.

<xsl:variable name="segment" select="name()"/>

is only a sample, you can use other value other than 'name()'.

if it is still not enough, please make me know the actually XML file and what do you want in details.

Give me a sample will be better.

thanks

Read only

Former Member
0 Likes
1,162

Ok here follows my simplified code:

Report code:

REPORT  z_simple_test.

TYPES:  BEGIN OF ty_test,
	   volgnr TYPE i,	
          artnr TYPE i,
	 END OF ty_test.

DATA:  xml_string TYPE string.

DATA  ls_test TYPE ty_test.

ls_test-volgnr = 1.
ls_test-artnr = 13.

CALL TRANSFORMATION ztest
  SOURCE     para = ls_test
  RESULT XML xml_string.

IF sy-subrc <> 0.
  WRITE sy-subrc.
ELSE.
  WRITE xml_string.
ENDIF.

The ztest transformation code:

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

  <tt:root name="PARA"/>

  <tt:template name="MainTempl">
    <X tt:ref="PARA">
      <VolgnrNode>        
        <tt:value ref="volgnr"/>
      </VolgnrNode>
      <ArtNode>
        <tt:value ref="artnr1"/>
      </ArtNode>
    </X>

  </tt:template>

</tt:transform>

This results in the following string:

<?xml version="1.0" encoding="utf-16"?>#<X><VolgnrNode>1</VolgnrNode><ArtNode>13</ArtNode></X>

Now I would like to see on the spot <VolgnrNode> something like <VolgnrNode_1> so a concatenation of the VolgNrNode and the value of the Volgnr.

Read only

Former Member
0 Likes
1,163

hi, I'm not familiar with ST, but write a equivalent XSLT, like following:

[code]

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

<xsl:strip-space elements="*"/>

<xsl:template match="PARA">

<PARA>

<xsl:variable name="segment" select="VOLGNR/name()"/>

<xsl:variable name="tail">

<xsl:value-of select="VOLGNR"/>

</xsl:variable>

<xsl:element name="{concat($segment, '_', $tail)}">

<xsl:value-of select="VOLGNR"/>

</xsl:element>

<xsl:variable name="segment2" select="ARTNR/name()"/>

<xsl:variable name="tail2">

<xsl:value-of select="ARTNR"/>

</xsl:variable>

<xsl:element name="{concat($segment2, '_', $tail2)}">

<xsl:value-of select="ARTNR"/>

</xsl:element>

</PARA>

</xsl:template>

<xsl:template match="test1">

</xsl:template>

</xsl:transform>

[/code]

ABAP CODE :

[code]

TYPES: BEGIN OF ty_test,

volgnr TYPE I,

artnr TYPE I,

END OF ty_test.

DATA: xml_string TYPE string.

DATA ls_test TYPE ty_test.

ls_test-volgnr = 1.ls_test-artnr = 13.

CALL TRANSFORMATION ztest

SOURCE para = ls_test

RESULT XML xml_string.

IF sy-subrc <> 0.

WRITE sy-subrc.

ELSE.

WRITE xml_string.

ENDIF.

[/code]

You can have a try, I think the result is what you need.

hope it will be helpful

thanks

Read only

Former Member
0 Likes
1,162

Zhenglin Gu thank you very much for your answer, this is exactly what I wanted.