I mentioned in my last blog post that you can use scripts in XSLT, so I just wanted to show shortly how you can use scripts to show the power it can yield.
Firstly – add a namespace to your XSLT file:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:scripts="urn:scripts.this">
Then you can use the scripts in various situations.
For example, I had to parse some "Yes" and "No" into Boolean strings "true" and "false" (for business logic reasons) so I made a script for that in the bottom of my XSLT file (I made the scripts in C# syntax):
<msxsl:script language="C#" implements-prefix="scripts">
<![CDATA[
//return true or false as string instead of "yes" and "no"
public string getBooleanString(string s0)
{
if ((String.Equals(s0, "yes", StringComparison.OrdinalIgnoreCase))
{ return "true"; }
if (String.Equals(s0, "no", StringComparison.OrdinalIgnoreCase))
{ return "false"; }
return s0;
}
]]>
</msxsl:script>
Now, I could use this script in my XSLT matching like this:
<xsl:value-of select ="scripts:getBooleanString(./VALUE)"/>
This makes a call to the C# script “getBooleanString” with the value, the script will test it for “Yes” and “No” and return “true” or “false” respectively, or the string itself it it wasn’t one of the two.
Some of the other things I used this for was to compare strings case with case insensitive, and to hold temporary values to avoid having to run through my XSLT file multiple time in the transformation process. I also had to convert some number formats to use period as decimal point instead (due to language specific settings), so I made a script which changes between period and comma.
It is a powerful tool to have in the XSLT toolbox.