[xsl] XSL stylesheet embedding for all browsers

Subject: [xsl] XSL stylesheet embedding for all browsers
From: "Valery Chernysh" <spy00000@xxxxxxxxx>
Date: Wed, 17 Aug 2011 12:14:26 +0400
Hello!!!

I'd like  to show you some technique to place XML data and XSLT stylesheet
in one file so, that transformation work in all browsers, which support XSL
transformation (Internet Explorer, FireFox, Chrome, Safari, Opera).

As we know, there is a standard way to embed xslt stylesheet in xml files,
described in "XSL Transformations (XSLT) Version 1.0" specification in
paragraph 2.7 "Embedding Stylesheets"
(http://www.w3.org/TR/xslt#section-Embedding-Stylesheets), and this does not
work in Internet Explorer (including version 9). Jeni Tennison  in her post
(http://www.biglist.com/lists/lists.mulberrytech.com/xsl-list/archives/20020
5/msg00146.html) describes the way for IE to embed the XML data within a
XSLT stylesheet. This is a very interesting method and in fact it works not
only in IE, but in other browsers (FireFox, Chrome, Safari, Opera). And
also, this method can be slightly modified for better using.

There is an inconvenience in this method  that in processing instruction in
href attribute we need to specify file name, and if file name does not match
name in PI (for example, if someone change name of the file, while saving),
then transformation stops working. I performed some experiments and found
that if the file extension is .xml, not .xsl (it's important for IE) and
specify PI in form of <?xml-stylesheet type="text/xsl" href="#"?> (without
any symbols between # and "), then XSLT stylesheet for transformation will
be expected in root node of XML document, and transformation will work in
all browsers. So for using XSLT stylesheet embedded in XML file we can use
next file structure:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="#"?>

<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:data="http://www.example.com/";>
  <data:data>
    <doc>
      <!-- place for data -->
    </doc>
  </data:data>

  <xsl:template match="/xsl:stylesheet">
    <xsl:apply-templates select="data:data/*" />
  </xsl:template>

  <xsl:template match="doc">
  </xsl:template>

</xsl:stylesheet>

I would like to thank Hermann Stamm-Wilbrandt for his help and for his
example of using <xsl:variable> to reduce the path to data node (you can see
this example in file supportALL.xml).

Example 1: http://stamm-wilbrandt.de/en/xsl-list/Valery_Chernysh/table.xml
Example 2:
http://stamm-wilbrandt.de/en/xsl-list/Valery_Chernysh/supportALL.xml

A few comments on referencing to XSLT stylesheet from processing
instruction.

1. For referencing to XSLT stylesheet we can use standard way from XSLT
specification (with id="idstr" attribute in <xsl:stylesheet> and
href="#idstr" in PI), but this works in all browsers except Safari. As I
understand from my experiments, in files with embed stylesheet Safari "hide"
<xsl:stylesheet> node used for transformation, and it is impossible to
utilize  the data placed inside <xsl:stylesheet> in Safari.

2. In IE referencing to XSLT stylesheet in form href="#style1" and href="#"
works in the files with the .xml extension, and does not work in .xsl files.
In other browsers such reference works both in .xml and .xsl files.


with best regards
Valery Chernysh
spy00000@xxxxxxxxx

Current Thread