Re: [xsl] IE's conditional comments in xslt

Subject: Re: [xsl] IE's conditional comments in xslt
From: "Manfred Staudinger" <manfred.staudinger@xxxxxxxxx>
Date: Sun, 5 Mar 2006 06:14:28 -0800
On 04/03/06, M. David Peterson <m.david@xxxxxxxxxx> wrote:
> Setting this portion aside, I am now intrigued to hear about what it is
> you are attempting, and how you are proposing to solve this.  I think I
> originally assumed too much as to what it seemed to be you were trying
> to solve.

This is about generating conditional comments with xslt on client-side.

Every xslt enabled browser has a standard css 2.1 support, except
IE 6, IE 5.5 and IE 5.01. I'm going to discuss here IE 6 only, as the
extensions necessary to serve IE 5.5 or IE 5.01 are straightforward.
It was and is considered good practice to use the following html:

<link rel="stylesheet" href="all_browsers.css" type="text/css" />
<!--[if IE 6]>
   <link rel="stylesheet" href="hacks_for_ie6.css" type="text/css" />
<![endif]-->

But if you really wanted to use for example css 2.1 descendant selectors,
you are at a loss with this model. Without client-side xslt, the only
solution is to use server-side processing to deliver
a) for non-IE browsers plus IE 7
<link rel="stylesheet" href="css_2_1_browsers.css" type="text/css" />
a) for IE browsers except IE 7
<link rel="stylesheet" href="all_browsers.css" type="text/css" />
<!--[if IE 6]>
   <link rel="stylesheet" href="hacks_for_ie6.css" type="text/css" />
<![endif]-->

For the xslt enabled browser, you can do exactly the same with this
client-side xslt:
<xsl:comment><![CDATA[[if IE]><![if IE 7]><![endif]]]></xsl:comment>
	<link rel="stylesheet" href="css_2_1_browsers.css" type="text/css" />
<xsl:comment><![CDATA[[if IE]><![endif]><![endif]]]></xsl:comment>
<xsl:if test="system-property('xsl:vendor')='Microsoft'">
	<xsl:comment><![CDATA[[if IE]><![if lte IE 6]><![endif]]]></xsl:comment>
		<link rel="stylesheet" href="all_browsers.css" type="text/css" />
	<xsl:comment><![CDATA[[if IE]><![endif]><![endif]]]></xsl:comment>
	<xsl:comment><![CDATA[[if IE]><![if IE 6]><![endif]]]></xsl:comment>
		<link rel="stylesheet" href="hacks_for_ie6.css" type="text/css" />
	<xsl:comment><![CDATA[[if IE]><![endif]><![endif]]]></xsl:comment>
</xsl:if>

An other example would be to add a div element to the body to simulate
position fixed for IE 5.5 and IE 5.01. You should note also that all the
"conditional" html gets parsed and is subject to the normal xslt transform.

Thinking about David's example and why I did not even try that.
When I use (browser-side, IE 6, MSXML 3):
         <xsl:comment>[if IE 6]>
         <style type="text/css">
           h1 {color: red}
         </style>
         &lt;![endif]</xsl:comment>
nothing happened; Saxon 6.5.4 gives the explanation: Recoverable error,
Non-text output nodes are ignored when writing an attribute, comment, or PI.

So back to David's example (simplified here) again, adding CDATA and
changing &lt to < (browser-side, IE 6, MSXML 3):
         <xsl:comment><![CDATA[[if IE 6]>
         <style type="text/css">
           h1 {color: red}
         </style>
         <![endif]]]></xsl:comment>
and now it works! So IE 6 created an element node out from CDATA-text,
this seems to me to be a non-conformant behaviour.

Manfred

Current Thread