Jim Maivald
Jim Maivald

Reputation: 520

Replacing spaces with underscores using XSLT

In my XSLT I am folding two data elements together to create a filename to import an image. The following template concatenates the elements and converts any upper case letters to lowercase using a variable. But some Surname and FirstName elements have spaces in them that we need to replace with "_" (underscores). How do I add that capability to this function?

Here's the template (I included the variable for reference):

...<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />...

<xsl:template match="Consultant">
<consultant>
<consultantphoto>
<xsl:attribute name="href">
<xsl:text>file://images/</xsl:text>
<xsl:value-of select="translate(Surname, $uppercase, $smallcase)"/>
<xsl:text>_</xsl:text>
<xsl:value-of select="translate(FirstName, $uppercase, $smallcase)"/>    
<xsl:text>.jpg</xsl:text></xsl:attribute>
</consultantphoto>
</xsl:text>
</consultant>
</xsl:template>

This template results in an XML image reference like: <consultant href="file://surname_firstName" />

Upvotes: 0

Views: 3739

Answers (1)

Linga Murthy C S
Linga Murthy C S

Reputation: 5432

You can add another character space in uppercase and underscore("_") in smallcase:

<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz_'" />
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ '" />

Upvotes: 1

Related Questions