Thursday, December 25, 2014

Copy Complete Source Message to a Single Node in Destination

Consider a Input Message.

  1. <ns0:Input attr="attr_0" xmlns:ns0="http://MappingXML.Input">
  2.   <Name>Name_0</Name>
  3.   <age>age_0</age>
  4. </ns0:Input>


Which need to be mapped to a single node in Output Message,

  1. <ns0:Output xmlns:ns0="http://MappingXML.output">
  2. <Existing>Existing_0</Existing>
  3. <SourceXML>Source XML Message comes Here</SourceXML>
  4. </ns0:Output>

so that the Output Message would look like,

  1. <ns0:Output xmlns:ns0="http://MappingXML.output">
  2.   <Existing>abc</Existing>
  3.   <SourceXML>
  4.     <ns0:Input attr="attr_0" xmlns:ns0="http://MappingXML.Input">
  5.     <Name>Name_0</Name>
  6.     <age>age_0</age>
  7.     </ns0:Input>
  8.   </SourceXML>
  9. </ns0:Output>


You can achieve it using a custom XSLT map, as described below.
  1. <?xml version="1.0" encoding="UTF-16"?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var s0 userCSharp" version="1.0" xmlns:ns0="http://MappingXML.output" xmlns:s0="http://MappingXML.Input" xmlns:userCSharp="http://schemas.microsoft.com/BizTalk/2003/userCSharp">
  3.   <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
  4.   <xsl:template name="identity" match="@* | node()">
  5.     <xsl:copy>
  6.      <xsl:apply-templates select="@* | node()"/>
  7.     </xsl:copy>
  8.   </xsl:template>
  9.   <xsl:template match="/s0:Input">
  10.     <ns0:Output>
  11.       <Existing>Something</Existing>
  12.       <xsl:element name="SourceXML">
  13.         <xsl:call-template name="identity" />
  14.       </xsl:element>
  15.     </ns0:Output>
  16.   </xsl:template>
  17. </xsl:stylesheet>
Here the entire logic to copy the Source Message is written in Line No 4 to 8, and Line No 12 to 14.
here we are creating a XSL template "identity" which copies the entire message to the place where template is called.

<xsl:call-template name="identity" />
above line calls the template written at line number 12 to 14, which creates the element "SourceXML" and then calls the template.
    <xsl:element name="SourceXML">
        <xsl:call-template name="identity" />
   </xsl:element>
Line number 10 to 15 is used to create the Output Message structure in the Output XML. you can add all the nodes you require in output message here,
    <ns0:Output>
      <Existing>Something</Existing>
      <xsl:element name="SourceXML">
        <xsl:call-template name="identity" />
      </xsl:element>
    </ns0:Output>
If you face problem in creating the other part of the XSLT map, then you can generate it by following the below steps,
  1. Create a map in Biztalk,
  2. Map the source and destination schema, but don't create any link.
  3. Debug the map, Visual studio automatically creates an XSL sheet for the map which you can save and edit it as per your requirements.
For mapping your custom XSLT to the map click on graph part of the map, then in the Property Bag, search for Property named "Custom XSLT Path", click on the ellipsis button and browse to the path of your XSLT file.