Surprisingly, neither XSLT v1.0 nor XSLT v2.0 have any built-in conversion support (well, not for the scenarios represented in this category anyway).
It is possible to download XSLT libraries which can do this sort of conversion (as mentioned in the notes below each functoid), but the XSLT is not pretty, and I'm not convinced about performance.
So C# is generally your only option here.
For each functoid I've shown:
- Whether XSLT or C# is emitted
- Whether an XSLT equivalent exists
- The XSLT or C# emitted by the functoid
- Where C# is emitted, the equivalent XSLT to achieve the same functionality (in both XSLT v1.0 and v2.0)
Functoids covered in this category:
|
Conversion Functoids |
|
|
ASCII to Character |
|
|
|
Generates: C# |
Has XSLT Equivalent: No |
|
|
Emitted Code: public string ConvertChr(string val) { string retval = ""; double d = 0; if (IsNumeric(val, ref d)) { int v = (int)d; if (v >= 1 && v <= 127) { char c = (char)v; retval = c.ToString(System.Globalization.CultureInfo.InvariantCulture); } } return retval; }
|
|
|
XSLT 1.0 Equivalent: (none) |
|
|
XSLT 2.0 Equivalent: (none) Note: There are some inelegant hacks you can use (e.g. listing all the ASCII chars in a variable and selecting by index, or using the translate() function with a list of possible values) but there is no built-in support for this conversion. |
|
|
|
|
Character to ASCII |
|
|
|
Generates: C# |
Has XSLT Equivalent: No |
|
|
Emitted Code: public string ConvertAsc(string val) { if (val == null || val == "") { return ""; } else { char c = val[0]; int x = c; return x.ToString(System.Globalization.CultureInfo.InvariantCulture); } }
|
|
|
XSLT 1.0 Equivalent: (none) |
|
|
XSLT 2.0 Equivalent: (none) Note: There are some inelegant hacks you can use (e.g. listing all the ASCII chars in a variable and selecting by index, or using the translate() function with a list of possible values) but there is no built-in support for this conversion. |
|
|
|
|
Hexadecimal |
|
|
|
Generates: C# |
Has XSLT Equivalent: No |
|
|
Emitted Code: public string ConvertHex(string val) { string retval = ""; double d = 0; if (IsNumeric(val, ref d)) { int v = (int)d; retval = Convert.ToString(v, 16).ToUpper(System.Globalization.CultureInfo.InvariantCulture); } return retval; }
|
|
|
XSLT 1.0 Equivalent: (none) |
|
|
XSLT 2.0 Equivalent: (none) Note: There are some inelegant templates you can use to perform this conversion, but they are all quite long and involve string manipulation. |
|
|
|
|
Octal |
|
|
|
Generates: C# |
Has XSLT Equivalent: No |
|
|
Emitted Code: public string ConvertOct(string val) { string retval = ""; double d = 0; if (IsNumeric(val, ref d)) { int v = (int)d; retval = Convert.ToString(v, 8); } return retval; }
|
|
|
XSLT 1.0 Equivalent: (none) |
|
|
XSLT 2.0 Equivalent: (none) Note: There are some inelegant templates you can use to perform this conversion, but they are all quite long and involve string manipulation. |
|
|
|
|
|
Common Code (this is common code used by all the conversion functoids) |
|
|
public bool IsNumeric(string val) { if (val == null) { return false; } double d = 0; return Double.TryParse(val, System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out d); } public bool IsNumeric(string val, ref double d) { if (val == null) { return false; } return Double.TryParse(val, System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out d); }
|