Perhaps there may be many ways to manage all the above requirements but sometimes it becomes like using a cannon to kill a fly! So here we go with our favorite message mapping and see how to play around with tag name, occurrence, namespace and prefix at target side. Also we will try to pass value to structure node, seems weird? Let’s see how it is possible with simple user defined function.
In first step to access target field in udf, we take help of getParameter method of Container with parameter STRUCTURE_NODE that returns access to target field/node to which the UDF is being directly mapped to. The node needs to be type casted to structure/leaf as per the type of target field.
Applicable to fields having child or sub-fields.
Applicable to fields having no child or sub-fields.
Next step involves using node methods as per the requirements.
Return the name of target field.
Set the target field name with input parameter newName.
Set the namespace declaration and attribute with input parameter decl.
e.g decl = " attribute1=\"a1\" attribute2=\"a2\" xmlns:prefix=\"namespace1\""
Set the occurrence of target field with input parameter min and max.
Add the string prevalue unescaped just after start tag of target field. Applicable only for structure field.
Add the string postvalue unescaped just before end tag of target field. Applicable only for structure field.
For example, lets say we have the source and target structure as shown below:
Field Name | Occurence | Field name | Occurence |
---|---|---|---|
Source | 1..1 | Target | 1..1 |
Rows | 0..Unbounded | Items | 0..Unbounded |
Key | 1..1 | Element | 1..1 |
Value | 1..1 |
And the requirements are..
Source instance and expected result xml are as shown below.
Source Instance | Result |
---|---|
<?xml version="1.0" encoding="UTF-8"?> <ns0:Source xmlns:ns0="http://test.com"> <Rows> <Key>CompanyA</Key> <Value>abc corp</Value> </Rows> <Rows> <Key>CompanyB</Key> <Value>xyz info</Value> </Rows> ... <Rows> <Key>CompanyZ</Key> <Value>123 ent</Value> </Rows> </ns0:Source> | <?xml version="1.0" encoding="UTF-8"?> <pfx:Target xmlns:pfx="http://company.com"> list of top 2 companies <Items> <CompanyA>abc corp</CompanyA> </Items> <Items> <CompanyB>xyz info</CompanyB> </Items> </pfx:Target>
|
And here comes the mapping part..
UDF setNSDeclarations would fix namespace to http://company.com , fix prefix to pfx and assign value "list of top 2 companies" to Target node.
public String setNSDeclarations(String prefix, String nmspace ,Container container) {
StructureNode node = ((StructureNode) container.getParameter("STRUCTURE_NODE"));
node.setNSDeclarations(" xmlns:" + prefix + "=" + nmspace); // notice the space before xmlns:
node.setQName(prefix + ":Target");
node.setPreValue("list of top 2 companies");
return"";
}
UDF setOccurences simply set the max occurence of Items to 2 to ensure top 2 Rows are mapped to Items.
public String setOccurences(String sourcenode, int maxOccur, Container container) {
StructureNode node = ((StructureNode) container.getParameter("STRUCTURE_NODE"));
node.setOccurences(0, maxOccur);
return "";
}
UDF setTargetFieldName dynamically changes the tag name Element.
public String setTargetFieldName(String targetFieldName, String targetFieldvalue, Container container) {
LeafStructureNode node =((LeafStructureNode) container.getParameter("STRUCTURE_NODE"));
node.setQName(targetFieldName);
return targetFieldvalue;
}
At end, please note that the code discussed above uses undocumented classes/methods that are subject to change in future without notice. In short, use at your own risk as any related issues will not be supported by SAP
Reference :
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
10 | |
7 | |
7 | |
5 | |
4 | |
4 | |
4 | |
3 | |
3 | |
3 |