Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :
09/30/2000
Times viewed :
2462
XSLT Case Conversion Solution
The following article was written by Erich
Von Hauske N. after a discussion on XSLTalk. Corey
Haines started a tread on how to do recursion in XSLT, which lead to this
solution that Erich and Corey came up with.
The solution of a problem like case conversion can take many forms,
especially when you use a language like XSLT, which gives you the flexibility
to give many solutions; each one with a different degree of
recursiveness. The solution presented in this document is a middle-ground
solution, it's recursive, but not as recursive as it could get, nevertheless it
has a better performance than the completely recursive solution.
What we are demonstrating is firstly how to convert your XML data from on
case to another. As XML is case sensitive, when you need to do searches on
your data, using the translate function demonstrated enables
case-insensitive searches. Then we are demonstrating a solution for
implementing proper case converting - which demonstrates the recursion.
Upper Case Transformation
Upper and lower case transformations are very easy in XSLT thanks to the translate function. An
alternative solution could be to implement the transformation and switch character by character, but since XSLT already
gives us a better solution, let's better take advantage of it. Please find
the full source in the downloadable file.
Firstly lets place all the letters of the alphabet, lower case and upper case
in variables.
The interesting part of this exercise is really in here: the proper case transformation. The presented solution first translates the entire string to
lower case, and after that it translate only the first letter of
every word to upper case. Here's the code:
Basically what it does is to check if the string to
covert has characters (end condition for recursion), and if it has characters
then it breaks the string in two parts: the first letter and the rest of the
string, converts the first letter to upper case and checks if the rest of the
string contains spaces (word separators), if it contains spaces then it outputs
the rest of the current word (substring-before($s," ")) and calls the
convertpropercase template with the rest of the string ('substring-after($s,"
")') to convert the rest of the words to proper case.
Procedural Programming in XSLT
If you were wondering what the xsl:call-template element does, it allows one to use functions in XSLT, where basically you can call
a template from anywhere in the XSLT. You pass the template parameters,
like you do in functions. Instead of the template having a matching
element, you give it a name. (<xsl:template name='convertpropercase'>)