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>
language attribute on the HTML <SCRIPT> element.| Number of occurrences | Unlimited |
| Parent elements | xsl:stylesheet, xsl:transform |
| Child elements | (No child elements) |
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.
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>