Monday, April 16, 2012

Mapping xs:any element to xs:base64Binary element


Source schema is containing <xs:any> element as shown below:


Destination schema is containing element with <xs:base64Binary> as data type as shown below:


To map the string value of <xs:any> element from source to Details element of type  <xs:base64Binary>  in destination. Drag a scripting functoid to map as shown below:


Use the following Inline C# code to convert string into Base64string:

public string GetBase64String(string stringData)
{
   byte[] encodedBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(stringData);
   return System.Convert.ToBase64String(encodedBytes);
}

Drag another scripting functoid and set script type to Inline XSLT as shown below:

 
Use following Inline XSLT code:

<Details xmlns:p="http://MappingBase64Encoded.SourceSchema">
  <xsl:value-of select="userCSharp:GetBase64String(string(/p:Record/Details/*))" />      
</Details>
Note: Here Details element refer to the element in the destination schema and this XPath is to extract the value of child element of Details as there is only one element if there are more then position of child element need to be specified.
Connect the second scripting functoid (having Inline XSLT) to Details element in destination schema as show below:
 
 

Testing the map

Source Instance used:
<ns0:Record xmlns:ns0="http://MappingBase64Encoded.SourceSchema">
  <Source>SourceElement</Source>
  <Details>
    <Data01>This is demo content from source.</Data01>
  </Details>
</ns0:Record>
Map output:
<ns0:Record xmlns:ns0="http://MappingBase64Encoded.DstSchema">
  <Source>SourceElement</Source>
  <Details>VGhpcyBpcyBkZW1vIGNvbnRlbnQgZnJvbSBzb3VyY2Uu</Details>
</ns0:Record>

Cheers
Rohit Sharma

No comments:

Post a Comment