XSL Transformation在IE中运行,而不是在Chrome中运行

时间:2014-09-24 16:04:22

标签: xml xslt

我在使用下面的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.documentElement.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>"

有什么想法吗?感谢。

编辑 - 我把一个jsfiddle放在一起 - 在Firefox中运行,但在Chrome中不运行。

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

1 个答案:

答案 0 :(得分:2)

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

如果我稍微改变语法

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

http://jsfiddle.net/70swrmjb/中,Chrome会给出结果,但当然整个方法都是错误的,HTML(HTML 4和HTML5都不是)不是XML,而是将XML元素放在那里进行处理,因为XML无法正常工作可靠。从正确作为application / xml的文件加载XSLT,你肯定不会有任何这样的问题。当然,您可以运行其他问题,例如Chrome不支持从文件URL加载XML,除非您明确地使用降低的安全设置启动它。但只要你加载HTTP并且不使用XML和XSLT的不同位置就可以了。

相关问题