[xsl] support of stylesheet embedding for ALL browsers

Subject: [xsl] support of stylesheet embedding for ALL browsers
From: Hermann Stamm-Wilbrandt <STAMMW@xxxxxxxxxx>
Date: Mon, 22 Feb 2010 22:14:06 +0100
Stylesheet embedding is not supported for Internet Explorer 6/7/8 browsers.

Posting

http://www.biglist.com/lists/lists.mulberrytech.com/xsl-list/archives/201001/msg00390.html
provided a work around -- stylesheet "ApplyEmbeddedStylesheetIE.xsl"
allowed
IE and FF browsers to process embedded stylesheets.

Although 80-90% of browser clients were supported by that solution this was
a bit unfair because with that technique browsers other than IE which were
able to deal with embedded stylesheets were disrupted.


Below stylesheet provides processing of embedded stylesheets for all
browsers.
Verified for several versions of Chrome/Firefox/Internet
Explorer/Opera/Safari
on Linux/Mac/Win, see here:
  http://stamm-wilbrandt.de/en/xsl-list/supportALL.xml


ApplyEmbeddedStylesheet.xsl (in case of disruptions by sending this email):
  http://stamm-wilbrandt.de/en/xsl-list/ApplyEmbeddedStylesheet.xsl

<!--
  ApplyEmbeddedStylesheet.xsl (2/22/2010)

  Posting
    http://www.biglist.com/lists/lists.mulberrytech.com/xsl-list/
    archives/201001/msg00390.html
  showed how to enable Internet Explorer 6/7/8 browsers for
  processing of embedded stylesheets.

  While that solution was fine for Firefox and IE browsers, it disabled
  processing of embedded stylesheets for other browsers although these
  browsers support processing of embedded stylesheets.

  This stylesheet overcomes the problems and provides processing of
  embedded stylesheets for ALL browsers (including IE).

  While FF browsers always selects the last xml-stylesheet PI
  IE browsers and other non-FF browsers always select the first one;
  this allows for a browser switch on xml-stylesheet PI level.

  Results in NO performance penalties for FF browsers and enables IE
browsers
  for processing embedded stylesheets while not disrupting other browsers.

  Samples for embedded stylesheets enabled for all browsers:
    http://stamm-wilbrandt.de/en/xsl-list/supportALL.xml
    http://stamm-wilbrandt.de/en/xsl-list/cdcatalogALL.xml
    http://stamm-wilbrandt.de/en/xsl-list/chess/board5bALL.xml
    http://stamm-wilbrandt.de/en/xsl-list/devshedALL.xml

  The first xml-stylesheet PI href points to this stylesheet (non-FF
browsers).
  Its (appended) query references the XML file itself.
  The last xml-stylesheet PI href references the embedded stylesheet id
(FF).

  [use view (Page) Source in browser to inspect XML files]
-->
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>

<xsl:template match="/">
<html>
<head>
<script>

<!--
  "DOMParser.parseFromString() in Explorer"
  http://www.van-steenbeek.net/?q=explorer_domparser_parsefromstring
-->
if(typeof(DOMParser) == 'undefined') {
  DOMParser = function() {}
  DOMParser.prototype.parseFromString = function(str, contentType) {
    if(typeof(ActiveXObject) != 'undefined') {
      var xmldata = new ActiveXObject('MSXML.DomDocument');
      xmldata.async = false;
      xmldata.loadXML(str);
      return xmldata;
    } else if(typeof(XMLHttpRequest) != 'undefined') {
      var xmldata = new XMLHttpRequest;
      if(!contentType) {
        contentType = 'application/xml';
      }
      xmldata.open('GET', 'data:' + contentType + ';charset=utf-8,' +
encodeURIComponent(str), false);
      if(xmldata.overrideMimeType) {
        xmldata.overrideMimeType(contentType);
      }
      xmldata.send(null);
      return xmldata.responseXML;
    }
  }
}

<!--
  "Transforming XML to XHTML in the Browser"
  http://www.w3schools.com/xsl/xsl_client.asp
-->
function loadXMLDoc(dname)
{
  if (window.XMLHttpRequest)
  {
    xhttp=new XMLHttpRequest();
  }
  else
  {
    xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xhttp.open("GET",dname,false);
  xhttp.send("");
  return xhttp.responseXML;
}

<!--
  "Transforming XML to XHTML in the Browser"
  http://www.w3schools.com/xsl/xsl_client.asp

  modified to do two transformations in series
-->
function displayResult()
{
  // get parser
  var parser = new DOMParser();

  // get input XML document (the ONLY non-static part of this stylesheet!)
  xml=loadXMLDoc('<xsl:value-of select="substring-before(substring-after
(substring-after(/processing-instruction()[local-name
()='xml-stylesheet'][1],'href=&quot;'),'?'),'&quot;')" />');

  // stylesheet to cut out the embedded stylesheet
<![CDATA[
cutxslstr="<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template
match='/'><xsl:copy-of select=\"//*[attribute::*[local-name
()='id']=substring-before(substring-after(/processing-instruction
()[local-name()='xml-stylesheet' and contains
(.,'href=&quot;#')],'href=&quot;#'),'&quot;')]\"/>
</xsl:template></xsl:stylesheet>";
]]>
  cutxsl = parser.parseFromString(cutxslstr, "text/xml");


  // code for IE
  if (window.ActiveXObject)
  {
    // apply cut out stylesheet to retrieve embedded stylesheet
    xslstr=xml.transformNode(cutxsl);
    xsl = parser.parseFromString(xslstr, "text/xml");

    // apply embedded stylesheet to XML document
    result=xml.transformNode(xsl);

    // display result
    document.getElementById("example").innerHTML=result;
  }
  // code for Mozilla, Firefox, Opera, etc.
  else if (document.implementation)
  {
    // apply cut out stylesheet to retrieve embedded stylesheet
    xsltproc1 = new XSLTProcessor();
    xsltproc1.importStylesheet(cutxsl);
    xsl = xsltproc1.transformToDocument(xml);

    // apply embedded stylesheet to XML document
    xsltproc2 = new XSLTProcessor();
    xsltproc2.importStylesheet(xsl);
    result = xsltproc2.transformToFragment(xml, document);

    // display result
    document.getElementById("example").appendChild(result);
  }
}

</script>
</head>
<body onload="displayResult()">
<div id="example"/>
</body>
</html>

</xsl:template>
</xsl:stylesheet>



Mit besten Gruessen / Best wishes,

Hermann Stamm-Wilbrandt
Developer, XML Compiler, L3
WebSphere DataPower SOA Appliances
----------------------------------------------------------------------
IBM Deutschland Research & Development GmbH
Vorsitzender des Aufsichtsrats: Martin Jetter
Geschaeftsfuehrung: Dirk Wittkopp
Sitz der Gesellschaft: Boeblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294

Current Thread