cancel
Showing results for 
Search instead for 
Did you mean: 

add namespace declaration into xml root element

Former Member
0 Kudos

Hello experts,

I have the following problem:

I generate a xml message with the following structure (example):

How can I realise this requirement?

Thanks and best regards!

Christopher Kühn

Accepted Solutions (1)

Accepted Solutions (1)

former_member187339
Active Contributor
0 Kudos

Hi Christopher,

Are you defining the Data type in PI? If yes then you can add the namespace while creating Message Type else if you are using XSD, then try to use XMLAnonymizerBean in your sender communication channel. You will get blogs/wiki pages on the usage of this bean

XMLAnonymizerBean

http://help.sap.com/saphelp_nw04/helpdata/en/45/d169186a29570ae10000000a114a6b/content.htm

Regards

Suraj

Former Member
0 Kudos

Hello Suray,

I use a xsd of an external partner.

I send the message to the external partner, so I have to use the module in receiver channel. Is that also possible? I think no.

Which possibilities have I?

best regards

Chris

former_member187339
Active Contributor
0 Kudos

Hi Christopher,

Yes that module can be used with receiver channels too (and channel in j2ee stack)

But if you want to append the namespace with receiver XML, then you can try to consider the option of XSLT of java mapping too...

Like in your operation mapping, today you have a normal mapping to convert xource XML to target XML (without namespace), you need to add one more java/xslt mapping and change this target (which is without namespace) to one with namespace... Java mapping will be the easiest one to do here..

Regards

Suraj

former_member200962
Active Contributor
0 Kudos
so I have to use the module in receiver channel. Is that also possible? I think no

Yes it is possible to use the XMLAnonymizerBean in the receiver channel......infact it is used in the receiver channel to make the namespace compatible with the receiving system.....if you still have a doubt you can refer the example in this blog where the module is used in receiver channel: /people/gabrielsagayaselvam.panneerselvam/blog/2009/12/07/standard-adapter-framework-modules-afmodules-in-pi-71-150part-1

Regards,

Abhishek.

Former Member
0 Kudos

Hello Suraj,

thanks for your answer.

I have no experience with java mappings. what I have to do? is there a guide for it?

have you sample java code for me to?

is a common java class enough which I import as .jar in esr? we have pi 7.11 system.

Thanks and regards.

former_member200962
Active Contributor
0 Kudos

you can even do it using XSLT (simpler as compared to JAVA)....check the code given by Stefan in this similar thread:

Regards,

Abhishek.

former_member187339
Active Contributor
0 Kudos

Hi Christopher,

Call the below code as a javamappinf in the operation mapping... So now your operation mapping will have two mappings one to convert source to target XML and second this java mapping which will add namespace to your target xml


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import com.sap.aii.mapping.api.AbstractTransformation;
import  com.sap.aii.mapping.api.DynamicConfiguration;
import com.sap.aii.mapping.api.DynamicConfigurationKey;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
import com.sap.aii.mapping.api.InputHeader;
 
public class JavaMapping extends AbstractTransformation {
     public void transform(TransformationInput arg0, TransformationOutput arg1) throws StreamTransformationException {
	getTrace().addInfo("JAVA Mapping Called");
	
	//Input payload is obtained by using arg0.getInputPayload().getInputStream()
	String inData = convertStreamToString(arg0.getInputPayload().getInputStream());

	String outData = inData.replaceFirst("<root>", "<root xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespace=\"http://test.de/test.xsd\">");
	
	try
	{
	//8. The JAVA mapping output payload is returned using the TransformationOutput class
	// arg1.getOutputPayload().getOutputStream()
		arg1.getOutputPayload().getOutputStream().write(outData.getBytes("UTF-8"));
	}
	catch(Exception exception1) { }
     }
     public String convertStreamToString(InputStream in){
	StringBuffer sb = new StringBuffer();
	try
	{
	InputStreamReader isr = new InputStreamReader(in);
	Reader reader =
	new BufferedReader(isr);
	int ch;
	while((ch = in.read()) > -1) {
		sb.append((char)ch);}
		reader.close();
	}
	catch(Exception exception) { }
	return sb.toString();
    }
}

check stefans blog on the jar files that you need to make this mapping /people/stefan.grube/blog/2006/10/23/testing-and-debugging-java-mapping-in-developer-studio

Regards

Suraj

Regards

Suraj

Former Member
0 Kudos

Hi,

thanks to all for the answers.

Can anybode provide a xsl to convert the element <root> into <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespace="http://test.de/test.xsd"

Thanks a lot

Christopher

Former Member
0 Kudos

Hello,

I have tried with the following 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="xml" encoding="UTF-8"/>

<xsl:template match="/root">

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespace="http://test.de/test.xsd">

<xsl:copy-of select="/root" />

</root>

</xsl:template>

</xsl:stylesheet>

-


this xsl generates the following output:

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespace="http://test.de/test.xsd">

<root>

...

</root>

</root>

how can I realise that I have only one root element?

thanks and regards

former_member200962
Active Contributor
0 Kudos

try with this XSLT....inlcude it as the very first mapping in Interface Mapping

<?xml version='1.0' encoding='utf-8'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template match="/">
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespace="http://test.de/test.xsd">
<xsl:copy-of select="." /> 
</root>
</xsl:template>
</xsl:stylesheet>

Regards,

Abhishek.

Former Member
0 Kudos

Hi,

I solved the problem with the following 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="xml" encoding="UTF-8"/>

<xsl:template match="/root">

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespace="http://test.de/test.xsd">

<xsl:copy-of select="/root/name"/>

<xsl:copy-of select="/root/email"/>

</root>

</xsl:template>

</xsl:stylesheet>

Thanks to all for your help!

regards!

Christopher

Answers (1)

Answers (1)

Former Member
0 Kudos

Christopher,

XMLAnonymizerBean module can be implemented for your requirement, but it can not be implemented with XI adapter, other option would be to add namespace in XSD it self.

Try to add Namespace in xsd as per below code


<xs:schema xmlns:xs="http://www.test.org" xmlns="namespace" elementFormDefault="qualified" targetNamespace="namespace">