Extract String Between Two Commas using XSLT.

Hi All,

today we will learn how can be extract any value between two commas using xslt 1.0 version.

there are many ways to achieve this scenario. following are some:

1. you can use string functions defined in xslt. for this scenario we will use two function : substring-before and substring-after.

substring-before(string,pattern): returns string before defined pattern. for eg: 
substring-before('my,name,is,ankit', ',') : Returns 'my' as it comes before defined pattern.

substring-after(string, pattern): returns string after defined pattern. for eg: 
substring-after('my,name,is,ankit', ',') : returns 'name,is,ankit'. as it comes after defined pattern.

so now, if we want only 'name' from this string then function will be:

substring-before((substring-after('my,name,is,ankit',','),',') will return name from the given string.

2. if you don't want to use substring function then you can use tokenize function defined in xslt.

tokenize(string,pattern): splits a string based on regular expression.

for eg:
tokenize('my,name,is,ankit',',')  : returns ('my','name','is','ankit').

now if you want only 'name' from the result output then you will need to give position(indexing starts from 1) : 

tokenize('my,name,is,ankit',',')[2] : will return only 'name' from the given string.

There might be many ways to achieve this scenario. I will also update this post if i find more. if you have any feel free to write into comment or share your thoughts on same.

Ankit Gupta  

Comments

Popular Posts