2

我制作了一个使用 msxml:script 与 XMLCompiledTransform 一起使用的 XSL 脚本,但需要它的人说该脚本无法在他们的 Linux/Perl 环境中运行(这就是我所知道的他们如何使用 XSL ) 因为它“使用 Microsoft 特定的扩展”。所以我试图通过使用 xsl:script 使 XSL 更加中立。但是,我很难让它工作。

<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:theScript="urn:CustomScript" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="xsl theScript fo xs fn">
<xsl:output method="xml" indent="yes" version="1.0" encoding="utf-8" omit-xml-declaration="no"/>
  <xsl:script language="javascript" implements-prefix="theScript">
      <![CDATA[
      function test() {return "test"; }
      ]]>
  </xsl:script>
  <xsl:template match="/">
    <test>
      <xsl:value-of select="theScript:test()" />
    </test>
  </xsl:template>
</xsl:stylesheet>

上面给了我错误“找不到实现前缀'urn:CustomScript'的脚本或外部对象。”

如果我摆脱xmlns:theScript="urn:CustomScript"它会给我错误“前缀'theScript'未定义。”

我还尝试删除所有“theScript”前缀的痕迹并仅使用implements-prefix="local",但这也不起作用。它告诉我test是一个未知的 XSLT 函数。

那么我只是在这里做错了什么,还是 XMLCompiledTransform 不支持xsl:script

4

2 回答 2

2

xsl:script仅在 XSLT1.1 中定义,但该标准已取消,取而代之的是 XSLT2.0,因此实际上如果您正在寻找可移植性,我不建议您使用它。

XSLT 最可移植的形式肯定是 XSLT1.0,因为对它的支持要多得多。尽管在 XSLT2.0 中有些事情肯定更容易,但您可能会发现您可能想做的大多数“脚本”代码都可以仅在 XSLT 中完成。XSLT2.0 可以定义函数,但是在 XSLT1.0 中可以使用命名模板来模拟函数,只是比较麻烦。

XSLT2.0:

<xsl:template match="/">
  <xsl:value-of select="test('param')" />
</xsl:template>

<xsl:function name="test" as="xs:string">
  <xsl:param name="param" as="xs:string" />
  <xsl:text>Test</xsl:text>
</xsl:function>

XSLT1.0:

<xsl:template match="/">
  <xsl:call-template name="test">
    <xsl:with-param name="param" select="'param'" />
  </xsl:call-template>
</xsl:template>

<xsl:template name="test">
  <xsl:param name="param" />
  <xsl:text>Test</xsl:text>
</xsl:template>

但是,此示例中未使用该参数,但您明白了。

于 2011-07-08T23:12:29.797 回答
1

XSLT中没有xsl:script指令。

没有任何XMLCompiledTransform类是 NET 的一部分。

System.Xml.Xsl 命名空间中 .NET 附带的现有XslCompiledTransform类实现了兼容的 XSLT 1.0 处理器,因此它不支持任何指令xsl:script

建议:要实现 XSLT 应用程序的真正可移植性,不要使用任何(内联或非内联)扩展功能或未记录的、非强制性或不兼容的功能。

虽然许多 XSLT 1.0 处理器支持 EXSLT,但该XslCompiledTransform处理器仅支持node-set()EXSLT 扩展功能。

于 2011-07-08T22:44:12.807 回答