Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 56697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:34:30+00:00 2026-05-10T17:34:30+00:00

Question Using XSLT 1.0, given a string with arbitrary characters how can I get

  • 0

Question

Using XSLT 1.0, given a string with arbitrary characters how can I get back a string that meets the following rules.

  1. First character must be one of these: a-z, A-Z, colon, or underscore
  2. All other characters must be any of those above or 0-9, period, or hyphen
  3. If any character does not meet the above rules, replace it with an underscore

Background

In an XSLT I’m translating some attributes into elements, but I need to be sure the attribute doesn’t contain any values that can’t be used in an element name. I don’t care much about the integrity of the attribute being converted to the name as long as it’s being converted predictably. I also don’t need to compensate for every valid character in an element name (there’s a bunch).

The problem I was having was with the attributes having spaces coming in, which the translate function can easily convert to underscores:

translate(@name,' ','_') 

But soon after I found some of the attributes using slashes, so I have to add that now too. This will quickly get out of hand. I want to be able to define a whitelist of allowed characters, and replace any non-allowed characters with an underscore, but translate works as by replacing from a blacklist.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:34:31+00:00Added an answer on May 10, 2026 at 5:34 pm

    You could write a recursive template to do this, working through the characters in the string one by one, testing them and changing them if necessary. Something like:

    <xsl:template name="normalizeName">   <xsl:param name="name" />   <xsl:param name="isFirst" select="true()" />   <xsl:if test="$name != ''">     <xsl:variable name="first" select="substring($name, 1, 1)" />     <xsl:variable name="rest" select="substring($name, 2)" />     <xsl:choose>       <xsl:when test="contains('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ:_', $first) or                       (not($first) and contains('0123456789.-', $first))">         <xsl:value-of select="$first" />       </xsl:when>       <xsl:otherwise>         <xsl:text>_</xsl:text>       </xsl:otherwise>     </xsl:choose>     <xsl:call-template name="normalizeName">       <xsl:with-param name="name" select="$rest" />       <xsl:with-param name="isFirst" select="false()" />     </xsl:call-template>   </xsl:if> </xsl:template> 

    However, there is shorter way of doing this if you’re prepared for some hackery. First declare some variables:

    <xsl:variable name="underscores"   select="'_______________________________________________________'" /> <xsl:variable name="initialNameChars"   select="'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ:_'" /> <xsl:variable name="nameChars"   select="concat($initialNameChars, '0123456789.-')" /> 

    Now the technique is to take the name and identify the characters that aren’t legal by replacing all the characters in the name that are legal with nothing. You can do this with the translate() function. Once you’ve got the set of illegal characters that appear in the string, you can replace them with underscores using the translate() function again. Here’s the template:

    <xsl:template name="normalizeName">   <xsl:param name="name" />   <xsl:variable name="first" select="substring($name, 1, 1)" />   <xsl:variable name="rest" select="substring($name, 2)" />   <xsl:variable name="illegalFirst"     select="translate($first, $initialNameChars, '')" />   <xsl:variable name="illegalRest"     select="translate($rest, $nameChars, '')" />   <xsl:value-of select="concat(translate($first, $illegalFirst, $underscores),                                translate($rest, $illegalRest, $underscores))" /> </xsl:template> 

    The only thing you have to watch out for is that the string of underscores needs to be long enough to cover all the illegal characters that might appear within a single name. Making it the same length as the longest name you’re likely to encounter will do the trick (though probably you could get away with it being a lot shorter).

    UPDATE:

    I wanted to add to this answer. In order to generate required length underscore string you can use this template.

    <!--Generate string with given number of replacement--> <xsl:template name="gen-replacement"> <xsl:param name="n"/>     <xsl:if test="$n > 0">         <xsl:call-template name="gen-replacement">             <xsl:with-param name="n" select="$n - 1"/>         </xsl:call-template>         <xsl:text>_</xsl:text>     </xsl:if> </xsl:template> 

    And call it when you need to generate underscores:

    <xsl:variable name="replacement"><xsl:call-template name="gen-replacement"><xsl:with-param name="n" select="string-length($value)"/></xsl:call-template></xsl:variable> 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 87k
  • Answers 87k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You have the foreign keys reversed. In ActiveRecord's conventions, the… May 11, 2026 at 5:28 pm
  • Editorial Team
    Editorial Team added an answer Yes, you are correct. The reason for this is that… May 11, 2026 at 5:28 pm
  • Editorial Team
    Editorial Team added an answer The advantage is that users get exactly what they want… May 11, 2026 at 5:28 pm

Related Questions

Is there a way to convert a HTML string into a Image .tiff file?
I've written some XSLT that uses one XML document to filter another. Now I'd
I've been working on database-driven web applications for a few years now and recently
I am copying an example from XSLT Cookbook: 2nd Edition (O'Reilly: Mangano, 2006) where

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.