'Restrict a string to whitelisted characters using XSLT 1.0
Question
Using XSLT 1.0, given a string with arbitrary characters how can I get back a string that meets the following rules.
- First character must be one of these: a-z, A-Z, colon, or underscore
- All other characters must be any of those above or 0-9, period, or hyphen
- 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.
Solution 1:[1]
As far as Im aware XSLT 1.0 doesnt have a builtin for this. XSLT 2.0 allows you to use regexes, though Im sure your all too aware of that.
If, on the off chance your using the MS parser, you can write .NET extension libraries that you can leverage in your XSLT and I wrote about this some months ago here.
If your using something like Saxon, I am pretty certain they also provide ways of coding your own extensions, and they may indeed have an extension of their own already, but Im unfamiliar with that engine.
Hope this helps.
Solution 2:[2]
As another alternative there is a string function that might work for you in the XSLT standard library. http://xsltsl.sourceforge.net/string.html#template.str:string-match
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jim Burger |
| Solution 2 | Jim Burger |
