Understanding the BizTalk Mapper: Part 5 - Logical Functoids#
Whenever I've looked at the XSLT generated by a map I've always been confused by the amount of inline C# generated by these functoids.
After the String Functoids I'd say that these are the next most widely used and yet all but one of them has an XSLT v1.0 equivalent!
The code emitted for "Logical Equal" always makes me laugh – 12 lines of C# code can be replaced by... (wait for it)... one "=" symbol!

For each functoid I've shown:
  1. Whether XSLT or C# is emitted
  2. Whether an XSLT equivalent exists
  3. The XSLT or C# emitted by the functoid
  4. Where C# is emitted, the equivalent XSLT to achieve the same functionality (in both XSLT v1.0 and v2.0)
Functoids covered in this category:
Equal Logical Existence
Greater Than Logical NOT
Greater Than or Equal To Logical Numeric
IsNil Logical OR
Less Than Logical String
Less Than or Equal To Not Equal
Logical Date Common Code

Note:
This is the fifth in a series of 13 posts about the BizTalk Mapper.
The other posts in this series are (links will become active as the posts become active):
Understanding the BizTalk Mapper: Part 1 - Introduction
Understanding the BizTalk Mapper: Part 2 - Functoids Overview
Understanding the BizTalk Mapper: Part 3 - String Functoids
Understanding the BizTalk Mapper: Part 4 - Mathematical Functoids
Understanding the BizTalk Mapper: Part 5 - Logical Functoids
Understanding the BizTalk Mapper: Part 6 - Date/Time Functoids
Understanding the BizTalk Mapper: Part 7 - Conversion Functoids
Understanding the BizTalk Mapper: Part 8 - Scientific Functoids
Understanding the BizTalk Mapper: Part 9 - Cumulative Functoids
Understanding the BizTalk Mapper: Part 10 - Database Functoids
Understanding the BizTalk Mapper: Part 11 - Advanced Functoids
Understanding the BizTalk Mapper: Part 12 - Performance and Maintainability
Understanding the BizTalk Mapper: Part 13 - Is the Mapper the best choice for Transformation in BizTalk?

Download the complete series as a single Microsoft Word document (1.2MB) or Adobe PDF document (620kb).

Logical Functoids

 

Equal

 

 

Generates: C#

Has XSLT Equivalent: in 1.0 and 2.0

  Emitted Code:
public bool LogicalEq(string val1, string val2)
{
    bool ret = false;
    double d1 = 0;
    double d2 = 0;
    if (IsNumeric(val1, ref d1) && IsNumeric(val2, ref d2))
    {
        ret = d1 == d2;
    }
    else
    {
        ret = String.Compare(val1, val2, StringComparison.Ordinal) == 0;
    }
    return ret;
}
 

XSLT 1.0 Equivalent: Use the "=" operator e.g. value = value

  XSLT 2.0 Equivalent: Use the "=" operator e.g. value = value
Note: Both the C# version and the XSLT version will perform a case-sensitive comparison.
 

 

Greater Than

 

 

Generates: C#

Has XSLT Equivalent: in 1.0 and 2.0

  Emitted Code:
public bool LogicalGt(string val1, string val2)
{
    bool ret = false;
    double d1 = 0;
    double d2 = 0;
    if (IsNumeric(val1, ref d1) && IsNumeric(val2, ref d2))
    {
        ret = d1 > d2;
    }
    else
    {
        ret = String.Compare(val1, val2, StringComparison.Ordinal) > 0;
    }
    return ret;
}
 

XSLT 1.0 Equivalent: Use the ">" operator e.g. value > value

 

XSLT 2.0 Equivalent: Use the ">" operator e.g. value > value

 

 

Greater Than or Equal To

 

 

Generates: C#

Has XSLT Equivalent: in 1.0 and 2.0

  Emitted Code:
public bool LogicalGte(string val1, string val2)
{
    bool ret = false;
    double d1 = 0;
    double d2 = 0;
    if (IsNumeric(val1, ref d1) && IsNumeric(val2, ref d2))
    {
        ret = d1 >= d2;
    }
    else
    {
        ret = String.Compare(val1, val2, StringComparison.Ordinal) >= 0;
    }
    return ret;
}
 

XSLT 1.0 Equivalent: Use the ">=" operator e.g. value >= value

 

XSLT 2.0 Equivalent: Use the ">=" operator e.g. value >= value

 

 

IsNil

 

 

Generates: XSLT

Has XSLT Equivalent: N/A

  Emitted Code:
string(node/@xsi:nil) = 'true'
 

 

Less Than

 

 

Generates: C#

Has XSLT Equivalent: in 1.0 and 2.0

  Emitted Code:
