Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
XSLT Reference
XSLT Elements
 <msxsl:script> Element
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
<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>
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.

For managed code, this attribute accepts the language names that are accepted by CreateProvider.

implements-prefix

Required. Associates the prefix with the script block. The value of this attribute is the prefix that represents the namespace.

Number of occurrences

Unlimited

Parent elements

xsl:stylesheet, xsl:transform

Child elements

<msxsl:assembly> Element

<msxsl:using> Element

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.

The <msxsl:assembly> and <msxsl:using> child elements are only available for managed code within the .NET Framework. When used, these elements must be at the beginning of the script block, before any script code.

In unmanaged code, 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>.

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>
Xml
<?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>

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"?>&lt;?xml version="1.0"?&gt;&lt;?xml-stylesheet
type="text/xsl" href="script.xsl" ?&gt;&lt;customers&gt;
&lt;customer&gt;  &lt;name&gt;John Smith&lt;/name&gt;  
&lt;address&gt;123 Elm St.&lt;/address&gt;  &lt;phone&gt;(123) 456-
7890&lt;/phone&gt; &lt;/customer&gt; &lt;customer&gt;  &lt;name&gt;Mary 
Jones&lt;/name&gt;  &lt;address&gt;456 Oak Ave.&lt;/address&gt;  
&lt;phone&gt;(156) 789-0123&lt;/phone&gt; 
&lt;/customer&gt;&lt;/customers&gt;
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
'xml' function slightly different in VS2008/.NET 3.5 SP1      Mick Badran - MVP   |   Edit   |   Show History
After setting up a simple test project in VS2008, an xml file, and a xslt. Using the debugging features, stepping through.

I found the 'nodelist' variable to be of type XPathArrayIterator and the MoveNext() method returns true. Then Inner or Outer Xml at this point works.

e.g. -----

<msxsl:script language="JScript" implements-prefix="user">
function xml(nodelist) {
if (nodelist.MoveNext())
return nodelist.Current.InnerXml;
else
return("ERROR: No Root Node");
}
</msxsl:script>
Re: 'xml' function slightly different in VS2008/.NET 3.5 SP1      OCMbui   |   Edit   |   Show History
XPathArrayIterator is internal an not accessible. Its actually XPathNodeIterator which is one of its base types.
Tags What's this?: Add a tag
Flag as ContentBug
use addObject and pass the DLL      Shuo   |   Edit   |   Show History
Could you please explain more on this part? I made a search through Internet, it seems less info on the topic. Currently, I need the ability that xslt talking with external world, through dll functions.
Senarior is similar like:
while parsing, I get the value 'abc' from source xml, but this value should be mapped to some other value 'def', I need to put 'def' into the output xml file. How that map handle is defined in one dll function.

Do you have any idea or comments?
Tags What's this?: dll (x) msxml (x) xml (x) xslt (x) Add a tag
Flag as ContentBug
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker