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:when test="$cpnNumber = 1">
            <xsl:when test="$cpnNumber = 2">
            <xsl:when test="$cpnNumber = 3">
            <xsl:when test="$cpnNumber = 4">

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="">
<xsl:variable name="firstPassResult">
<xsl:apply-templates select="/" mode="firstPass"/>
<xsl:template match="@*|node()" mode="firstPass">
<xsl:apply-templates select="@*|node()"/>
<xsl:template match="*" mode="firstPass">
<xsl:element name="{translate(name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
<xsl:apply-templates select="@*|node()"/>
<xsl:template match="/">
<xsl:apply-templates select="$firstPassResult" mode="secondPass"/>
<xsl:template match="//boss" mode="secondPass">
<xsl:value-of select="text()"/>

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

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

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:


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 =
!= -1);

protected bool IsDesignMode
return isDesignMode;