Monday, April 16, 2012

Break a continues sentence into words using the capital letters in xslt ...

Sometimes we need to break a chunk of sentence which are without spaces into the words like

Input : ManojJoshi
OutPut: Manoj Joshi

In XSLT

First of all we need to create a template which will call recursively and return words... so here is the template :


<!--template to break the String into words -->
<xsl:template name="Split">
<xsl:param name="Value"/>
<xsl:param name="First" select="false()"/>
<xsl:if test="$Value!=''">
<xsl:variable name="Space">*</xsl:variable>
<xsl:variable name="FirstChar" select="substring($Value, 1, 1)"/>
<xsl:variable name="Rest" select="substring-after($Value, $FirstChar)"/>
<xsl:if test="not($First)">
<xsl:if test="translate($FirstChar, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', '..........................')= '.'">
<xsl:value-of select="$Space"/>
</xsl:if>
</xsl:if>
<xsl:value-of select="$FirstChar"/>
<xsl:call-template name="Split">
<xsl:with-param name="Value" select="$Rest"/>
</xsl:call-template>
</xsl:if>
</xsl:template>

So that's all ... we need to call the template like below :
<xsl:call-template name="Split">
<xsl:with-param name="Value" select="'ManojJoshi'"/>
<xsl:with-param name="First" select="true()"/>
</xsl:call-template>

And we are done here ..

Insert default option to the drop downs using jQuery

There are some cases where we need to add dynamically a default value to the dropdowns. A simple code snippet which enters default option to all the dropdown inside a container :

//Used to insert default value for the dropdowns
function InsertFirstOptionToDropdowns(divId) {

//Find all the select and add span
$("#divContainer").find('select').each(function () {
$(this).prepend("<option value='' selected='selected'>Please Select</option>");
});

}

cheers