1

I'm having an issue that I just can't seem to figure out. I built a website not too long ago in HTML and have recently integrated Symphony CMS and have had to change everything to XML.

Originally in my head, I had a internet explorer specific stylesheet, the head looked something like this:

<head>
       <link rel="stylesheet" href="../css/master.css" type="text/css" media="screen"></link>
       <!--[if IE]>
             <link rel="stylesheet" type="text/css" href="../css/ie.css"></link>
             <script src="../js/html5shiv.js"></script>
       <![endif]-->
</head>

Since switching, this conditional comment does no longer work, I've changed it to this but unfortunately, my master.css is getting ignored for Chrome/Firefox etc... It is just loading the ie.css stylesheet for all browsers.

<head>
    <link rel="stylesheet" href="../css/master.css" type="text/css" media="screen"></link>                
    <xsl:comment>[if IE]<![CDATA[><!]]></xsl:comment>
          <link rel="stylesheet" type="text/css" href="../css/ie.css"></link>
          <script src="../js/html5shiv.js"></script>
    <xsl:comment><![CDATA[<!]]>[endif]</xsl:comment>    
</head>

Sorry I'm fairly new at this and I'm just not sure what I'm doing wrong, I'm guessing I might need some sort of xsl:if comment but just not sure how to go about it really. I just need something that will make chrome/firefox/opera/safari ignore the ie.css stylesheet.

Any help would be greatly appreciated! Thanks

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
LT86
  • 635
  • 2
  • 15
  • 29

2 Answers2

8

Just use one xsl:comment and wrap all of the contents in <![CDATA[]]>...

    <head>
        <link rel="stylesheet" href="../css/master.css" type="text/css" media="screen"></link>
        <xsl:comment><![CDATA[[if IE 6]>
         <link rel="stylesheet" type="text/css" href="../css/ie.css"></link>
         <script src="../js/html5shiv.js"></script>
   <![endif]]]></xsl:comment>
    </head> 
Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
  • Hi thanks for the suggestion, I'll give this a try this evening and let you know if it works :) – LT86 Dec 05 '12 at 16:50
  • Thanks so much! I'd already tried the above but it appears it was my CDATA closing tag that was incorrect. You fixed my issue many thanks! :) – LT86 Dec 05 '12 at 19:12
  • 1
    Accepted! Sorry I'm fairly new to this site :) – LT86 Dec 07 '12 at 11:04
  • how to get a dynamic variable value inside a comment – Susheel Singh Oct 08 '13 at 13:01
  • 1
    Daniel's fix worked for me. One thing that tricked me up was that initially I had the on a separate line to <![CDATA[[if IE 6]>. When I did this the browser was unable to interpret the conditional comment, which confused me for a while. – ashario Jun 16 '15 at 00:55
2

Use a template to allow conditional comments to be defined programmatically:

<xsl:template name="conditionalComment">
    <xsl:param name="qualifier" select="'IE'"/>
    <xsl:param name="contentRTF" select="''" />


    <!--Use entity variables to allow invalid XML output from an XSLT processor-->
    <xsl:comment>[if <xsl:value-of select="$qualifier"/>]<![CDATA[>]]>
    <!--Use copy-of rather than value-of to preserve tag delimiters-->
        <xsl:copy-of select="$contentRTF" />
    <!--Use CDATA to output raw characters-->
        <![CDATA[<![endif]]]></xsl:comment>

</xsl:template>

The template takes two parameters:

<xsl:call-template name="conditionalComment">
    <!--Conditional check parameter-->
    <xsl:with-param name="qualifier" select="'lte IE 6'"/>
    <!--Stylesheet parameter-->
    <xsl:with-param name="contentRTF">
        &lt;link rel="stylesheet" type="text/css" href="ie-win-fixup.css" /&gt;
    </xsl:with-param>
</xsl:call-template> 

References

Community
  • 1
  • 1
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265