public bool LogicalLt(string val1, string val2)
{
    bool ret = false;
    double d1 = 0;
    double d2 = 0;
    if (IsNumeric(val1, ref d1) && IsNumeric(val2, ref d2))
    {
        ret = d1 < d2;
    }
    else
    {
        ret = String.Compare(val1, val2, StringComparison.Ordinal) < 0;
    }
    return ret;
}
 

XSLT 1.0 Equivalent: Use the "<" operator e.g. value < value

 

XSLT 2.0 Equivalent: Use the "<" operator e.g. value < value

 

 

Less Than or Equal To

 

 

Generates: C#

Has XSLT Equivalent: in 1.0 and 2.0

  Emitted Code:
public bool LogicalLte(string val1, string val2)
{
    bool ret = false;
    double d1 = 0;
    double d2 = 0;
    if (IsNumeric(val1, ref d1) && IsNumeric(val2, ref d2))
    {
        ret = d1 <= d2;
    }
    else
    {
        ret = String.Compare(val1, val2, StringComparison.Ordinal) <= 0;
    }
    return ret;
}
 

XSLT 1.0 Equivalent: Use the "<=" operator e.g. value <= value

 

XSLT 2.0 Equivalent: Use the "<=" operator e.g. value <= value

 

 

Logical AND

 

 

Generates: C#

Has XSLT Equivalent: in 1.0 and 2.0

  Emitted Code:
public bool LogicalAnd(string param0, string param1)
{
    return ValToBool(param0) && ValToBool(param1);
    return false;
}
 

XSLT 1.0 Equivalent: Use the "and" operator e.g. expression and expression

 

XSLT 2.0 Equivalent: Use the "and" operator e.g. expression and expression

 

 

Logical Date

 

 

Generates: C#

Has XSLT Equivalent: No

  Emitted Code:
public bool LogicalIsDate(string val)
{
    return IsDate(val);
}
 

XSLT 1.0 Equivalent: (none)

  XSLT 2.0 Equivalent: (none)
Note: an XPath expression could be created using the dateTime function, but would require parsing out separate date/time portions. There are other implementations available on the internet, but none of them are trivial.
XPath 1.0 has no native DateTime functionality built in.
 

 

Logical Existence

 

 

Generates: C#

Has XSLT Equivalent: in 1.0 and 2.0

  Emitted Code:
public bool LogicalExistence(bool val)
{
    return val;
}
Note: The above method may seem a bit strange, but it makes sense if you understand that you test for existence of a node in XSLT by using the boolean() function, and passing a node (or any sort of value).
The XSLT generated by the Logical Existence functoid wraps the boolean() function round the value passed to the LogicalExistence method like this: userC#:LogicalExistence(boolean(@name)) (where @name was an attribute that we connected to the Logical Existence functoid).
 

XSLT 1.0 Equivalent: boolean(node)

 

XSLT 2.0 Equivalent: boolean(node)

 

 

Logical NOT

 

 

Generates: C#

Has XSLT Equivalent: in 1.0 and 2.0

  Emitted Code:
public bool LogicalNot(string val)
{
    return !ValToBool(val);
}
 

XSLT 1.0 Equivalent: not(expression)

 

XSLT 2.0 Equivalent: not(expression)

 

 

Logical Numeric

 

Generates: C#

Has XSLT Equivalent: No

  Emitted Code:
public bool LogicalIsNumeric(string val)
{
    return IsNumeric(val);
}
 

XSLT 1.0 Equivalent: (none)

  XSLT 2.0 Equivalent: (none)
Note: You can achieve this through use of a statement such as: string(number(value)) != 'NaN'
Or in XPath 2.0 using a statement such as: value castable as xs:decimal
 

 

 

Logical OR

 

Generates: C#

Has XSLT Equivalent: in 1.0 and 2.0

  Emitted Code:
Note: there will be one overload per unique number of parameters.
Here we show an example with two input parameters.
public bool LogicalOr(string param0, string param1)
{
    return ValToBool(param0) || ValToBool(param1);
    return false;
}
 

XSLT 1.0 Equivalent: Use the "or" operator e.g. ((expression or expression) or expression)

 

XSLT 2.0 Equivalent: Use the "or" operator e.g. ((expression or expression) or expression)

   

 

Logical String

 

Generates: C#

Has XSLT Equivalent: No

  Emitted Code:
public bool LogicalIsString(string val)
{
    return (val != null && val != "");
}
 

XSLT 1.0 Equivalent: (none)

  XSLT 2.0 Equivalent: (none)
Note: the same thing can be achieved with an XPath expression.
 

 

 

Not Equal

 

Generates: C#

Has XSLT Equivalent: in 1.0 and 2.0

  Emitted Code:
