<xp:script> Element

 
 
Note
In this document it is assumed that the prefix xp is bound to the namespace http://www.xmlprime.com/.
 
Contains a script block used to define custom functions in an external language.

Syntax

 
<xp:script
  implements-prefix? = ncname
  language? = string >
<-- Content: ((<xp:assembly><xp:assembly><xp:assembly> | <xp:using><xp:using><xp:using>)*, text)-->
</xp: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 value of this attribute is case insensitive. If this attribute is not specified then the language defaults to JScript.

Remarks

 
 
Note
This element is specific to XmlPrime, and is not part of the XSLT 2.0 standard.
 

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.

<xp: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. The class is instanciated the first time a non-static method is called in an XSLT transformation.

 
 
Note
This behaviour may change in a future version. In particular we intend to compile each script block into a seperate class.
 

Once the script blocks are compiled, their methods are exposed as if the compiled classes were passed to the NativeModule<T> ()NativeModule(Of T) ()NativeModule<T> () method. The rules for how methods are exported as functions are explained in . In particular the XdmFunctionAttributeXdmFunctionAttributeXdmFunctionAttribute and XdmTypeAttributeXdmTypeAttributeXdmTypeAttribute attributes can be used to more precisely define the signatures of the exported functions.

 
 
Note
The way in which types are mapped is significantly different to <msxsl:script><msxsl:script><msxsl:script>.
 

Assemblies

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

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

  • XmlPrime.dll
  • 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 fact which computes the factorial of its argument.

 
<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xp="http://www.xmlprime.com/"
                xmlns:my="urn:MyModule">

  <xp:script implements-prefix="my" language="C#">
    [XdmFunction("fact")]
    [XdmType(XmlTypeCode.Integer)]
    public int Factorial([XdmType(XmlTypeCode.Integer)] int arg)
    {
      int fact = 1;
      for (int x = 1; x < arg; x++)
      {
        fact *= x;
      }

      return fact;
    }
  </xp:script>

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