Showing posts with label Mapper. Show all posts
Showing posts with label Mapper. Show all posts

Tuesday, July 19, 2011

How to remove leading zeros from the value of element in a map

Charan: I have one additional requirement to my old problem mentioned here. Now I want to remove the leading  Zeros for the value of Element1 in addition to finding the existence of this optional element in input schema. Is there any easy way to do it.

Rohit: hmmm yes we can do it using the scripting functoid and simple C# code. Let me show you how.

Step1: Add new scripting functoid.

 
Step2: Configure Functoid Script use Inline C# as Script Type: and use the following simple C# code to remove the leading zeros in string:

public string RemoveLeadingZeros(string param1)
{
      string result=param1;
      if(param1.StartsWith("0"))
      {
          result=param1.TrimStart(new char[]{'0'});        
      }  
      return result;
}


Step3: Modify the existing scripting functoid to use this code to remove the leading zeros:

<Element xmlns:p="http://CheckNodeExistenceInMap.InputSchema_XML">
    <xsl:choose>
        <xsl:when test="/p:Record/Element1">
            <xsl:value-of select="userCSharp:RemoveLeadingZeros(string(/p:Record/Element1))" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="/p:Record/Element2" />
        </xsl:otherwise>
    </xsl:choose>
</Element>


Cheers
Rohit Sharma

Friday, July 15, 2011

Checking if the optional element exists or not in Mapping

Charan: I have a problem I have one schema which has one optional filed 'Element1' and I need to map this schema to other schema but the twist is if this optional filed is not present in the input schema then I need to map other filed 'Element2' to the output schema.
 Input schema with optional filed 'Element1'

 Output Schema

Rohit: You can do this using the Scripting functoid as shown below:
 Drag scripting functoid

 Configure scripting functoid

Following is the XSLT used for scripting functoid.
<Element xmlns:p="http://CheckNodeExistenceInMap.InputSchema_XML">
    <xsl:choose>
        <xsl:when test="/p:Record/Element1">
            <xsl:value-of select="/p:Record/Element1" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="/p:Record/Element2" />
        </xsl:otherwise>
    </xsl:choose>
</Element>

Note: If the value of property 'Element FormDefault' for the source schema is 'Qualified' then the alias for namespace need to be used for each element as mentioned below:

<Element xmlns:p="http://CheckNodeExistenceInMap.InputSchema_XML">
    <xsl:choose>
        <xsl:when test="/p:Record/p:Element1">
            <xsl:value-of select="/p:Record/p:Element1" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="/p:Record/p:Element2" />
        </xsl:otherwise>
    </xsl:choose>
</Element> 



Cheers
Rohit Sharma