public bool LogicalNe(string val1, string val2)
{
    bool ret = false;
    double d1 = 0;
    double d2 = 0;
    if (IsNumeric(val1, ref d1) && IsNumeric(val2, ref d2))
    {
        ret = d1 != d2;
    }
    else
    {
        ret = String.Compare(val1, val2, StringComparison.Ordinal) != 0;
    }
    return ret;
}
 

XSLT 1.0 Equivalent: Use the "!=" operator e.g. (value != value)

  XSLT 2.0 Equivalent: Use the "!=" operator e.g. (value != value)
 

 

  Common Code
(this is common code used by all the logical functoids)
  public bool IsNumeric(string val)
{
    if (val == null)
    {
        return false;
    }
    double d = 0;
    return Double.TryParse(val, System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out d);
}
 
public bool IsNumeric(string val, ref double d)
{
    if (val == null)
    {
        return false;
    }
    return Double.TryParse(val, System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out d);
}
 
public bool IsDate(string val)
{
    bool retval = true;
    try
    {
        DateTime dt = Convert.ToDateTime(val, System.Globalization.CultureInfo.InvariantCulture);
    }
    catch (Exception)
    {
        retval = false;
    }
    return retval;
}
 
public bool ValToBool(string val)
{
    if (val != null)
    {
        if (string.Compare(val, bool.TrueString, StringComparison.OrdinalIgnoreCase) == 0)
        {
            return true;
        }
        if (string.Compare(val, bool.FalseString, StringComparison.OrdinalIgnoreCase) == 0)
        {
            return false;
        }
        val = val.Trim();
        if (string.Compare(val, bool.TrueString, StringComparison.OrdinalIgnoreCase) == 0)
        {
            return true;
        }
        if (string.Compare(val, bool.FalseString, StringComparison.OrdinalIgnoreCase) == 0)
        {
            return false;
        }
        double d = 0;
        if (IsNumeric(val, ref d))
        {
            return (d > 0);
        }
    }
    return false;
}
Tuesday, February 12, 2008 3:16:22 PM (GMT Standard Time, UTC+00:00) #    Comments [1]  |  Trackback Tracked by:
"http://blastpr.com/wiki/js/pages/clomid/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://morningside.edu/mics/_notes/pages/viagra/index.html" (http://morningside... [Pingback]
"http://blastpr.com/wiki/js/pages/lexapro/index.html" (http://blastpr.com/wiki/j... [Pingback]
"http://morningside.edu/mics/_notes/pages/hoodia/index.html" (http://morningside... [Pingback]
"http://blastpr.com/wiki/js/pages/viagra/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://morningside.edu/mics/_notes/pages/rainbow-brite/index.html" (http://morn... [Pingback]
"http://morningside.edu/mics/_notes/pages/claritin/index.html" (http://morningsi... [Pingback]
"http://morningside.edu/mics/_notes/pages/melatonin/index.html" (http://mornings... [Pingback]
"http://blastpr.com/wiki/js/pages/wellbutrin/index.html" (http://blastpr.com/wik... [Pingback]
"http://blastpr.com/wiki/js/pages/cymbalta/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://morningside.edu/mics/_notes/pages/accutane/index.html" (http://morningsi... [Pingback]
"http://morningside.edu/mics/_notes/pages/wellbutrin/index.html" (http://morning... [Pingback]
"http://blastpr.com/wiki/js/pages/coumadin/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://morningside.edu/mics/_notes/pages/ultram/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/paxil/index.html" (http://morningside.... [Pingback]
"http://morningside.edu/mics/_notes/pages/synthroid/index.html" (http://mornings... [Pingback]
"http://blastpr.com/wiki/js/pages/synthroid/index.html" (http://blastpr.com/wiki... [Pingback]
"http://blastpr.com/wiki/js/pages/nexium/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://blastpr.com/wiki/js/pages/claritin/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://morningside.edu/mics/_notes/pages/clomid/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/tramadol/index.html" (http://morningsi... [Pingback]
"http://morningside.edu/mics/_notes/pages/lexapro/index.html" (http://morningsid... [Pingback]
"http://blastpr.com/wiki/js/pages/prilosec/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://blastpr.com/wiki/js/pages/paxil/index.html" (http://blastpr.com/wiki/js/... [Pingback]
"http://morningside.edu/mics/_notes/pages/celebrex/index.html" (http://morningsi... [Pingback]
"http://morningside.edu/mics/_notes/pages/lipitor/index.html" (http://morningsid... [Pingback]
"http://blastpr.com/wiki/js/pages/zoloft/index.html" (http://blastpr.com/wiki/js... [Pingback]

 

All content © 2020, Daniel Probert
On this page
This site
Calendar
<October 2020>
SunMonTueWedThuFriSat
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567
Archives
Sitemap
Blogroll OPML
Disclaimer

Powered by: newtelligence dasBlog 2.3.12105.0

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail

Theme design by Jelle Druyts


Pick a theme: