Presents your
XML E-NEWSLETTER for October 9, 2002
<------------------------------------------->
CONVERT TEXT CASE WITH XSL
A common problem when working with XML data is managing the proper
casing of the text. This can be a particular nuisance when integrating
legacy systems that expect data in all caps. Here's a solution to manage XML
casing problems that uses a simple XSL template.
THE SAMPLE PROBLEM
Imagine you have a set of data that needs to be sent to a legacy
system. The legacy programmers have been nice enough to accept data in XML
format; however, they need you to provide all the data, including the
XML in uppercase. The data that you currently have looks like LISTING 1.
Listing 1: person.xml
Brian
Schaffner
The legacy programmers (and their application) want you to provide this
same data in a little different format. They need it to look like LISTING
2.
Listing 2: newperson.xml
BRIAN
SCHAFFNER
XSL TRANSLATION
The solution to this problem begins with the XPath function called
translate. This function will translate a string using two character
sets. The first character set is used to identify which characters to replace;
the second identifies the replacement characters. Here's a short example:
This will result in the following output:
ThIS IS a TeST
The translate function has replaced each 't' with 'T', each 'i' with
'I', and each 's' with 'S'.
It's possible to create simple cryptograms using the translate function
as well.
CASE CONVERSION
The process of converting case is simply a matter of translating all of
the uppercase letters to lowercase or vice versa. To facilitate this
process, we'll use two XSL variables to store the uppercase and
lowercase character sets:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
We can use these variables in our calls to the translate function. Now,
to convert an uppercase string to lowercase, all you need to do is call
the translate function, pass in the original string, and the uppercase
and lowercase variables. We modify our previous example to test out our
conversion:
When processed, the result of this function will be this:
THIS IS A TEST
BACK TO OUR PROBLEM
We should now have enough information to solve our sample problem. We
really have two parts to the problem. One is converting the data to
uppercase; the other is converting our XML tags to uppercase. We'll
solve the
first problem using the translate function we just learned about. We'll
solve the second by recoding the tags as uppercase within the XSL
template.
LISTING 3 shows what the complete XSL solution looks like.
Listing 3: person.xsl
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
JUST FOR FUN
Now that we've solved the problem, here's something fun to try. You can
use the translate function to create simple cryptograms. The process is
quite simple and uses the same function as above. The difference is in
the variables. Instead of uppercase and lowercase, you use an alphanumeric
set and an encoded set, like this:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz0123456789
K1aWXJBb2cRdeAMfQgL8yhij976klE
FzmYSnIo4DpTZq5rNsUtC0uOPvVwGxH3
Then when you want to encode data, you call the translate function like
this:
The result should look encrypted, like this:
8Snr6nr6k6NzrN
Keep in mind that this is the simplest encryption and is only for fun.
You should not employ this method for encrypting any confidential or
otherwise private data.
Brian Schaffner is a senior consultant for Fujitsu Consulting. He
provides architecture, design, and development support for Fujitsu's
Telcom360 group.
----------------------------------------