cancel
Showing results for 
Search instead for 
Did you mean: 

Create target XML nodes based on field names in the source

10-03-2023 4:11 PM
pi_consultant1 Participant
1797 views 9 comments
0 Likes
SAP Managed Tags
Subscribe

Hi experts,

I have a Requirement where in I need to map the field names from Source structure to the target structure...

For Example:

My Source structure:

<field1>123</field1>

<field2>XYZ</field2>

Expected Target structure:

<Node>

<Fieldname>field1</Fieldname>

<Fieldvalue>123</Fieldvalue>

</Node>

<Node>

<Fieldname>field2</Fieldname>

<Fieldvalue>XYZ</Fieldvalue>

</Node>

Can you please advise how to achieve this in Graphical Mapping of SAP PI (and maybe with an UDF)?

Best regards,

John

0 Likes

Accepted Solutions (0)

Answers (2)

Answers (2)

Ryan-Crosby
Active Contributor

Hi John,

An XSLT like the following would handle that mapping requirement (assuming a root node named root) -

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="root">
      <xsl:copy>
        <xsl:for-each select="*">
          <Node>
            <Fieldname><xsl:value-of select="local-name()"/></Fieldname>
            <Fieldvalue><xsl:value-of select="."/></Fieldvalue>
          </Node>
        </xsl:for-each>
      </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Regards,

Ryan Crosby

pi_consultant1
Participant
0 Likes

Thank you Ryan,

Would this also be possible with the graphical mapping and perhaps an UDF?

The client prefers that...

Regards

John

Ryan-Crosby
Active Contributor

pi.consultant1 I'm sure it can be done with graphical mapping but it's not worth the effort. The XSLT option took mere minutes, but using graphical mapping requires that you know the field names at build time and they may never change. The XSLT would work if the field names were changed to Field1, Field2 or MyField1, MyField2, but graphical mapping would break immediately.

pi_consultant1
Participant
0 Likes

Thank you Ryan,

I will look into this, however the client prefers a graphical mapping and its a very big xml with already lots of other UDF's.

So it would be nice to have have graphical mapping option too.

Regards,

John

Ryan-Crosby
Active Contributor

pi.consultant1 I'm now with an organization that does not have a PI/PO system and the graphical mapping tool in CPI leaves much to be desired. That said, XSLT is far more powerful and flexible IMO than the trusty old graphical mappings.

fprokopiuk
Active Participant
0 Likes

Hello John,

For UDF + graphical mapping alternative you can refer to this blog: https://blogs.sap.com/2012/11/05/message-mapping-play-around-with-target-structure/.

However I would go with XSLT option here which Ryan suggested.

pi_consultant1
Participant
0 Likes

Thank you Filip.

This is a useful blog, however my usecase is exactly the other way around. So the source looks like this:

<Items>

<CompanyA>abc corp</CompanyA>

<CompanyB>xyz info</CompanyB>

</Items>

And the target like this:

<Rows>

<Key>CompanyA</Key>

<Value>abc corp</Value>

</Rows>

<Rows>

<Key>CompanyB</Key>

<Value>xyz info</Value>

</Rows>

…

It would be great to know exactly what to do if its the other way around...

I also will look at the XSLT option.

Regards,

John

fprokopiuk
Active Participant

To get it other way around you need to find method that reads structure of the input. You can check this documentation: https://help.sap.com/doc/javadocs_nw75_sps04/7.5.4/en-US/PI/overview-summary.html for existing packages. You should find something in com.sap.aii.mappingtool.tf7.rt and com.sap.aii.mapping.api packages.

fprokopiuk
Active Participant

As an alternative you can use "Return as XML" option and parse it in udf using javax.xml.parser. Example of reading xml node names: https://stackoverflow.com/questions/10784036/retrieving-xml-node-names.