How to use MSXSL: Script when using XSLT in .NET

xiaoxiao2021-03-06  59

In MSXML, you can use JavaScript and VBScript, reference in MSXSL: Script

Element

But in .NET, you can only use the language supported by .NET, including C #, VB.NET, JScript, etc.

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

1 2 3 4

We can use XSLT like this

sum:

In MSXML and browsers, we can also do this.

Return D;}]]>

Sum:

But if you use the above XSLT in .NET, you will get the following error:

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 Construction, Boolean Brackets, vsaEngine Engine) AT Microsoft.xslt.comPiledscripts.jscript.scriptclass_1.sum (Object Nodelist) .... Why? Because in MSXML, Nodelist This NodeSet parameter is an IXMLDomnodeList instance, and its nextNode method returns an IXMLDomnode instance.

And .NET maps this NodeSet parameter to instances of System.xml.xPath.XPathNodeItemrator or their subclasses (as for other mapping, refer to XSLT Stylesheet scripting applicing or later mentioned Aaron Skonnard article) . In fact, if you run the above conversion, you can see the XPathQueryITeiterator class. But no matter what, under .NET, you can only use the class under .NET, you can't use the class / way related to MSXML, such as

function gettype (nxpni) {return nXpni.gettype (). name;}

Function SUM (NXPNI) {var D = 0; while (nxpni.movenext ()) D = Convert.Toint32 (NXPni.current.Value);

Return D;}

Type:

Of course you can also use C #

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;}

Type:

Aaron Skonnard has a description in the column "The XML Files" in MSDN Magazine

Extending Xslt with Jscript, C #, And Visual Basic .NET

If you want to use the extended function under the .NET in XSLT, refer to Microsoft Dare Obasanjo on the Extreme XML column on MSDN.

EXSLT Meets XPath From: http://blog.joycode.com/saucer/

转载请注明原文地址:https://www.9cbs.com/read-113279.html

New Post(0)