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 February 23rd, 2006, 05:56 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 131
Thanks: 10
Thanked 0 Times in 0 Posts
Default string character conversion

Hi,

I need to eliminate ALL characters in a string (strExample) that are NOT 'Alpha' or 'Numeric' or '.' and return a single blank space for each occurence.

'2006/02/23' to be returned as:
'2006 02 23'

Is there an easy way using the translate function? Or am I thinking of the wrong method?

<xsl:variable name="AddrLine" select="translate(string($strExample),???? ,' ')"/>

i.e. If = a '(' character appears
do I use '!=chr(40)' or '!=#40;' ?

for all except either:
#48; - #57; (0-9)
#65; - #90; (A-Z)
#97; - #122; (a-z)

or
chr(48) - chr(57) (0-9)
chr(65) - chr(90) (A-Z)
chr(97) - chr(122)(a-z)

I wanted a better method than creating the following monster:
<xsl:variable name="AddrLine" select="translate(string($strExample),'!"£$%^&*() _=+@#?<>[]{}|`¬;:' ,' ')"/>

double quotes '"' causes a syntax problem in the above example.
I also realise that the characters '& < >' are '&amp; &lt; &gt;' respectively.



Also, which is = ' ' (SINGLE BLANK)

chr(160) or chr(173) ?


Neal

A Northern Soul
__________________
Neal

A Northern Soul
 
Old February 23rd, 2006, 06:28 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This is obviously a lot easier with the regular expression capabilities in XSLT 2.0. In 1.0, using translate(), there's a neat trick for *deleting* all characters outside a given set (say 0-9 for example):

translate($in, translate($in, '0123456789', ''), '')

which you can adapt by changing the last '' to a sufficiently-long string of spaces (and changing '0123...' to the set of characters you want to keep). Sufficiently-long means "at least as long as the number of different characters in $in", or if you prefer "at least as long as $in".

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 23rd, 2006, 08:23 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 131
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Excellent (though I had to read it through a couple of times).

Obviously for my purposes I have to increase the length of the last set of single quotes from '' to ' '.
Therefore:
<xsl:variable name="AddrLine" select="translate($strExample, translate($strExample, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm nopqrstuvwxyz', ''), ' ')"/>

MANY THANKS!


Neal

A Northern Soul





Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing a character from string itHighway Classic ASP Basics 5 March 14th, 2005 11:15 PM
IMP-00016: required character set conversion not saravananedu Oracle 1 September 7th, 2004 05:00 AM
character to range conversion for VLOOKUP gskoog Excel VBA 1 May 26th, 2004 05:10 AM
Wide Character String kochoo Classic ASP Components 0 October 12th, 2003 12:06 AM
How do I get rid of the last character in a string Lucy Classic ASP Professional 3 September 30th, 2003 05:33 AM





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