XSL转换适用于IE,而不是Chrome



使用下面的xsl转换xml文档时遇到了一些问题。该代码使用xmlDoc.transformNode在IE上工作,但以下代码在chrome中不工作-resultDocument返回null。

var xslString = "<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="options">
        <div id="divAllOpt" class="sDiv" ALL="1" style="FONT-WEIGHT:bold;">
          All
        </div>
        <div id="divGroupOnOpt" class="sDiv" GROUP="1" style="FONT-WEIGHT:bold;">
          Group By
        </div>
        <xsl:apply-templates select="option[. != '']">
          <xsl:sort select="."/>
        </xsl:apply-templates>
      </xsl:template>
      <xsl:template match="option">
        <div class="sDiv">
          <xsl:choose>
            <xsl:when test="@value">
              <xsl:attribute name="VALUE">
                <xsl:value-of select="@value"></xsl:value-of>
              </xsl:attribute>
            </xsl:when>
            <xsl:otherwise>
              <xsl:attribute name="VALUE">
                <xsl:value-of select="."></xsl:value-of>
              </xsl:attribute>
            </xsl:otherwise>
          </xsl:choose>
          <xsl:attribute name="title">
            <xsl:value-of select="."></xsl:value-of>
          </xsl:attribute>
          <xsl:value-of select="."></xsl:value-of>
          <xsl:if test="@count">
            (<xsl:value-of select="@count"></xsl:value-of>)
          </xsl:if>
        </div>
      </xsl:template>
    </xsl:stylesheet>";
var xsltProcessor = new XSLTProcessor();
var xslSheet = new DOMParser().parseFromString(xslString, 'application/xml');
xsltProcessor.importStylesheet(xslSheet);
var resultDocument = xsltProcessor.transformToFragment(xmlDoc.documentElement, document);

我尝试转换的xml文档(xmlDoc.docentElement.outerHTML)如下所示-

"<options>
  <option count="3">ABC</option>
  <option count="8">XYZ</option>
  <option count="32">CVB</option>
  <option count="1">TST</option>
  <option count="2">CNN</option>
</options>"

有什么想法吗?谢谢

EDIT——我把jsfiddle放在一起——在Firefox中有效,但在Chrome中无效。

http://jsfiddle.net/facm1ptz/5/

不要期望能够将XML放入HTML文档中,并使用outerHTML将其正确序列化为XML。

如果我将语法稍微更改为

<xsl:sort select="."></xsl:sort>

在http://jsfiddle.net/70swrmjb/然后Chrome给出了一个结果,但当然整个方法是错误的,HTML(既不是HTML4也不是HTML5)不是XML,将XML元素放在其中作为XML处理是不可靠的。从一个适当地用作application/xml的文件中加载XSLT,您肯定不会有任何这样的问题。当然,你还可以遇到其他问题,比如Chrome不支持从文件URL加载XML,除非你明确地以较低的安全设置启动它。但是,只要您通过HTTP加载,并且不对XML和XSLT使用不同的位置,您就可以了。

相关内容

最新更新