The DevCenter has moved!

The Developer Center has a new home, developer.ektron.com. The site you are currently viewing is available as a read-only archive.
Things to note:
  1. Your username & password is the same on the new site, developer.ektron.com
  2. Going forward, please use the new URL, developer.ektron.com
  3. The old discussion forums and old KB articles are still available as an archive (read-only)

Ektron Knowledge Base

The Knowledge Base has thousands of articles describing open and past issues and providing answers to frequently asked questions.

Hover the mouse over to the side bar to open a side menu and expend a node for a list of articles.

INFO:XSLT: Using JavaScript in XSLT

Applies To:
eWebEditPro+XML
Summary:

The Microsoft XSLT services (and some other manufacturers too) allow JavaScript (and other script languages) to be run from within an XSL transform. This can be necessary for certain functions. The string processing in XPath functions are limited and having access to the JavaScript string functions can be a project saver. With this extension the full power of JavaScript regular expressions and their ability to intelligently replace strings is available in XPath expressions. This could be very useful when handling attribute values such as dates and phone numbers.

WARNING: The Microsoft .NET XslCompiledTransform class, which is used in Ektron CMS400, causes several problems when  msxsl:script is used. The XslCompiledTransform creates a DLL in the temporary folder and locks it in memory. 

  1. The ASP.NET process may not have access to the temporary folder and the XSLT will fail. 
  2. The temporary DLL is not temporary and remains in the folder: one DLL for every transform that uses msxsl:script. Eventually the server's hard drive is filled to capacity. 
  3. The DLL is locked in memory, causing a severe memory leak. 

DO NOT USE msxsl:script WITH Microsoft .NET XslCompiledTransform class. Instead, use an extension object to call C# or VB.NET methods  outside the XSLT. See Microsoft's documentation on   XSLT Extension Objects .  

This article describes how to call JavaScript from within an XSLT using the Microsoft extensions. These extensions are available in the MSXML 4.0, which is the version installed with eWebEditPro+XML. As of eWebEditPro 5.1, MSXML 4.0 is no longer installed. MSXML 6.0 is used instead.

JavaScript can be used in XSLT by following these steps:

  • Declare the "msxsl" namespace asshown.

xmlns:msxsl="urn:schemas-microsoft-com:xslt" 

  • Declare a namespace for your script, for example,

xmlns:js="urn:custom-javascript" 

  • (Optional) Omit the prefixes from the output, for example,

exclude-result-prefixes="msxsl js" 

  • Write the JavaScript in the msxsl:script element, for example,

<msxsl:script language="JavaScript" implements-prefix="js">
<![CDATA[
:
>
</msxsl:script>

  • Call your JavaScript function in an XPath expression, for example,

<xsl:value-of select="js:escapeUnicode(string(.))"/> 

Example

This example converts non-ASCII characters to escaped JavaScript codes that are ASCII and UTF-8 compatible. For example, a Unicode char U12488 will be changed to "\u30c8".

The sections critical to using JavaScript are in bold. Note that the prefix, "js" and the namespace "urn:custom-javascript" are arbitrary, but it is helpful to have meaningful values.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"  
xmlns:js="urn:custom-javascript"  
exclude-result-prefixes="msxsl js" >

<xsl:template match="text()">
<!-- escapes unicode chars to be safe for JavaScript -->
<xsl:value-of select="js:escapeUnicode(string(.))"/>
</xsl:template> 

<msxsl:script language="JavaScript" implements-prefix="js">
<![CDATA[
function escapeUnicode(strText)
// Escape strText to be ASCII, safe for native encoding or UTF-8.
{
var strConverted = "";
var lenText = strText.length;
var numCharCode = new Number(0);
for (var i = 0; i < lenText; i++)
{
numCharCode = strText.charCodeAt(i);
if (numCharCode <= 0x7f)
{
// ASCII, no conversion
strConverted += strText.charAt(i);
}
else if (numCharCode <= 0xff)
{
// single byte Latin1
strConverted += "\\x" + toHexString(numCharCode, 2);
}
else
{
// double byte Unicode
strConverted += "\\u" + toHexString(numCharCode, 4);
}
}
return strConverted;
} 

// private
function toHexString(numValue, minLength)
// Convert numValue (Number object) to a hexidecimal representation string of
// minimum length (minLength) padded with leading zeros as necessary.
{
if (typeof numValue != "number")
{
return "Bad numValue, type: "+ typeof numValue;
}
if (typeof minLength != "number")
{
return "Bad minLength, type: " + typeof minLength;
}
var strValue = numValue.toString(16); // convert to hex string
// Ensure min len
while (strValue.length < minLength)
{
strValue = "0" + strValue;
}
return strValue;
}
>
</msxsl:script>
 

<!-- ... more templates go here ... -->

</xsl:stylesheet>

More Resources:

XSLT Stylesheet Scripting using msxsl:script 

Tip: Call JavaScript from an XSLT style sheet