Im trying to use XSL and Xpath functions to remove all the white space from a XML attribute called name and make it lower case. At the moment i have something like
<xsl:variable name="linkName">
<xsl:value-of select="normalize-space(name)"/>
</xsl:variable>
This removes the white space at the beginning and end but not the middle. Any suggestions?
What is the best practice for handling and altering XML data as it seems that you can use
<xsl:value-of select="x"/>
placed directly in the HTML
or
<xsl:attribute name="y">
<xsl:value-of select="x"/>
or use a
<xsl:variable name="x">
I’m not really sure of the differences or when each should be used. Any help is much appreciated.
Ally
As Tim Robinson points out,
translatewill do the trick. (I wouldn’t call it a “hack,” but then I’ve long been at the identifying-with-my-torturers stage of my relationship with XSLT.) Your code will be a lot more readable if you use something like this:…which is part of the
globals.xsltfile that I include at the top of most transforms I write. Then this:translates each upper-case letter into its lower-case equivalent, and each whitespace character into nothing.
Usually the reason you use
xsl:variableis to make code more readable (as in the above example), or to store intermediate results that can’t otherwise be effectively calculated. A fourth way of getting data into the output is one you didn’t mention, and that’s pretty darned useful: the attribute value template. All of these do the same thing:In this particular case, it’s arguable which of the last two is simpler and clearer. Most of the time, though, it’s not: separating the calculation of values from how they get inserted into the output makes both easier to understand, as does using an AVT instead of more verbose XSLT constructs that do the same thing.