Search This Blog

Tuesday, 2 October 2018

Copying the root node in the source Schema to the CDATA or String field in destination schema in BizTalk Map


In this article, I would like to share some of my experience on the copying the root/any specific node as CDATA/string to the destination element using in BizTalk Map.
There are 2 options that we have to achieve the above mentioned.
1)      Using C#
2)      Using XSLT(Inline XSLT)
I have used below 2 schemas as an example but you can change them according to your own requirement.
CustomerXmlSchema.xsd
<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns="http://BizTalkConcepts.CustomerXmlSchema" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" elementFormDefault="qualified" targetNamespace="http://BizTalkConcepts.CustomerXmlSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Customers">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Customer">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="CustomerId" type="xs:string" />
              <xs:element name="CustomerName" type="xs:string" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
CustomerCDATASchema.xsd
<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns="http://BizTalkConcepts.CustomerCDATASchema" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" elementFormDefault="qualified" targetNamespace="http://BizTalkConcepts.CustomerCDATASchema" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Customers">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="CustomersCDATA" type="xs:string" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
Create a map CustomerXmlSchema_To_CustomerCDATASchema.btm:- In this Map we are getting an XML document as a source and we need to copy the whole document to a CDATA/string element in the destination schema.
  
1) Using C#

Note:-This method used both C# and XSLT capabilities to achieve the required functionality.
Step 1:- Drag and drop the scripting functiod(This node has no mapping to it.) and select Script type to Inline C# and copy-paste the below code in it.
        public string ConvertNodeToXmlString(XPathNodeIterator node)
        {
            node.MoveNext();
            return node.Current.OuterXml;
        }

Step 2:-Add another scripting functiod and select the Script type to Inline XSLT and copy and paste the below code.
<!--This code creates the CustomersCDATA element and calls the ConvertNodeToXmlString(C# method)
to copy the source XML to the CustomersCDATA element in this example. If your elementFormDefault property in the destination schema is set to default or unqualified then you must not use ns0:CustomersCDATA must instead use must CustomersCDATA -->
<xsl:element name="ns0:CustomersCDATA">
  <xsl:value-of select="userCSharp:ConvertNodeToXmlString(.)" />
</xsl:element>

This Scripting functiod need to be mapped to CustomersCDATA element in the destination schema.
The final view of the map looks as shown below.

 2)      Using XSLT
Step 1:- Drag and drop the scripting functiod(This node has no mapping to it.) and select Script type to Inline XSLT and copy-paste the below code in it.

<!--This code creates the CustomersCDATA element to copy the source XML to the CustomersCDATA element in this example. If your elementFormDefault property in the destination schema is set to default or unqualified then you must not use ns0:CustomersCDATA must instead use must CustomersCDATA -->
<xsl:element name="ns0:CustomersCDATA">
  <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
  <xsl:copy-of select="." />
  <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
</xsl:element>

This Scripting functiod need to be mapped to CustomersCDATA element in the destination schema.
The final view of the map looks as shown below.










NB: I hope this article helps people out there. Kindly comment or ask questions if you need mode clarity on this topic.


No comments:

Post a Comment

Sending Email to Multiple recipients using PowerShell

Hi All,  Below is the working code to Send emails to Multiple recipients using PowerShell. [STRING]$PSEmailServer = "YourSMTPServerIPOr...