<msxsl:script> Element

 
 
Note
In this document it is assumed that the prefix msxsl is bound to the namespace urn:schemas-microsoft-com:xslt.
 
Contains a script block used to define custom functions in a .NET language.

Syntax

 
<msxsl:script
  implements-prefix? = ncname
  language? = string >
<-- Content: ((<msxsl:assembly><msxsl:assembly><msxsl:assembly> | <msxsl:using><msxsl:using><msxsl:using>)*, text)-->
</msxsl:script>
 

Attributes

implements-prefix

Type:ncname

The prefix of the target namespace of the exported module. This prefix must be bound to an in scope namespace.

language

Type:string

The language used to compile the content of this element. The languages supported are those supported by the CreateProviderCreateProviderCreateProvider method. If this attribute is not specified then the language defaults to JScript.

Remarks

 
 
Note
This element is provided for compatability with the XslCompiledTransformXslCompiledTransformXslCompiledTransform and XslTransformXslTransformXslTransform classes. For new development with XmlPrime we recommend using the <xp:script><xp:script><xp:script> element instead.
 

The language attribute specifies the language used to implement the script block. The languages supported are those supported by the CreateProviderCreateProviderCreateProvider method. Any .NET language can be used, as long as the appropriate code provider is installed and registered on the machine.

<msxsl:script><msxsl:script><msxsl:script> is a top level element and can only appear as a child of the <xsl:stylesheet> element.

 
 
Warning
Compilation of a stylesheet using the <xp:script><xp:script><xp:script> element requires Full Trust.
 
 
 
Warning
If XmlPrime is not installed to the GAC, then in order to link to the XmlPrime DLL any temporary DLLs generated with the script extension must be written to disk. For optimal performance we recommend that XmlPrime is installed to the GAC.
 

The content of the script element should be code in the specified language, and can contain methods and variables.

 
 
Note
It is recommended that the content of the script element is placed in a CDATA section, to avoid having to escape characters like '<'.
 

Exporting Functions

All script elements implementing the same target namespace are combined to produce a single class. This class is compiled, and is then made available to the stylesheet. For this reason all script blocks for a single namespace must be written in the same language. All functions and variables in a script block are made available to all other script blocks in the same namespace. The class is instanciated the first time a non-static method is called in an XSLT transformation.

All methods that satisfy the following conditions are exported.

  • The method is public.
  • There is no other public method with the same name and same number of arguments.
  • All argument types and return types are supported.

The following argument and return types are supported.

CLR Type XDM Type objectObjectobject item()* XPathItemXPathItemXPathItem item() node()* node() xs:string xs:double boolBooleanbool xs:boolean voidvoidvoid empty-sequence()

If an argument is declared to be of type objectObjectobject then the value of the argument is constructed as follows:

 
 
Note
The type bindings are defined in this way for compatability. For greater control over the signatures of your script functions we recommend instead using the <xp:script><xp:script><xp:script> element.
 

Namespaces

Assemblies

Additional assemblies can be referenced for use in compiling a script block by using the <msxsl:assembly><msxsl:assembly><msxsl:assembly> element.

A number of assemblies are referenced by default. These assemblies are listed below:

  • System.dll
  • System.Xml.dll
  • Microsoft.VisualBasic.dll (when the language is VisualBasic)

Example

The following example shows a script block for the namespace urn:MyModule which exports a function Factorial which computes the factorial of its argument. Note that the function exported has signature my:Factorial(xs:double) as xs:double.

 
<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:my="urn:MyModule">

  <msxsl:script implements-prefix="my" language="C#">
    public int Factorial(int arg)
    {
      int fact = 1;
      for (int x = 1; x < arg; x++)
      {
        fact *= x;
      }

      return fact;
    }
  </msxsl:script>

  <xsl:template match="/">
    <result>
      <xsl:value-of select="my:Factorial(10)" />
    </result>
  </xsl:template>
</xsl:stylesheet>