Bookamp
Bookamp

Reputation: 682

XSL Transformation working in IE, not in Chrome

I'm having a bit of trouble transforming an xml document using the xsl below. The code works on IE using xmlDoc.transformNode, but the following code does not work in chrome - resultDocument returns 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);

The xml document (xmlDoc.documentElement.outerHTML) that I'm trying to transform is as shown below -

"<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>"

Any ideas? Thanks.

EDIT - I put a jsfiddle together - works in Firefox, but not in Chrome.

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

Upvotes: 0

Views: 667

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167571

Don't expect to be able to put XML into an HTML document and to have it properly serialized back as XML with outerHTML.

If I change the syntax slightly to

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

in http://jsfiddle.net/70swrmjb/ then Chrome gives a result but of course the whole approach is wrong, HTML (neither HTML 4 nor HTML5) is not XML and putting XML elements in there to be processed as XML is not going to work reliably. Load the XSLT from a file properly served as application/xml and you will certainly not have any such problem. Of course there are other issues you can run in, like Chrome not supporting loading XML from file URLs unless you explictly start it with lowered security settings. But as long as you load over HTTP and don't use different locations for XML and XSLT you should be fine.

Upvotes: 2

Related Questions