When I was in college, it was common to learn more than one programming language at a time, and it was just as common to accidentally switch languages mid-program.

Though this language switch never worked, students always had one of two valid reasons for it. One reason was that we knew how to accomplish parts of the task in one language but not the other. The other reason (which occurred more towards the end of the semester than at the beginning) was that the language just didn't support what we were trying to accomplish. So, in any given semester, you'd see a program that looked like it was out of John Carpenter's The Thing, frozen mid-morph between two very different programming languages.

Things have changed considerably since my college days. When I switch languages mid-program I mean to make the change, and the best part is now it works. Although I have to admit that the reason for the switch still falls into one of two reasons, though usually the second reason. I'll share a recent example of when I had to use a hybrid XSLT solution.

I needed to send an email; this would normally be a simple enough task, but the program also needed to do some heavy duty transformations, so it was written in MSXML XSLT, version 1. A lot of things can't be accomplished in straight XSLT, and this is one of them. Fortunately, there seems to be a rule that Microsoft can't blindly follow W3C recommendations without tweaking it. So, Microsoft added a few extensions, including one that allows a developer to add extension functions in JavaScript, VBScript, VB.NET, and C#. Since I know how to send an email in C#, the choice was made for me. The code below shows the C# function to send an email.

C# function to send an email
/*
Send an email.
*/
public bool mail(string server, string format, string sender, string recipient, string subject, string body)
{
  try
{
System.Net.Mail.SmtpClient scMail = new System.Net.Mail.SmtpClient(server);
System.Net.Mail.MailMessage mmMail = new System.Net.Mail.MailMessage(sender, recipient, subject, body);


mmMail.BodyEncoding = Encoding.UTF8; 
mmMail.IsBodyHtml = (format == "htm") ? true : false;
 
scMail.Send(mmMail);
 
return true;
}
catch (Exception e)
{
return false;
}
}

My biggest issue was finding the address of my SMTP server; once I found the address, all I had to do was wrap the C# in the appropriate XSLT, as shown in code below.

XSLT that sends an email
<?xml version="1.0″ encoding="UTF-8″?>
<xsl:stylesheet version="1.0″ xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:cs="urn:cs">
<msxsl:script language="CSharp" implements-prefix="cs"><![CDATA[
/*
Send an email.
*/
public bool mail(string server, string format, string sender, string recipient, string subject, string body)
{
try
{
System.Net.Mail.SmtpClient scMail = new System.Net.Mail.SmtpClient(server);
System.Net.Mail.MailMessage mmMail = new System.Net.Mail.MailMessage(sender, recipient, subject, body);

mmMail.BodyEncoding = Encoding.UTF8; 
mmMail.IsBodyHtml = (format == "htm") ? true : false;
 
scMail.Send(mmMail);
 
return true;
}
catch (Exception e)
{
return false;
}
}
]]></msxsl:script>
<xsl:template match="/">
<xsl:variable name="smtpServer">something.whatever.com</xsl:variable>
<xsl:variable name="format">text</xsl:variable>
<xsl:variable name="sender">sender@whatever.com</xsl:variable>
<xsl:variable name="recipient">recipient@whatever.com</xsl:variable>
<xsl:variable name="subject">Test</xsl:variable>
<xsl:variable name="body">Test message.</xsl:variable>
<xsl:element name="email">
<xsl:value-of select="cs:mail($smtpServer,$format,$sender,$recipient,$subject,$body)"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

I'm constantly amazed by how many of the things I learned in college still apply in development — even the screw-ups. Yes, it still looks like code frozen in mid-morph between one thing and another, but unlike college or (The Thing), I'm not going to lose any sleep over this solution.

Via TechRepublic

Cast your .NET This was published in Cast your .NET, check every Thursday for more stories

Comments

1

uggs outlet - 25/06/10

"It looks good,I have learn a recruit!
Recently,I found an excellent online store, the XX are completely various, good quality and cheap price,it’s worth buying!
<a href="http://www.air-jordan-4.com" >air jordan 4</a>

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

1

uggs outlet - 25/06/10

"It looks good,I have learn a recruit! Recently,I found an excellent online store, the XX are completely various, good quality and cheap ... more

Log in


Sign up | Forgot your password?

What's on?

  • Optus Deal

    Broadband + home phone + PlayStation®3 in a single package price!