Share via


BizTalk Mapper: Source Record is Missing but Still Need to Identify and Map to Target

This Wiki is based on TechNet Wiki Forum Post: source record is missing but still need to identify it and map to target record

Problem

In some cases we need to create destination schema record even if we are not getting respective source record. We can use Logical Existence functoid in fields mapping but it will create destination record only in case we have a source record (empty record will work e.g. <Record />).

Solution

This solution is created for the case where we have no source record and we have to create respective destination record (with default value "NA"). It is achieved by using XSLT as an alternative.

Source Schema

Destination Schema

Custom XSLT for Map

<?xml version="1.0" encoding="UTF-16"?>
<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://LogicalExistenceThroughXSLT.DestinationSchema" xmlns:s0="http://LogicalExistenceThroughXSLT.SourceSchema" xmlns:userCSharp="http://schemas.microsoft.com/BizTalk/2003/userCSharp">
  <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
  <xsl:template match="/">
    <xsl:apply-templates select="/s0:Orders" />
  </xsl:template>
  <xsl:template match="/s0:Orders">
    <xsl:variable name="var:d1" select="count(Order/Detail)" />
    <ns0:Order>
      <Header>
        <ID>
          <xsl:value-of select="Order/Header/ID/text()" />
        </ID>
      </Header>
      <xsl:if test="string($var:d1)>'0'">
        <xsl:for-each select="Order/Detail">
          <Detail>
              <DID>
                <xsl:value-of select="DID/text()" />
              </DID>
              <QTY>
                <xsl:value-of select="QTY/text()" />
              </QTY>
          </Detail>
        </xsl:for-each>
      </xsl:if>
      <xsl:if test="string($var:d1)='0'">
        <Detail>
          <DID>
            <xsl:text>NA</xsl:text>
          </DID>
          <QTY>
            <xsl:text>NA</xsl:text>
          </QTY>
        </Detail>
      </xsl:if>
    </ns0:Order>
  </xsl:template>
</xsl:stylesheet>

Sample Input 1

<ns0:Orders xmlns:ns0="http://LogicalExistenceThroughXSLT.SourceSchema">
  <Order>
    <Header>
      <ID>1</ID>
    </Header>
  </Order>
</ns0:Orders>

Sample Output 1

<ns0:Order xmlns:ns0="http://LogicalExistenceThroughXSLT.DestinationSchema">
  <Header>
    <ID>1</ID>
  </Header>
  <Detail>
    <DID>NA</DID>
    <QTY>NA</QTY>
  </Detail>
</ns0:Order>

Sample Input 2

<ns0:Orders xmlns:ns0="http://LogicalExistenceThroughXSLT.SourceSchema">
  <Order>
    <Header>
      <ID>1</ID>
    </Header>
    <Detail>
      <DID>1</DID>
      <QTY>10</QTY>
    </Detail>
  </Order>
</ns0:Orders>

Sample Output 2

<ns0:Order xmlns:ns0="http://LogicalExistenceThroughXSLT.DestinationSchema">
  <Header>
    <ID>1</ID>
  </Header>
  <Detail>
    <DID>1</DID>
    <QTY>10</QTY>
  </Detail>
</ns0:Order>

Source Code

Sample code can be downloaded from TechNet Gallery:

See Also

Another important place to find a huge amount of BizTalk related articles is the TechNet Wiki itself. The best entry point is BizTalk Server Resources on the TechNet Wiki.