6

I need to be able to replace certain characters so that I can use them as CSS classes.

I have strings like, class(name), class&name, amonst others which are not valid CSS classes (As far as i can tell).

How can I use the replace function to replace multiple chracters,

E.g.

translate(className, ' ','') (would replace a space)

But is it possible to doo this for multiple characters?

Translate doesnt seem to work with &

Example

XML

<title>Mary & the Wolf<title>

XSLT

<xsl:value-of select="translate(title, ' &','')"/></xsl:attribute>

So I want the output to be:

MarytheWolf

But at the moment I get an error with the & character.

2 Answers 2

7

translate() works character-wise:

translate(className, ' &#?!','')  // would remove any character in the string #1

or

translate(className, ' &#?!','_____')  // would replace any character 
                                       // in the string #1 with '_'
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, seems like a good idea to replace with a char rather than nothing at all.
Whats the & code for XSLT? ive tried & in the translate but throws an error?
&amp; is the XML escape sequence for the ampersand. It's not that XSLT or XPath require it, but XML itself does.
For some reason this doesnt work: translate(title, ' &amp;():/\','')" The &amp is causing a problem?
Unless you post the complete line of code you try it with, I won't be able to tell what's wrong. ;-)
|
1

You're most of the way there:

translate('abcd', 'cbda', 'CBDA')

would give 'ABCD'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.