We help IT Professionals succeed at work.

XSL: Replace underscore with a space?

greddin asked
Medium Priority
Last Modified: 2012-06-22
In an xsl stylesheet how would I go about replacing the underscores "_" with a space in the Title nodes of my xml file below?

<?xml version="1.0" encoding="utf-8" ?>
<Folder xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Description />
      <Description />
      <Folders />
      <Description />
      <Folders />

Thank you.
Watch Question

use the translate function whenever you call it's value,
eg. <xsl:value-of select="translate(Title,'_',' ')"/>


Thanks, I have this but it's not working:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ccs="CancerConsultants.Syndication" exclude-result-prefixes="ccs">
     <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
     <xsl:variable name="FolderCountHalf" select="(count(/ccs:Folder/ccs:Folders/ccs:Folder) + 1) div 2"/>
     <xsl:template match="/ccs:Folder">
                    <table border="1">
                         <xsl:for-each select="ccs:Folders/ccs:Folder[position() &lt;= $FolderCountHalf]">
                                      <td><xsl:apply-templates select="."/><br/>
                                      <xsl:value-of select="translate(Title,'_',' ')"/>
                                                      <xsl:when test="following-sibling::ccs:Folder[position() = $FolderCountHalf]">
                                                            <xsl:apply-templates select="following-sibling::ccs:Folder[position() = $FolderCountHalf]"/>
     <xsl:template match="ccs:Folder">
          <a href="{ccs:FolderID}"><xsl:value-of select="ccs:Title"/></a>
Forgot, as your xml in this post didn't have the xmlns attribute, so you wouldn't get the Title because it doesn't match.

You only need to change the output template:

<xsl:template match="ccs:Folder">
  <a href="{ccs:FolderID}"><xsl:value-of select="translate(ccs:Title,'_', ' ')"/></a>

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Thank you so much again.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.