cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Converting XML elements into Values in SAP CI

barath_dv
Participant
0 Likes
886

Experts,

Source Data

<root>
  <test>
     <product>
        <box>square</box>
        <qty>5</qty>
     </product>
  </test>
</root>

Expected Data

<root>
  <Level1>test<Level1>
  <Level2>Product<Level2>
  <Level3>square</Level3>
  <Level4>5</Level4>
</root>

Can you please advice me on how to achieve the above requirement?

TIA,

Barath

Accepted Solutions (0)

Answers (2)

Answers (2)

karthikarjun
Active Contributor

Hi, I can recommend you to use the XSLT to achieve your output

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>


  <xsl:template match="/">
    <root>
      <xsl:apply-templates select="root/test/product"/>
    </root>
  </xsl:template>


  <xsl:template match="product">
    <Level1>
      <xsl:value-of select="ancestor::test[1]/name()"/>
    </Level1>
    <Level2>
      <xsl:value-of select="name()"/>
    </Level2>
    <xsl:apply-templates/>
  </xsl:template>


  <xsl:template match="box">
    <Level3>
      <xsl:value-of select="."/>
    </Level3>
  </xsl:template>


  <xsl:template match="qty">
    <Level4>
      <xsl:value-of select="."/>
    </Level4>
  </xsl:template>


</xsl:stylesheet>



Output:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<Level1>test</Level1>
<Level2>product</Level2>
<Level3>square</Level3>
<Level4>5</Level4>
</root>
barath_dv
Participant
0 Likes

karthikarjun - Thanks for the comment.

<root>
  <dynamic1>
     <dynamic2>
        <dynamic3>square</dynamic3>
<dynamic4>5</dynamic4>
</dynamic2>
</dynamic1>
</root>

The source field names can be dynamic. Is it possible to read and use it?

karthikarjun
Active Contributor
0 Likes

HI barath.v - In the general scenario, the inbound message does not adhere to a specific pattern for data handling. Instead, it is dynamic, indicating the presence of unstructured data. This situation can be both intriguing and challenging.

daviddasilva
Active Contributor
0 Likes

Hi,

It depends if the incoming messages are always the same structure?
If so, then you could use message mapping, create the source and target xsd accordingly.
Alternatively, you could do this with a groovy script which will be much more flexible.

Kind regards,

David

barath_dv
Participant
0 Likes

daviddasilva - Thanks for the comment.

The structure will be dynamic so we cannot make use of message mapping.