简体   繁体   中英

Using C# in XSLT with MSXSL 6.0

I am attempting to execute an XML transform using the MSXSL 6.0 processor, and the XSLT file has a C# method at the top of it. Here is the sample XSLT I'm using:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:my-scripts">
    <msxsl:script language="C#" implements-prefix="user">
        <msxsl:using namespace="System.DateTime"/>
        <msxsl:using namespace="System.TimeZone"/>
        <![CDATA[
            public string GetLocalTime(string returnPart, string utcTime){
                string[] timeList = utcTime.Split(':');
                string endString = string.Join(":", timeList.Take(3));
                DateTime result = TimeZone.CurrentTimeZone.ToLocalTime(DateTime.Parse(endString));
                if(returnPart == "Date")
                {
                    return result.ToString("MM/dd/yyyy");
                }
                else if(returnPart == "Time")
                {
                    return result.ToString("HH:mm:ss");
                }
                else
                {
                    return result.ToString();
                }
            }           
        ]]>
    </msxsl:script>

Initially I had a line just after the msxsl:script tag like this:

<msxsl:assembly name="System.DateTime" />

When attempting to run the transform I received an error here:

External XSLT processing started...

Error occurred while compiling blah blah blah

    Code:   0x80004005
    Keyword msxsl:script may not contain msxsl:assembly.
    ...done 

I did a little research and found that the System assembly is included by default, so I removed the assembly line and tried to run it again. THis time I got:

External XSLT processing started...

Error occurred while compiling blah blah blah

Code:   0x80004005
Keyword msxsl:script may not contain msxsl:using.
...done 

I've tried searching up this particular error, but have not found anything very useful. Any help would be appreciated.

Thanks

You won't be able to run C# code embedded in Xslt if you are using the msxsl processor. msxsl is using native Xml/Xslt processor which will not bootstrap CLR (managed runtime) for you. You can use vbscript/jscript inside msxsl:script when using the native Xml stack but C#/VB.NET can be used only with the managed Xslt processor (ie XsltCompiledTransform ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM