Do it online

Online programming editors/debuggers are getting popular. Here are the couple of them which I use from time to time:

  • IDEOne – a broad choice of programming languages (there is even BrainF**k, however, there is no XSLT)
  • jsFiddle – a nice IDE for JavaScript.
  • jsBin – another one for JavaScript
  • XSLTCake – useful IDE for XSLT. It is still ‘beta’, but it offers three types of XSLT processing (JavaScript, web service or .NET 4.0)
  • XML Playground – another cool IDE for XSL templates. Unfortunately it doesn’t support XSLT 2.0
  • Rubular – IDE for checking regular expression written in Ruby
  • XSLTTest – Small and neat application for XSLT. Supports XSLT 2.0.

Useful XSLT snippet with translate()

Let’s say there is a following function written in XSLT 2.0:

    <xsl:function name="str:convertBits">
        <xsl:param name="cpnNumber" />

        <xsl:choose>
            <xsl:when test="$cpnNumber = 1">
                <xsl:value-of>8</xsl:value-of>
            </xsl:when>
            <xsl:when test="$cpnNumber = 2">
                <xsl:value-of>4</xsl:value-of>
            </xsl:when>
            <xsl:when test="$cpnNumber = 3">
                <xsl:value-of>2</xsl:value-of>
            </xsl:when>
            <xsl:when test="$cpnNumber = 4">
               <xsl:value-of>1</xsl:value-of>
            </xsl:when>
            <xsl:otherwise>
               <xsl:value-of>0</xsl:value-of>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:function>

Quite long, isn’t it?

This is how it can be shortened:

number(translate(xs:string($cpnNumbers[1]), '1234567890', '8421000000'))

How to change XML to lower case using XSLT multi-pass?

Today I’ve resolved another issue using XSLT multi-pass technique presented in my previous post. I think this be might useful in future, so I post it here. The XSLT translates the input XML to lower case (it changes only node names, it doesn’t affect data inside elements or attributes). After that, it applies the actual logic.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="firstPassResult">
<xsl:apply-templates select="/" mode="firstPass"/>
</xsl:variable>
<xsl:template match="@*|node()" mode="firstPass">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*" mode="firstPass">
<xsl:element name="{translate(name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'abcdefghijklmnopqrstuvwxyz')}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="$firstPassResult" mode="secondPass"/>
</xsl:template>
<xsl:template match="//boss" mode="secondPass">
<xsl:value-of select="text()"/>
</xsl:template>
</xsl:stylesheet>

The sample XML file (only to prove that this technique works):

<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Boss>John</Boss>
<BoSs>Henry</BoSs>
<BOSS>Nathan</BOSS>
<boss>igor</boss>
</Root>

It worked under Altova XMLSpy and produced the following output:

<?xml version="1.0" encoding="UTF-8"?>JohnHenryNathanigor

VS .NET 2008 design mode issue

Today I came across a strange issue related to VS .NET 2008 design mode. I was coding a c# stand-alone application whose purpose was to connect to database using NHibernate library. I created a couple of controls which all inherit from DevExpress.XtraEditors.XtraUserControl (btw. I am using DexExpress in this project). The controls were using NHibernate DAOs in order to load data from database. However, when I was trying to edit them in Visual Studio designer I kept getting the following error:

vs_design_mode

Of course, as you may probably have guessed, I have not paid too much attention to catch NHibernate exceptions properly and that is why the exception in the picture above crashes VS designer. Nevertheless, something should be done to handle such issues properly. After a few minutes of google’ing I have found the solution. I put it here, simply because the useful information on the Internet is apt to dissapear quickly:

public class BaseDesignModeUserControl : UserControl
{
private static readonly bool isDesignMode;

static BaseDesignModeUserControl()
{
isDesignMode =
(System.Diagnostics.Process.GetCurrentProcess().
ProcessName.IndexOf("devenv")
!= -1);
}

protected bool IsDesignMode
{
[DebuggerStepThrough]
get
{
return isDesignMode;
}
}

}