<msxsl:script> Element
msxsl
is bound to the namespace urn:schemas-microsoft-com:xslt
.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
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.
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. 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 objectObjectobjectitem()*
XPathItemXPathItemXPathItem
item()
node()*
node()
xs:string
- doubleDoubledouble
- floatSinglefloat
- decimalDecimaldecimal
- longLonglong
- intIntegerint
- shortShortshort
- byteBytebyte
- ulongULongulong
- uintUIntegeruint
- ushortUShortushort
- sbyteSBytesbyte
xs:double
boolBooleanbool
xs:boolean
voidvoidvoid
empty-sequence()
Namespaces
Namespaces can be imported for use in a script block using the <msxsl:using><msxsl:using><msxsl:using> element.
A number of namespaces are imported by default. These namespaces are listed below:
- SystemSystemSystem
- System.CollectionsSystem.CollectionsSystem.Collections
- System.TextSystem.TextSystem.Text
- System.Text.RegularExpressionsSystem.Text.RegularExpressionsSystem.Text.RegularExpressions
- System.XmlSystem.XmlSystem.Xml
- System.Xml.XslSystem.Xml.XslSystem.Xml.Xsl
- System.Xml.XPathSystem.Xml.XPathSystem.Xml.XPath
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 isVisualBasic
)
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>