Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 16th, 2011, 01:24 PM
Authorized User
 
Join Date: Feb 2008
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
Default Extracting number from a string

Hello All,

I have some strings like
1."I have 10 apples.",
2."I have 5 oranges,4 strawberrys." etc

I need to get numbers from 1st string as 10 and from 2nd string 5 and 4 etc

in xslt 1.0 version

Help me out.

Thanks
__________________
tq98
 
Old November 16th, 2011, 02:05 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This kind of thing is MUCH easier in XSLT 2.0. But there's a trick you can use with the translate function:

Code:
translate($x, translate($x, '0123456789', ''), '')
removes all characters from $x that are not digits. (Study it carefully to understand why).

Of course this isn't the whole answer because it also loses the separators. So change it to replace the non-digits with a space, and then delete redundant spaces:

Code:
normalize-space(translate($x, translate($x, '0123456789', ''), ' '))
and then you can use the str:tokenize recursive template from the EXSLT site to split the sequence of numbers and process them individually.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old November 17th, 2011, 01:57 AM
Authorized User
 
Join Date: Feb 2008
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks for the response,

But I dont know how to use str:tokenize function.Could you please let me know that functionality for getting my final result.I think tokenize method cant work in version 1.0
__________________
tq98

Last edited by thaqi498; November 17th, 2011 at 02:03 AM..
 
Old November 17th, 2011, 03:58 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

See http://www.exslt.org/str/functions/tokenize/index.html.

Some processors provide str:tokenize() as a function you can call from XPath. If your processor doesn't, download the version that's written as a named template, and call it using xsl:call-template, supplying the string to be tokenized as the $string argument, and a single space as the $delimiters argument.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
thaqi498 (November 17th, 2011)
 
Old November 17th, 2011, 06:35 AM
Authorized User
 
Join Date: Feb 2008
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks mhkay,


Thank you so much. I got 100% result what I need.

Cheers
__________________
tq98





Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting a number range from tag attribute group maikm XSLT 6 August 25th, 2008 04:54 PM
string extracting fraijo SQL Server 2000 4 October 25th, 2006 09:53 PM
Extracting an Integer from a string gjc VB How-To 2 September 13th, 2005 03:50 PM
extracting last word of a string p_gauri7 Crystal Reports 1 January 3rd, 2005 09:08 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.