<msxsl:script> Element

Contains script blocks, so that custom functions can be used in an XSLT transformation. This is a top-level element.

<msxsl:script
  language = "language-name"
  implements-prefix = "prefix of user's namespace">
</msxsl:script>

Attributes

  • language
    The Active Scripting language used for the functions defined within this element. If left unspecified, Microsoft® JScript® (compatible with ECMA 262 language specification) is used. This attribute accepts the same values as the language attribute on the HTML <SCRIPT> element.

    For managed code, this attribute accepts the language names that are accepted by CreateProvider.

  • implements-prefix
    Required. Associates the prefix with the script block. The value of this attribute is the prefix that represents the namespace.

Element Information

Number of occurrences

Unlimited

Parent elements

xsl:stylesheet, xsl:transform

Child elements

<msxsl:assembly> Element

<msxsl:using> Element

Remarks

The <msxsl:script> element belongs to the namespace urn:schemas-microsoft-com:xslt. You can declare variables and define functions within this element. This element can appear within the <xsl:stylesheet> element. A script block thus nested is treated as a global script block.

The <msxsl:assembly> and <msxsl:using> child elements are only available for managed code within the .NET Framework. When used, these elements must be at the beginning of the script block, before any script code.

In unmanaged code, you can also instantiate COM objects in the <msxsl:script> element. However, a user's security settings might prevent your script from instantiating a client-side object.

For high performance, avoid script blocks from XSLT files, because they require loading the script engine multiple times. Instead, create a COM object that is equivalent to the script, producing a DLL. Then use addObject and pass the DLL.

Example

This example illustrates how to use <msxsl:script> to define a script block with a namespace prefix of user to declare a function called xml(). The xml() function takes a node-list as an argument. Notice how this function, xml(nodelist) in the user namespace, is called from the select attribute of <xsl:value-of>.

XML File (customers.xml)

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="script.xsl" ?>
<customers>
   <customer>
      <name>John Smith</name>
      <address>123 Elm St.</address>
      <phone>(123) 456-7890</phone>
   </customer>
   <customer>
      <name>Mary Jones</name>
      <address>456 Oak Ave.</address>
      <phone>(156) 789-0123</phone>
   </customer>
</customers>

XSLT File (script.xsl)

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
      xmlns:user="http://mycompany.com/mynamespace">

<msxsl:script language="JScript" implements-prefix="user">
   function xml(nodelist) {
      return nodelist.nextNode().xml;
   }
</msxsl:script>

<xsl:template match="/">
   <xsl:value-of select="user:xml(.)"/>
</xsl:template>

</xsl:stylesheet>

Output

This is the formatted output:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="script.xsl" ?>
<customers>
  <customer>
    <name>John Smith</name>
    <address>123 Elm St.</address>
    <phone>(123) 456-7890</phone>
  </customer>
  <customer>
    <name>Mary Jones</name>
    <address>456 Oak Ave.</address>
    <phone>(156) 789-0123</phone>
  </customer>
</customers>

This is the processor output:

<?xml version="1.0"?>&lt;?xml version="1.0"?&gt;&lt;?xml-stylesheet
type="text/xsl" href="script.xsl" ?&gt;&lt;customers&gt;
&lt;customer&gt;  &lt;name&gt;John Smith&lt;/name&gt;  
&lt;address&gt;123 Elm St.&lt;/address&gt;  &lt;phone&gt;(123) 456-
7890&lt;/phone&gt; &lt;/customer&gt; &lt;customer&gt;  &lt;name&gt;Mary 
Jones&lt;/name&gt;  &lt;address&gt;456 Oak Ave.&lt;/address&gt;  
&lt;phone&gt;(156) 789-0123&lt;/phone&gt; 
&lt;/customer&gt;&lt;/customers&gt;