摘要:今天在CSDN论坛上看到一个网友在请教关于msxsl:script的用法。感觉这问题也许有点意思,所以在下面简述一下。
在MSXML里,你在msxsl:script里可以使用Javascript和VBScript,参考
<msxsl:script> Element
但在.NET里,你只能使用那些.NET支持的语言,包括C#,VB.NET,JScript等。
譬如,我们想在XSLT里计算下面这个XML里people的和,
<root> <people>1</people> <people>2</people> <people>3</people> <people>4</people></root>
我们可以使用象这样的XSLT
<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>
在MSXML和浏览器里,我们也可以这么做,
<xsl:stylesheet version="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>
但如果你在.NET下使用上述XSLT的话,你就会得到下列错误:
Unhandled Exception: System.Xml.Xsl.XsltException: Function 'user:sum()' has failed. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Microsoft.JScript.JScriptException: Function expectedFunction 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......[
阅读全文]