Subject: Re: Converting a string to Uppercase or Lowercase without using translate() ?
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Mon, 26 Nov 2007 15:49:26 -0800
|
> If by "nicer" you'll accept "more compact", this is what I direct my
> students to include at the top of their stylesheets:
>
> <!DOCTYPE xsl:stylesheet [
> <!ENTITY lower 'abcdefghijklmnopqrstuvwxyz'>
> <!ENTITY upper 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'>
> ]>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> version="1.0">
>
>
> Then, in your stylesheet you would have:
>
> <xsl:variable name="pClubCaps"
> select="translate($pClub,'&lower;','&upper;')"/>
>
> But as for functionality, translate() is all we have to work with in XSLT 1.0.
A good advice, Ken.
Just for fun, here is a "pure functional one-liner" implementation of
lower-case() using FXSL for XSLT 2.0: :o)
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:f="http://fxsl.sf.net/"
exclude-result-prefixes="f xs"
>
<xsl:import href="../f/func-map.xsl"/>
<xsl:import href="../f/func-zipWith.xsl"/>
<xsl:import href="../f/func-Operators.xsl"/>
<xsl:import href="../f/func-standardXpathFunctions.xsl"/>
<!--
Expected result: "houston, we have a problem!"
-->
<xsl:strip-space elements="*"/>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vcptA" select="string-to-codepoints('A')"/>
<xsl:variable name="vcptZ" select="string-to-codepoints('Z')"/>
<xsl:variable name="vcpta" select="string-to-codepoints('a')"/>
<xsl:variable name="vcptz" select="string-to-codepoints('z')"/>
<xsl:variable name="vtheCodepts"
select="string-to-codepoints('Houston, We have a problem!')"/>
<xsl:variable name="vOffset" select="$vcpta - $vcptA"/>
<xsl:template match="/">
<xsl:value-of select=
"codepoints-to-string(
f:map(f:integer(),
f:zipWith(f:add(),
$vtheCodepts,
f:map(f:mult($vOffset),
f:map(f:number(),
f:zipWith(f:and(),
f:map(f:le($vcptA), $vtheCodepts),
f:map(f:gt($vcpta), $vtheCodepts)
)
)
)
)
)
)
"/>
</xsl:template>
</xsl:stylesheet>
--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
|