This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Converting a string to small-caps?
- From: "Brook Elgie" <brook dot elgie at mwr dot biz>
- To: <xsl-list at lists dot mulberrytech dot com>
- Date: Wed, 15 May 2002 17:27:13 +0100
- Subject: [xsl] Converting a string to small-caps?
- References: <scb6b125.015@deltafunding.com>
- Reply-to: xsl-list at lists dot mulberrytech dot com
The CSS font-variant: small-caps doesn't render as expected in most
browsers( ie. in IE 5.x it renders all the characters as upper-case, and
NN4.x doesn't format it at all). So I'm trying to write a bit of xsl that
does it better. Here's what I've come up with so far:
#### XSL ####
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="main">
<xsl:call-template name="cap">
<xsl:with-param name="data" select="convert_me"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="cap">
<xsl:param name="data"/>
<xsl:if test="$data">
<xsl:variable name="first_letter"
select="substring($data,1,1)"/>
<xsl:variable name="upperCase"
select="translate($first_letter,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMN
OPQRSTUVWXYZ')"/>
<xsl:choose>
<!-- IF THE LETTER IS IN UPPERCASE, LEAVE IT AS IT
IS. -->
<xsl:when test="$first_letter = $upperCase">
<xsl:value-of select="$upperCase"/>
</xsl:when>
<!-- OTHERWISE (IF THE LETTER IS IN LOWERCASE),
SHRINK IT A TOUCH-->
<xsl:otherwise>
<span style="font-size:smaller">
<xsl:value-of select="$upperCase"/>
</span>
</xsl:otherwise>
</xsl:choose>
<xsl:call-template name="cap">
<xsl:with-param name="data"
select="substring($data,2)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
#################
Which transforms this:
###### XML #######
<?xml version="1.0" encoding="UTF-8"?>
<main>
<convert_me>This is the First Sentence.</convert_me>
</main>
#################
as expected into this:
######## HTML #############
T<span style="font-size:smaller">H</span><span
style="font-size:smaller">I</span><span style="font-size:smaller">S</span>
<span style="font-size:smaller">I</span><span
style="font-size:smaller">S</span> <span
style="font-size:smaller">T</span><span
style="font-size:smaller">H</span><span style="font-size:smaller">E</span>
F<span style="font-size:smaller">I</span><span
style="font-size:smaller">R</span><span
style="font-size:smaller">S</span><span style="font-size:smaller">T</span>
S<span style="font-size:smaller">E</span><span
style="font-size:smaller">N</span><span
style="font-size:smaller">T</span><span
style="font-size:smaller">E</span><span
style="font-size:smaller">N</span><span
style="font-size:smaller">C</span><span style="font-size:smaller">E</span>.
#########################
Which renders exactly as expected - but it's hardly efficient to have a span
tag around every letter that I want small!
Is there another way? either tweaking my script or a completely different
way.
cheers
brew
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list