【问题标题】:XSLT+JavaScript: using classesXSLT+JavaScript:使用类
【发布时间】:2010-04-21 13:22:18
【问题描述】:

我正在尝试使用 XSL 中的类(“msxsl:script”标签)。但是在调试文件时我收到“语法错误”消息。这是我正在使用的一个简单代码:

function Test1(str)
{
    this.str = str;
}

Test1.prototype.getStr = function()
{
    return this.str;
}

function test()
{
    var newTest1 = new Test1("some string");
    return (newTest1.getStr());
}

如果我将代码插入到 aspx 文件并调用测试函数,一切正常,没有任何错误消息。 是否可以在 XSL 中使用类?

【问题讨论】:

  • 能否请您发布您正在使用的实际 XSLT。我认为语法错误与您使用类无关。
  • 实际上,我的 XSL 文件一切正常。我可以毫无问题地执行它(没有脚本)。插入脚本后,我在这一行出现语法错误:Test1.prototype.getStr = function() 所有代码都位于 msxsl:script 中。我还插入了 CDATA。
  • 您是否在脚本中插入了 CDATA 转义 <>& 字符?只有一个是必需的 - CDATA 转义。
  • 所有代码都在 CDATA 中。如您所见,代码中没有 或其他特殊符号。

标签: javascript xml xslt class function


【解决方案1】:

似乎对您可以在脚本块的顶层使用的内容有一些奇怪的限制,并且它们不允许在顶层函数中使用this。但是,如果您再深入一层,其中一些限制就会消失:

function MakeTest1()
{
   function inner(s)
   {
      this.str = s;
   }

   inner.prototype.getStr = function()
   {
      return this.str;
   }

   return inner;
}
var Test1 = MakeTest1();

function test()
{
    var newTest1 = new Test1("some string");
    return (newTest1.getStr());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-02
    • 2012-05-17
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多