【问题标题】:XSL/JScript difference in behaviour between Visual Studio and IEVisual Studio 和 IE 之间的 XSL/JScript 行为差异
【发布时间】:2011-05-23 10:01:00
【问题描述】:

谁能告诉我为什么下面的 XSL 在 IE9 中可以愉快地转换下面的 XML,但是在所有版本的 Visual Studio 下相同的转换都失败了?如果我在 IE 9 中打开 XML 文件,它会被转换并且输出符合预期,但是如果我尝试在 Visual Studio 中对 XML 文件进行相同的转换(使用工具栏上的“开始 XSLT”按钮),我会得到一个 JScriptException说功能就行了

var node = root.nextNode();

修复似乎是更改 javascript 函数以执行以下操作:

function test(root, attr)
{
  root.MoveNext();
  var node = root.Current;
  return node.Select("breakfast" + attr);
}

但这会导致 IE 中的 XSLT 转换失败!我好像赢不了!

XSL:

<!--<?xml version="1.0"?>-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="urn:custom-scripts">
<msxsl:script language="JScript" implements-prefix="user">
  <![CDATA[
function test(root, attr)
{
  var node = root.nextNode();
  return node.selectSingleNode("breakfast" + attr);
}
]]>
</msxsl:script>
  <xsl:template match="/">
    <HTML>
      <BODY STYLE="font-family:Arial, helvetica, sans-serif; font-size:12pt;
            background-color:#EEEEEE">
          <xsl:value-of select="user:test(., '-menu')"/>
      </BODY>
    </HTML>
  </xsl:template>
</xsl:stylesheet>

目标 XML:

<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="simple.xsl" ?>
<breakfast-menu>
  <food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>
    <description>Two of our famous Belgian Waffles 
      with plenty of real maple syrup.</description>
    <calories>650</calories>
  </food>
  <food>
    <name>Homestyle Breakfast</name>
    <price>$6.95</price>
    <description>Two eggs, bacon or sausage, toast, 
      and our ever-popular hash browns.</description>
    <calories>950</calories>
  </food>
</breakfast-menu>

【问题讨论】:

    标签: javascript xml internet-explorer xslt


    【解决方案1】:

    IE 使用 MSXML 作为其 XSLT 处理器(我认为 IE 9 使用 MSXML 6),而 Visual Studio 使用 XslCompiledTransform。 MSXML 和 XslCompiledTransform 公开和使用的 API 差别很大,因此不要期望针对 MSXML API 编写的扩展函数代码可以与 XslCompiledTransform 和 .NET API 一起使用。请参阅http://msdn.microsoft.com/en-us/library/wxaw5z5e.aspx,了解使用扩展函数时 XSLT/XPath 类型如何映射到 .NET 类型。在您的情况下,您从 XSLT 传入一个节点集和一个字符串,该字符串映射到 .NET 中的 XPathNodeIterator 和字符串。 这是为 .NET 重写扩展函数的快速尝试:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:user="urn:custom-scripts">
      <msxsl:script language="JScript" implements-prefix="user">
        <![CDATA[
    function test(nodeIterator, string)
    {
      nodeIterator.MoveNext();
      return nodeIterator.Current.SelectSingleNode("breakfast" + string);
    }
    ]]>
      </msxsl:script>
      <xsl:template match="/">
        <HTML>
          <BODY STYLE="font-family:Arial, helvetica, sans-serif; font-size:12pt;
                background-color:#EEEEEE">
            <xsl:value-of select="user:test(., '-menu')"/>
          </BODY>
        </HTML>
      </xsl:template>
    </xsl:stylesheet>
    

    [编辑] 糟糕,我错过了您自己已经找到了 .NET 代码,并且只想知道如何针对这两个 XSLT 处理器编写代码。那很难。您的目标平台和目标是什么,您想为 IE 编写 XSLT 但使用 VS 开发吗?或者您真的需要在 IE 和 .NET 平台上使用相同的样式表?

    这里尝试为两种类型的处理器编写一个扩展函数:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:user="urn:custom-scripts">
      <msxsl:script language="JScript" implements-prefix="user">
        <![CDATA[
    function test(nodeSet, string)
    {
      if (typeof nodeSet.nextNode !== 'undefined') {
        var node = nodeSet.nextNode();
        return node.selectSingleNode('breakfast' + string);
      }
      else if (typeof nodeSet.MoveNext !== 'undefined') {
        nodeSet.MoveNext();
        return nodeSet.Current.SelectSingleNode("breakfast" + string);
      }
    }
    ]]>
      </msxsl:script>
      <xsl:template match="/">
        <HTML>
          <BODY STYLE="font-family:Arial, helvetica, sans-serif; font-size:12pt;
                background-color:#EEEEEE">
            <xsl:value-of select="user:test(., '-menu')"/>
          </BODY>
        </HTML>
      </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

    • 谢谢 - 我想你已经找到了答案。我不打算同时开发,我想这只是一个工作流问题 - 我在 IE 上部署之前在 Visual Studio 中进行调试,所以这让人有些头疼。
    【解决方案2】:

    您可能会发现嵌入到 Visual Studio 中的浏览器不是 IE9。它很可能是 IE8 的一个版本,或者可能是 IE7,这取决于发布 Visual Studio 时哪个是稳定版本。

    我对 XLST 的了解并不多,无法解释为什么它不起作用,但这可以解释您所看到的行为差异。

    【讨论】:

      【解决方案3】:

      我想知道这个关于 SO 的答案是否相关? IE9 selectSingleNode missing from beta, how to overcome this in JavaScript?

      【讨论】:

      • 谢谢,很有趣。 SelectSingleNode() (如我上面的例子)似乎在 IE9 中工作,所以如果他们将它添加回来,我会徘徊......
      猜你喜欢
      • 1970-01-01
      • 2010-09-25
      • 2012-03-05
      • 1970-01-01
      • 1970-01-01
      • 2016-08-31
      • 2011-09-01
      • 2014-07-25
      • 2020-03-14
      相关资源
      最近更新 更多