Remove Namespace in PI by XMLAnonymizer Bean in communication channel-Example step by step
We may have to nurture with the namespaces in the payload before posting to the end system.The requirement could be to remove namespace prefix in XML Payload.This module then can help the developer without writing long XSLT codes to achieve the result.
First let us understand the basics of Namespace.
What is a namespace?
Ans:XML namespaces are used for providing uniquely named elements and attributes in an XML document.
Use?
Ans:XML namespace is to avoid naming conflicts when using and re-using multiple times.
How we use it in the XML?
Ans:Example below.
<ns0:table xmlns:ns0=“http://www.w3.org/TR/html4/“>
In the attribute xmlns:ns0, xmlns is a reserved word, used to declare a namespace. That is the above example is read as binding the prefix ‘ns0’ with the namespace ‘http://www.w3.org/TR/html4/’
Now let us see the use of the XML Anonymizer Bean in real case.
We had a scenario where the output was like below after the mapping in PI.
<?xml version=“1.0” encoding=“UTF-8”?>
<soap:Envelope xmlns:soap=“http://schemas.xmlsoap.org/soap/envelope/“ xmlns:xsd=“http://www.w3.org/2001/XMLSchema“ xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance“>
<soap:Body>
<ns1:ImportString xmlns:ns1=“http://xyz.com/“>
<ns1:filename>test.xml</ns1:filename>
<ns1:payload>xxx</ns1:payload>
</ns1:ImportString>
</soap:Body>
</soap:Envelope>
However the customer wanted us to remove the prefix of ns1 without removing the namespace.However they wanted the soap,xsd and xsi prefix and namespace both to be there at the output. So they wanted a structure like below.
<?xml version=“1.0” encoding=“UTF-8”?>
<soap:Envelope xmlns:soap=“http://schemas.xmlsoap.org/soap/envelope/“ xmlns:xsd=“http://www.w3.org/2001/XMLSchema“ xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance“>
<soap:Body>
<ImportString xmlns=“http://xyz.com/“>
<filename>test.xml</filename>
<payload>xxx</payload>
</ImportString>
</soap:Body>
</soap:Envelope>
To do this we had to use the XML Anonymizer Bean like below
Here is the configuration procedure:
- Add the Module in the Processing Sequence.Insert this Anonymizer module before the adapter module as shown above.
Module Name : AF_Modules/XMLAnonymizerBean
Module Type:Local Enerprise Bean
Module Key: 0
The module name ‘CallSapAdapter’ is default one that can be left as it is
- Add Parameters in the Module Configuration.
Module Key: 0
Parameter Name: anonymizer.acceptNamespaces
Parameter Value: http://www.w3.org/2001/XMLSchema-instance xsi http://www.w3.org/2001/XMLSchema xsd http://schemas.xmlsoap.org/soap/envelope/ soap http://xyz.com/ ”
Enter a list of namespaces and their prefixes that are to be kept in the target XML document and to result a namespace without a prefix, enter ” (two single quotation marks).
Module Key: 0
Parameter Name: anonymizer.quote
Parameter Value: ‘
Here specify the character to be used to enclose the attribute values. The default value is ‘.
Once we activate all the above configurations and execute the scenario we get the desired output as below.
<soap:Envelope xmlns:soap=“http://schemas.xmlsoap.org/soap/envelope/“ xmlns:xsd=“http://www.w3.org/2001/XMLSchema“ xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance“>
<soap:Body>
<ImportString xmlns=“http://xyz.com/“>
<filename>test.xml</filename>
<payload>xxx</payload>
</ImportString>
</soap:Body>
</soap:Envelope>
Now how to replace namespace? Check this Replace Namespace in PI by XMLAnonymizer Bean in communication channel-Example step by step
Nice blog Arijit. Please keep these coming. 🙂
Thanks Rob....One Guru like you is always inspiring 🙂
Hi Arijit,
It is a very nice blog using this we can avoid coding to perform the activity.
Can you please tell us what does the Parameter Value: http://www.w3.org/2001/XMLSchema-instance xsi http://www.w3.org/2001/XMLSchema xsdhttp://schemas.xmlsoap.org/soap/envelope/ soap http://xyz.com/ ''
do to remove the ns1.
Regards,
Avinash.
Hi Avinash,
In our requirement we needed to keep the below prefix and namespaces.
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
However we were asked to remove the prefix "ns1" but keep the corresponding namespace.
xmlns:ns1="http://xyz.com/
And as per the rule,you need to mention the namespaces and their prefix separated by a space if you want to keep them at output. And if you want a namespace without a prefix, enter '' (two single quotation marks). So we had to form the below value for that parameter.
http://www.w3.org/2001/XMLSchema-instance xsi http://www.w3.org/2001/XMLSchema xsdhttp://schemas.xmlsoap.org/soap/envelope/soap http://xyz.com/ ''
If you notice carefully then you can see I have written all the namespace and prefix pair separated by space, whom I needed to get in the output and for the one where the prefix were needed to be suppressed,I mentioned '' in the place of prefix for its corresponding namespace.
Thanks,
Arijit
Hi Arjit,
Thank you very much for your reply now i understood the parameter value.
Is it possible to replace the prefix with another prefix for that namespace.
Eg: replacing "ns1" prefix with "ns2" is it possible.
http://www.w3.org/2001/XMLSchema-instance xsi http://www.w3.org/2001/XMLSchemaxsdhttp://schemas.xmlsoap.org/soap/envelope/soap http://xyz.com/ns2
does the above parameter value works?
Regards,
Avinash
Hi Avinash,
For replacing namespace you may need to write XSLT mapping code. Probably this bean will not work for this purpose.
Thanks,
Arijit
Hi Avinash,
I did some further investigation and yes,you can replace the namespace with the above parameter you mentioned.Please refer Replace Namespace in PI by XMLAnonymizer Bean in communication channel-Example step by step
Thanks,
Arijit
Hi Arijit,
My requirement is to remove the header information in the soap envelope.
In soap header i am getting some dummy data from source side.Is there ant chance to remove soap header with XMLAnonymizerBean.
Hi Mohit,
Not tried yet!! Go for XSLT, that might be easy.
Thanks
Arijit
Hi Arjit,
Does this XML anonymizer bean works to remove namespace for SOAP Body element ?In your example to remove xmlns=“http://xyz.com/“ ?In my case the SOAP body element has <ns0:Element xmlns:ns0="http://www.xxxxx.com/wfm/"> I need to remove the namespace but needs to replace the prefix ns0 with wfm for all the elements in the SOAP Body .
Could you please let me know how we can do this?
Thanks and Regards,
Raj
Hi Rajesh,
You can use XSLT mapping remove all the namespace.
Thanks
Arijit
Hi Team,
Â
Have the similar requirement and configured the Module parameters in JMS sender adapter. The prefix started with <dsr:LineItemItems>6</dsr:LineItemItems> are removed by the module parameters but in the main tag the “xmlns” & “xsi” namespaces are missing in the ouput xml.
Â
Please help us to get the main tag with the namespaces.
Â
Â
Input xml:
<?xml version=”1.0″ encoding=”utf-8″?>
<POSLog xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:dsr=”http://www.dsr.com/rsd/tlog/markup/poslog” xsi:schemaLocation=”http://www.nrf-arts.org/IXRetail/namespace/ POSLog.xsd http://www.dsr.com/rsd/tlog/markup/poslog DSRPOSLog.xsd” xmlns=”http://www.nrf-arts.org/IXRetail/namespace/”>
<Transaction CancelFlag=”false” TrainingModeFlag=”false”>
<RetailStoreID>1101</RetailStoreID>
<BusinessUnit>
<UnitID Name=”1101″ TypeCode=”RetailStore”>Store_1101</UnitID>
Â
Â
Module Configurations in JMS sender adapter:
Â
Â
Output Message:
<?xml version=’1.0′ encoding=’utf-8′?>
<POSLog schemaLocation=’http://www.nrf-arts.org/IXRetail/namespace/ POSLog.xsd http://www.ncr.com/rsd/tlog/markup/poslog DSRPOSLog.xsd’>
<Transaction CancelFlag=’false’ TrainingModeFlag=’false’>
<RetailStoreID>1101</RetailStoreID>
Â
Â
Namespaces are missing in the output messages the POSLog message.
Â
Thanks,
Ravi
Â
Â
Â
Â
Â
Â