<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. - implements-prefix
- Required. Declares a namespace and associates it 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 | (No child elements) |
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.
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>
.
Note To test this example in Internet Explorer, you need to use a script. For more information, see Initiate XSLT in a Script.
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" encoding="UTF-16"?><?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>