cancel
Showing results for 
Search instead for 
Did you mean: 

Prefix inside XML tags

0 Kudos

Hi,

I am working on a scenario where I need to insert prefix to XML tags that belong to Header section. Following is an example of it -

<sh:StandardBusinessDocumentHeader>

  <sh:HeaderVersion>1.0</sh:HeaderVersion>

  <sh:Sender>

  <sh:Identifier Authority="GS1">5412345000013</sh:Identifier>

  <sh:ContactInformation>

  <sh:Contact>John Doe</sh:Contact>

  </sh:ContactInformation>

  </sh:Sender>

<advancedRemittanceNotification>

        <creationDateTime>2011-04-12T11:00:00.000-05:00</creationDateTime>

        <documentStatusCode>ORIGINAL</documentStatusCode>

        <advancedRemittanceNotificationIdentification>

            <entityIdentification>ARN51101</entityIdentification>

            <contentOwner>

                <gln>5412345000013</gln>

            </contentOwner>

        </advancedRemittanceNotificationIdentification>

        <expectedPaymentDate>2005-04-15</expectedPaymentDate>

        <isThereADiscrepancy>false</isThereADiscrepancy>

        <receiptOfGoodsDate>2005-02-19</receiptOfGoodsDate>

       

As can be seen, prefix 'sh' is added to the header only. When sending out, 'sh' needs to be inserted and incoming messages to PI will have 'sh' prefixed to header elements too.

Is only XSLT mapping a best solution for this or someone has experience handling in some other way ?

Would appreciate if someone can share XSLT code for it.

Thanks

Accepted Solutions (0)

Answers (4)

Answers (4)

RaghuVamseedhar
Active Contributor
0 Kudos

Elly,

Create target structure like this


<sh___ContactInformation>

  <sh___Contact>John Doe</sh___Contact>

</sh___ContactInformation>

: will be ___ (there underscores).

Use Java mapping after Message Mapping to create required target not well-formed XML.


package com.map; 

import java.io.*;

import com.sap.aii.mapping.api.*;

public class Test_JavaMapping extends AbstractTransformation {

    @Override

    public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput) throws StreamTransformationException {

        try {

            InputStream inputstream = transformationInput.getInputPayload().getInputStream();

            OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream();

            // Copy Input content to Output content 

            byte[] b = new byte[inputstream.available()];

            inputstream.read(b);

            String input = new String(b);

            input = input.replaceAll("___", ":");

            outputstream.write(input.getBytes());

        } catch (Exception exception) {

            getTrace().addDebugMessage(exception.getMessage());

            throw new StreamTransformationException(exception.toString());

        }

    }

}

maheswarareddykonda
Active Contributor
0 Kudos

Hi Raghu,

your approch is wonderful..and we suppose to do declare prefixes(sh:) in namespace level...

can u pls add that code too

former_member186851
Active Contributor
0 Kudos
Harish
Active Contributor
0 Kudos

Hi Elly,

Please check the Michal's blog

regards,

Harish

iaki_vila
Active Contributor
0 Kudos

Hi Elly Kan,

You can do it with java mapping or message mapping if you prepare an XSD that fulfill your requirement. If you share a source and target XML, it will be easier for SCN member to develop a XSLT, right now i haven't so clear you source and target XML.

Regards.

0 Kudos

Hi Inaki,

Source fields will be from IDoc and target is an XSD. XSD is not allowing to put 'sh:' prefix in the header elements and is giving error.

I have just seen the sample files and noticed the prefix in tags. Dont have source and target messages ready yet.

Any generic idea how to include prefix in XML element tags using XSLT ? I can then try to build over that once the messages are ready.

For ex - source message is <Name>Ted</Name>

                                         <City>HK</City>

                                          <ZIP>WD34R</ZIP>

and Target message should be like

                                        <Name>Ted</Name>

                                         <sh:City>HK</City>

                                         <ZIP>WD34R</ZIP>

Thanks a lot!

iaki_vila
Active Contributor
0 Kudos

Hi Elly,

First of all your target XML is incorrect, it should be:


                                         <Name>Ted</Name>

                                         <sh:City>HK</sh:City>

                                         <ZIP>WD34R</ZIP>

You can map with XSL tag by tag, the easiest way to do it, but not the best way to do it.

You can set in the XML the tag directly in the way:

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

<sh:City><xsl:value-of select="//City" /></sh:City>

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

Regards,

0 Kudos

Thanks Inaki.

You are correct, I manually typed and missed putting 'sh:' in closing tag.

If I have say 30 fields after </sh:city> which do not require 'sh:' prefix, then still I need to map tag by tag for each of them or there is some other way around ? They will be used as they are in the XSD.

iaki_vila
Active Contributor
0 Kudos

Hi Elly,

Kindly wait more suggestions, may be someone knows a best way. I don't know a better way to do it. Depending where are the tags with the new namespace you can simplify your XSLT, but in the worse case you will need to type all the fields. You can use a java mapping, but finally you would need to go tag by tag as well.

Regards.