How to Use msxsl: script when using XSLT in. net

Source: Internet
Author: User

In MSXML, you can use JavaScript and VBScript in msxsl: script. For more information, see

<Msxsl: SCRIPT> element

However, in. net, you can only use languages supported by. net, including C #, VB. NET, and JScript.

For example, we want to calculate the sum of people in the XML below in XSLT,

<Root>
<People> 1 </People>
<People> 2 </People>
<People> 3 </People>
<People> 4 </People>
</Root>

We can use XSLT like this

<XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">
<XSL: output method = "text"/>

<XSL: template match = "/">
Sum: <XSL: value-of select = "sum (root/People)"/>
</XSL: Template>

</XSL: stylesheet>

In MSXML and browsers, we can also do this,

<XSL: stylesheetversion = "1.0"
Xmlns: XSL = "http://www.w3.org/1999/XSL/Transform"
Xmlns: msxsl = "urn: Schemas-Microsoft-com: XSLT"
Xmlns: User = "anything here">

<XSL: output method = "text"/>

<Msxsl: script language = "JavaScript" implements-Prefix = "user">
<! [CDATA [
Function sum (nodelist)
{
VaR D = 0;
VaR node = nodelist. nextnode ();
While (node! = NULL)
{
D + = parseint (node. Text );
Node = nodelist. nextnode ();
}

Return D;
}
]>
</Msxsl: SCRIPT>

<XSL: template match = "/">
Sum: <XSL: value-of select = "User: sum (root/People)"/>
</XSL: Template>

</XSL: stylesheet>

However, if you use the preceding XSLT in. net, you will get the following error:

Unhandled exception: system. XML. XSL. extends texception: function 'user: sum () 'has failed. ---> system. reflection. targetinvocationexception: exception has been thrown by the target of an invocation. ---> Microsoft. JScript. jscriptexception: function expected
Function expected
At Microsoft. JScript. latebinding. Call (Binder binder, object [] arguments, parametermodifier [] modifiers, cultureinfo culture, string [] namedparameters, Boolean construct, Boolean brackets, vsaengine engine)
At Microsoft. JScript. latebinding. Call (object [] arguments, Boolean construct, Boolean brackets, vsaengine engine)
At Microsoft. XSLT. compiledscripts. JScript. scriptclass_1.sum (Object nodelist)
....

Why? In MSXML, The nodeset parameter nodelist is an ixmldomnodelist instance, and its nextnode method returns an ixmldomnode instance.

However. net maps the nodeset parameter to system. XML. XPath. xpathnodeiterator or its subclass instances (for more mapping information, see XSLT stylesheet scripting using <msxsl: SCRIPT> or Aaron skonnard'sArticle). In fact, if you run the preceding conversion, you will see the xpathqueryiterator class. However, in. net, you can only use classes in. net, rather than MSXML-related classes/methods, such

<XSL: stylesheetversion = "1.0"
Xmlns: XSL = "http://www.w3.org/1999/XSL/Transform"
Xmlns: msxsl = "urn: Schemas-Microsoft-com: XSLT"
Xmlns: User = "anything here"
>

<XSL: output method = "text"/>

<Msxsl: script language = "JavaScript" implements-Prefix = "user">
Function GetType (nxpni)
{
Return nxpni. GetType (). Name;
}

Function sum (nxpni)
{
VaR D = 0;
While (nxpni. movenext ())
D + = convert. toint32 (nxpni. Current. value );

Return D;
}
</Msxsl: SCRIPT>

<XSL: template match = "/">
Type: <XSL: value-of select = "User: GetType (.)"/>
Sum: <XSL: value-of select = "User: sum (root/People)"/>
</XSL: Template>

</XSL: stylesheet>

Of course, you can also use C #

<XSL: stylesheetversion = "1.0"
Xmlns: XSL = "http://www.w3.org/1999/XSL/Transform"
Xmlns: msxsl = "urn: Schemas-Microsoft-com: XSLT"
Xmlns: User = "anything here"
>

<XSL: output method = "text"/>

<Msxsl: script language = "C #" implements-Prefix = "user">
String GetType (xpathnodeiterator nxpni)
{
Return nxpni. GetType (). Name;
}

Int sum (xpathnodeiterator nxpni)
{
Int D = 0;
While (nxpni. movenext ())
D + = convert. toint32 (nxpni. Current. value );

Return D;
}
</Msxsl: SCRIPT>

<XSL: template match = "/">
Type: <XSL: value-of select = "User: GetType (.)"/>
Sum: <XSL: value-of select = "User: sum (root/People)"/>
</XSL: Template>

</XSL: stylesheet>

Aaron skonnard described this in the column "the XML files" in the msdn magazine.

Extending XSLT with JScript, C #, and Visual Basic. net

If you want to use extension functions under. Net in XSLT, refer to the extreme XML column of Microsoft dare Obasanjo on msdn.

Exslt meets xpath

to: http://blog.joycode.com/saucer/archive/2004/05/12/21273.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.