XSLT 名称空间别名在 Firefox 或 Chrome 中不起作用

XSLT namespace-alias Not Working in Firefox or Chrome

我正在将 xhtml 转换为 xhtml,但需要一个 xslt 样式表作为结果文档的一部分(样式表将包含在 <script type"text/template"> 元素中。我正在使用 xsl:namespace-alias 指令,在 IE 中运行良好,但在 Chrome 和 Firefox 中均失败。

相关代码如下:

<xsl:output  doctype-system="about:legacy-compat" omit-xml-declaration="yes" indent="yes" method="html" media-type="application/xhml" encoding="utf-8"/>
<xsl:namespace-alias stylesheet-prefix="wxsl" result-prefix="xsl" />

  <xsl:template match="head">
     <!-- Some code omitted for clarity -->
       <script type="text/template">

        <wxsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"
          xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:eli="local">

          <wxsl:template select="/">
            <wxsl:apply-templates />
        </wxsl:template>


      </wxsl:stylesheet>


    </script>

    </xsl:copy>
  </xsl:template>

它在 IE 中输出所需的转换,但是 Firefox 和 Chrome 的 XSLT 处理器没有用 xsl 替换 wxsl 前缀。

Mozilla 不支持它,有一个公开的错误报告 https://bugzilla.mozilla.org/show_bug.cgi?id=213996

至于 Chrome,我写了一个简短的测试用例,XHTML 输入 http://home.arcor.de/martin.honnen/xslt/test2016012402.xml (just a short XHTML sample with a head to add some XSLT then with XSLT) and the stylesheet http://home.arcor.de/martin.honnen/xslt/test2016012401.xsl 进行 XHTML 到 XHTML 的转换,使用别名保护任何 XSLT 和 XSLT 嵌套的 XHTML 结果元素:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0"
  xmlns:xhtml="http://www.w3.org/1999/xhtml"
  xmlns:axsl="http://example.com/xsl-alias"
  xmlns:axhtml="http://example.com/xhtml-alias"
  exclude-result-prefixes="xhtml axsl axhtml"
  xmlns="http://www.w3.org/1999/xhtml">

<xsl:output method="xml" indent="yes"/>

<xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>
<xsl:namespace-alias stylesheet-prefix="axhtml" result-prefix="#default"/>

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="xhtml:head">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
    <axsl:stylesheet version="1.0">
      <axsl:template match="/">
        <axhtml:p>XSLT created paragraph.</axhtml:p>
      </axsl:template>
    </axsl:stylesheet>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

并且 Chrome 似乎转换得很好,检查的控制台显示结果元素位于正确的命名空间中:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Test</title>
<axsl:stylesheet xmlns:axsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><axsl:template match="/"><axhtml:p xmlns:axhtml="http://www.w3.org/1999/xhtml">XSLT created paragraph.</axhtml:p></axsl:template></axsl:stylesheet></head>
<body>
<h1>Test</h1>
</body>
</html>

我已经扩展了测试用例以尝试执行嵌入式样式表并且 Edge 和 Chrome 都能够做到这一点,请参阅 http://home.arcor.de/martin.honnen/xslt/test2016012407.xml,尽管 Chrome 出于我的原因无法识别无法在我设置的 DOMContentLoaded 事件侦听器中执行此操作。但这似乎不是与使用 XSLT 插入 XSLT 相关的问题,当我使用按钮 运行 使用嵌入式样式表执行 XSLT 的脚本时,它在 Chrome 中工作正常。显然,Firefox 缺乏对 xsl:namespace-alias 的支持,永远找不到样式表根元素能够将一些代码提供给 XSLTProcessor.