cancel
Showing results for 
Search instead for 
Did you mean: 

How to map a field from a XML structure within a structure

pi_consultant1
Participant
0 Kudos

Dear friends,

I have a question about Graphical mapping in PI. I have a XML structure within a XML structure...

A snap shot of my structure looks like this:

<Content>

   <![CDATA[<?xml version="1.0" encoding="UTF-8"?>

    <SendToRequest>

    <ORGANISATION_ID>XXXXX</ORGANISATION_ID>

    <MESSAGE_ID>716378916347891624781</MESSAGE_ID>

    </SendToRequest>]]>

</urn1:Content>

So because of CDATA there can be a XML structure within a XML structure and in I just have to pass the value in from Message_ID to the taget field Content...

It must be possible with an UDF.

Can you please help me with this and provide me the code for this solution?

Thank you,

John

Accepted Solutions (1)

Accepted Solutions (1)

anupam_ghosh2
Active Contributor
0 Kudos

Hi PI Consultant,

                           Please Try this UDF

public static String UDF(String s)

          {

                         try

                         {

                                        int i=s.indexOf("<MESSAGE_ID>"),j=s.indexOf("</MESSAGE_ID>");

                                        s=s.substring(i+"<MESSAGE_ID>".length(),j);

                         }

                         catch(Exception e)

                         {

                                        e.printStackTrace();

                         }

                         return s;

          }

Ideally you need a java/XSLT mapping to get the message ID value. Please try this if this works else you need java/XSLT mapping.

Map like this

content-------->UDF------>target

Regards

Anupam

pi_consultant1
Participant
0 Kudos

Thank you Anupam for your reply...

I will try your solution...

So the UDF looks like this:

Execution Type: Single Values

One Argument with the name: s

And the code looks like this:

public String CDATA(String s, Container container) throws StreamTransformationException{

public static String CDATA(String s);

{

try

{

int i=s.indexOf("<MESSAGE_ID>"),j=s.indexOf("</MESSAGE_ID>");

s=s.substring(i+"<MESSAGE_ID>".length(),j);

catch(Exception e)  

{

e.printStackTrace();

}

return s;

}

It this correct?

Regards,

John

anupam_ghosh2
Active Contributor
0 Kudos

Hi PI Consultant,

                            Here it is once again. Use UDF of type "value"

public String CDATA(String s, Container container) {

try

{

     int i=s.indexOf("<MESSAGE_ID>"),j=s.indexOf("</MESSAGE_ID>");

     s=s.substring(i+"<MESSAGE_ID>".length(),j);

}

catch(Exception e) 

{

     e.printStackTrace();

}

return s;



}

the name of the UDF is CDATA. Thus the mapping becomes

Content ------>CDATA--------->Message ID

Regards

Anupam

Answers (2)

Answers (2)

pi_consultant1
Participant
0 Kudos

Thank you very much Anupam,

This was exactly what I needed

Points are rewarded...

I will bother you more in the future

Regards,

John

iaki_vila
Active Contributor
0 Kudos

Hi,

You could use a XSL mapping, as well.

With this example:

<?xml version="1.0" encoding="UTF-8"?>

<urn1:Content xmlns:urn1="http//:someweirdnamespace">

<![CDATA[<?xml version="1.0" encoding="UTF-8"?>

    <SendToRequest>

    <ORGANISATION_ID>XXXXX</ORGANISATION_ID>

    <MESSAGE_ID>716378916347891624781</MESSAGE_ID>

    </SendToRequest>]]>

</urn1:Content>

If you use this  XSL:

<?xml version="1.0" encoding="UTF-8"?>

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

    <xsl:output method="text" indent="no" encoding="UTF-8"/>

    <xsl:template match="/">

        <xsl:apply-templates/>

    </xsl:template>

    <xsl:template match="Extract">   

            <xsl:copy-of select="@*"/>

    </xsl:template>

</xsl:stylesheet>

You will have the follow output XML:

<?xml version="1.0" encoding="UTF-8"?>

    <SendToRequest>

    <ORGANISATION_ID>XXXXX</ORGANISATION_ID>

    <MESSAGE_ID>716378916347891624781</MESSAGE_ID>

    </SendToRequest>

Regards.

pi_consultant1
Participant
0 Kudos

Thank you Inaki

Unfortunately I have to use UDF because its a change on a graphical mapping...

Regards,

John

Former Member
0 Kudos

Hi John,

Use can use the below dynamic configuration UDF to get the message ID.

public String msgid(Container container){

DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);

DynamicConfigurationKey keyHeader1 = DynamicConfigurationKey.create( "http://sap.com/xi/XI/System/File", "Message_ID");

String msgid = conf.get(keyHeader1);

return msgid;