1

I want to be able to translate and XML item to remove spaces & any special character and transform upper case characters to lower case and I'm getting a little stuck to say the least!

Ideally I'd like a way to catch all of the special characters to rip them out, rather than specifying them individually. I've read some answers on whitelisting but not sure how I can achieve it here. The other thing to point out is that I can only use XSLT 1.0.

Thanks in advance! :)

Here is what I have so far:

<xsl:template match="faq">
<article>
    <xsl:call-template name="questionMatch"/>
</article>
</xsl:template>
<xsl:template name="questionMatch" match="section">
    <xsl:for-each select="section">
        <xsl:for-each select="qa">
            <div class="toggler" id="{translate(translate(translate(question,'?!£$%^*',''), ' ', ''), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')}">
            </div>
        </xsl:for-each>
    </xsl:for-each>
</xsl:template> 
Barlow1984
  • 205
  • 1
  • 3
  • 14
  • 1
    I am not an XSL expert but found a link which might help you: http://stackoverflow.com/questions/5084065/replace-special-characters-in-xslt – Tirthankar Dec 13 '12 at 10:44

1 Answers1

6

Aha, eureka! I appear to have figured it out with the help of some other topics (eventually). I thought I would share the answer as it may help someone else in the future?

The answer was:

<xsl:template name="questionMatch" match="section">
    <xsl:variable name="vAllowedSymbols" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'"/>
        <xsl:for-each select="qa">
            <div class="toggler" id="{translate(translate(question, translate(question, $vAllowedSymbols, ''), ''), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')}">
            </div>
    </xsl:for-each>
</xsl:template> 
Barlow1984
  • 205
  • 1
  • 3
  • 14
  • 7
    This is indeed a useful trick to know. `translate(question, $vAllowedSymbols, '')` removes all the "allowed" symbols from the value of `question`, leaving just the "not-allowed" ones. Thus the double-translate `translate(question, translate(question, $vAllowedSymbols, ''), '')` takes the value of `question` and removes all the _not_-allowed characters from it, leaving just the allowed ones. – Ian Roberts Dec 13 '12 at 11:23