Wednesday, June 26, 2013

Separate Numbers and Characters from string using XSL

This can be achieved using Xpath function translate in XSL. 


Let us first understand how this function works, then we will use it for our requirement.

Xpath function : string translate (string, string, string)

The translate function accepts 3 arguments and returns the first argument string with occurrences of characters in the second argument string replaced by the character at the corresponding position in the third argument string. 
Lets understand it by an example, translate("bar","abc","ABC") returns the string BAr. If there is a character in the second argument string with no character at a corresponding position in the third argument string (because the second argument string is longer than the third argument string), then occurrences of that character in the first argument string are removed. For example, translate("--aaa--","abc-","ABC") returns "AAA". If a character occurs more than once in the second argument string, then the first occurrence determines the replacement character. If the third argument string is longer than the second argument string, then excess characters are ignored.

Solution to our problem - 
  • For fetching numbers use -
    Above statement will return '123123'

  • For fetching characters use -
    Above statement will return 'qweasd'

Please leave your valuable suggestions in the comments section. Enjoy.  :)

Monday, February 11, 2013

How to get last day / date of a month using XSL

Below are the steps used for achieving it. I have used Xsl variables so that it becomes easier to understand.

  • Create a month and year variable.
  • Add Choose xsl node where we will have one when condition (to check whether month is Feb and year is leap year or not) and in otherwise we will simply get the date. Below is the Xsl source code -