使用 msxsl:script 从 XSLT 中使用 VBScript 扩展函数时"cannot create ActiveX component"错误



我需要在xsl内部外部调用vbscript。当我在无法创建ActiveX组件的错误中验证xml时,我编写了一个示例xml和xsl。接下来,我使用Altova将xml转换为xsl,但我发现由于以下错误,xml转换失败:xpath表达式中的函数不在名称空间中,函数不在名称空间中。我已经包含了下面的xml

XML:

<LOOP_ID>
    <ID LINE="1" ID00="ISA" ISA01="00" ID02="" ID03="12" ID04="" ID05="11" ID06="111111" ID07="ZZ" ID08="11111" ID09="121005" ID10="1759" ID11="^" ID12="00501" ID13="005926056" ID14="0" ID15="P" ID16=""/>
</LOOP_ID>
xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="http://mycompany.com/mynamespace">
    <msxsl:script language="VBScript" implements-prefix="user">
        Function ValidDateFormat(sDateValue)    
            Dim Test As DateTime
           If DateTime.TryParseExact(datetime, sFormat, New CultureInfo("en-US"), DateTimeStyles.None, Test) = True Then
          Return "t"
          Else
          Return "f"
          End If
        End Function   
        Function checkDateLessthanCurrID(sValue)    
            Dim Test As DateTime
            If DateTime.TryParseExact(sDate, "yyMMdd", New CultureInfo("en-US"), DateTimeStyles.None, Test) AndAlso Test &lt; DateTime.Now Then
                Return "t"
            Else
                Return "f"
            End If
                End Function
                Function checkDateLessthanCurr(sValue)      
            Dim Test As DateTime
            If DateTime.TryParseExact(datetime, "MM:dd:yyyy", New CultureInfo("en-US"), DateTimeStyles.None, Test) AndAlso Test &lt; DateTime.Now Then
                Return "t"
            Else
                Return "f"
            End If
        End Function
    </msxsl:script>
    <xsl:output method="text" omit-xml-declaration="yes" />
    <xsl:template match="/">
        <xsl:apply-templates select="/LOOP_ID"/>
    </xsl:template>
    <xsl:template match="/LOOP_ID">
        <xsl:if test="ID/@ID09 !=''">
            <xsl:if test="user:checkDateLessthanCurrISA(string(ID/@ID09))='t'">
            </xsl:if>
        </xsl:if>

我需要验证xml中给出的属性中的日期。但是我不能验证显示错误不能找到命名空间。

我还为项目创建了一个外部dll并使用regasm注册,但我无法访问xsl中的dll。有人能帮我解决这个问题吗?

第一个错误:

无法创建ActiveX组件

意味着你的代码实际上执行了,但由于缺少ActiveX引用而失败。

您没有说明,但最有可能的是您通过使用MSXML收到此消息,因为这是我所知道的唯一使用ActiveX的XSLT编译器。在Internet Explorer中也使用。

第二个错误:

接下来,我使用Altova将xml转换为xsl,但我发现xml转换失败,由于以下错误:函数不在xpath表达式中的名称空间错误,函数不在名称空间中。

是奇怪。如果我用Altova(我使用2013 Community版本)运行它,使用/xslt命令行开关关闭XSLT 2.0向后兼容性处理,它会尝试解析脚本块并返回以下内容:

Script Compile Error(s) (relative to script begin):
Line 3, Character 0: 'datetime' is a type and cannot be used as an expression.
Line 3, Character 0: 'sFormat' is not declared. It may be inaccessible due to its protection level.
Line 3, Character 0: Type 'CultureInfo' is not defined.
Line 3, Character 0: 'DateTimeStyles' is not declared. It may be inaccessible due to its protection level.
Line 12, Character 0: 'sDate' is not declared. It may be inaccessible due to its protection level.
Line 12, Character 0: Type 'CultureInfo' is not defined.
Line 12, Character 0: 'DateTimeStyles' is not declared. It may be inaccessible due to its protection level.
Line 21, Character 0: 'datetime' is a type and cannot be used as an expression.
Line 21, Character 0: Type 'CultureInfo' is not defined.
Line 21, Character 0: 'DateTimeStyles' is not declared. It may be inaccessible due to its protection level.

表示代码不正确。我认为代码使用VBScript,你似乎正在使用的类是VB.NET。

在Microsoft的。net版本的XSLT 1.0上运行代码时,我收到与上述类似的错误。

事实证明,微软的脚本解析器不把你的代码作为VBScript(这是ActiveX),但作为一个VB。净脚本。这是可以的,因为你的代码看起来像VB.NET。

然而,实际上充满了错误。我不打算在这里修复所有错误,但这里有一个缩短版本的代码,可以在Altova和Microsoft XSLT版本上正确运行。

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:fo="http://www.w3.org/1999/XSL/Format" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    xmlns:user="http://mycompany.com/mynamespace">
    <xsl:strip-space elements="*"/>
    <msxsl:script language="VBScript" implements-prefix="user">
        Function ValidDateFormat(sDateValue As String) As String
            Dim Test As DateTime
            Dim sFormat As String = "MM-dd-YY"
            If DateTime.TryParseExact(sDateValue, sFormat, New Globalization.CultureInfo("en-US"), Globalization.DateTimeStyles.None, Test) = True Then
                Return "true"
            Else
                Return "false"
            End If
        End Function   
    </msxsl:script>
    <xsl:output method="text" omit-xml-declaration="yes" />
    <xsl:template match="/">
        <xsl:apply-templates />
    </xsl:template>
    <xsl:template match="ID[@ID09 !='']">
        <xsl:value-of select="user:ValidDateFormat(string(@ID09))"/>                
    </xsl:template>
</xsl:stylesheet>

返回真或假。我建议您在使用XSLT之前先在Visual Studio中试用这些代码,因为在XSLT中很难调试。

On mxsxl:script

在我之前的文章中,我错误地认为mssxsl:script不被Altova支持。Martin Honnen在评论中纠正了我。它是受支持的,看起来它使用了与Microsoft使用的相同的代码提供程序。

您最初的错误是ActiveX错误。如果希望使用ActiveX XSLT处理器运行代码,则需要做更多的事情才能使其运行。首先,你的代码不是ActiveX VBScript。其次,您需要确保代码所需的适当ActiveX对象可以被实例化(即,在系统路径上)。

在非Microsoft Internet Explorer和其他处理器的浏览器中,不支持msxsl:script,任何浏览器都不支持。net脚本,请使用ActiveX脚本。

关于XSLT 2.0

如果您可以使用Altova,那么您实际上是在使用XSLT 2.0,它不需要您编写的扩展:它可以本机比较时间和日期,并且可以获得当前日期和时间。XSLT 2.0处理器包括但不限于:Altova(本机)、Saxon (Java和IKVM.NET)、Exselt (. net)、XMLPrime (. net)。

相关内容

最新更新