<!--
  ApplyEmbeddedStylesheetIE.xsl (1/24/2010)

  Thread "[xsl] question on support of stylesheet embedding"
    http://www.biglist.com/lists/lists.mulberrytech.com/xsl-list/archives/200907/msg00144.html
  identified IE6/7/8 browsers being unable to process embedded stylesheets.

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

  Results in NO performance penalties for Firefox and enables 
  IE browsers for processing embedded stylesheets.

  Samples for embedded stylesheets enabled for IE by this stylesheet:
    http://stamm-wilbrandt.de/en/xsl-list/cdcatalogIE.xml
    http://stamm-wilbrandt.de/en/xsl-list/chess/board5bIE.xml

  The first xml-stylesheet PI href points to this stylesheet for IE.
  The last xml-stylesheet PI href references the embedded stylesheet id.
  [use view (Page) Source in browser to inspect XML files]

  Having an absolute href for the first xml-stylesheet PI allows for
  copying around of XML files with embedded stylesheets while still
  allowing them to be rendered by Firefox and IE browsers anytime.
-->
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:extra="urn:extra-functions"
  extension-element-prefixes="extra"
>

<!--
  "msgeturl.xsl"
  http://stamm-wilbrandt.de/en/xsl-list/xsl-url/msgeturl/
-->
<msxsl:script language="JScript" implements-prefix="extra">
  function msgeturl(nodelist) {
    return nodelist.nextNode().url;
  }
</msxsl:script>

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

<!--
  "DOMParser.parseFromString() in Explorer"
  http://www.van-steenbeek.net/?q=explorer_domparser_parsefromstring
-->
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="extra:msgeturl(.)" />');

  // 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");

  // 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;
}

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

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