<xp:script> Element
xp
is bound to the namespace http://www.xmlprime.com/
.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
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.
The content of the script element should be code in the specified language, and can contain methods and variables.
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.
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.
Namespaces
Namespaces can be imported for use in a script block using the <xp:using><xp:using><xp:using> element.
A number of namespaces are imported by default. These namespaces are listed below:
- XmlPrimeXmlPrimeXmlPrime
- XmlPrime.SerializationXmlPrime.SerializationXmlPrime.Serialization
- SystemSystemSystem
- System.Collections.GenericSystem.Collections.GenericSystem.Collections.Generic
- System.TextSystem.TextSystem.Text
- System.Text.RegularExpressionsSystem.Text.RegularExpressionsSystem.Text.RegularExpressions
- System.XmlSystem.XmlSystem.Xml
- System.Xml.SchemaSystem.Xml.SchemaSystem.Xml.Schema
- System.Xml.XPathSystem.Xml.XPathSystem.Xml.XPath
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 isVisualBasic
)
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>