04 March 2005

XSL and Conditional Comments

I have just had a killer 2 hours trying to make my XSL translate this XML:

<base> 
	<item name="cssFiles" media="screen"> allBrowers.css</item> 
	<item name="cssFiles" media="screen" ieonly="true"> ieOnly.css</item> 
</base>  

Into this XHTML:

<style type="text/css" media="screen">  
	@import url("allBrowers.css"); 
</style> 

<!--[if gte ie 5.5000]>  
	<style media="screen" type="text/css"> 
 		@import url("ieOnly .css"); 
	</style>  
<![endif]-->    

You will all recognise of course, the above, as a style sheet inside a Conditional Comment. Used commonly in the web design world to give that ‘little extra' CSS that is needed to make things work in IE sometimes.

As part of the separation of Data and Structure, my XML file contains the list of CSS files to be used in a page (and other data such as navigation, page content). Then I use XSL to translate my XML Data into an XHTML page to be served.

BUT, XSL has a little thing with comments. It doesn't output XML comments (that's <!—something --> ) from the XSL document. Instead, if you wish a comment to be in your output XHTML you have to use <xsl:comment> . So this didn't work:  

<xsl:for-each select="base/item[@name='cssFiles']">  
	<xsl:choose>  
		<xsl:when test="@ieonly">  
			<!--[if gte ie 5.5000]>  
				<style type="text/css" media="{@media}">  
					@import url("<xsl:value-of select="." /> "); 
				</style>  
			<![endif]-->  
		</xsl:when>  
		<xsl:otherwise>  
			<style type="text/css" media="{@media}">  
				@import url("<xsl:value-of select="." /> "); 
			</style>  
		</xsl:otherwise>  
	</xsl:choose>  
</xsl:for-each>    

Nought wrong with that I suppose, since if there is a comment in the XSL, surely it's only meant to be in the XSL. Good idea not to include it I say. And if I HAVE to have a comment in the XHTML, I can use <xsl:comment> yeah? Well, yes, but it's not that easy. See for example, I first tried this (about an hour after realising that I had to use <xsl:comment> ):  

<xsl:for-each select="base/item[@name='cssFiles']">  
	<xsl:choose>  
		<xsl:when test="@ieonly">  
			<xsl:comment> [if gte ie 5.5000]>  
				<style type="text/css" media="{@media}">  
					@import url("<xsl:value-of select="." /> "); 
				</style>  
			<![endif]</xsl:comment>  
		</xsl:when>  
		<xsl:otherwise>  
			<style type="text/css" media="{@media}">  
				@import url("<xsl:value-of select="." /> "); 
			</style>  
		</xsl:otherwise>  
	</xsl:choose>  
</xsl:for-each>  

Looks like it should work, but it doesn't. The <! at the <![endif] throws the XSL into a wobbly, because that is used in DOCTYPES. I almost gave up at this point, but instead I tried replacing the <! with &lt;!. I don't know why I tried it, but it worked. No more error! Well, it kind of worked. Now the <style> tag is missing all together, and all you're left with is:  

<style type="text/css" media="screen">  
@import url("engvocabEN.css"); 
</style> 

<!--[if gte ie 5.5000]>  
<![endif]-->    

Again, at the point of giving up, and after trying a few different, pointless things, I thought about my method for dealing with the <! problem. Then I went about changing all the Greater Than (> ) and Less Than (<) symbols of the <style> tag into &gt; and &lt; prospectively. Giving me this:  

<xsl:for-each select="base/item[@name='cssFiles']">  
	<xsl:choose>  
		<xsl:when test="@ieonly">  
			<xsl:comment> [if gte ie 5.5000]>  
				&lt;style type="text/css" media="<xsl:value-of select="@media" /> "&gt;
					@import url("<xsl:value-of select="." /> "); 
				&lt;/style&gt; 
			&lt;![endif]</xsl:comment>  
		</xsl:when>  
		<xsl:otherwise>  
			<style type="text/css" media="{@media}">  
				@import url("<xsl:value-of select="." /> "); 
			</style>  
		</xsl:otherwise>  
	</xsl:choose>  
</xsl:for-each>   

A little messy sure, but the output:  

<style type="text/css" media="screen">  
	@import url("allBrowers.css"); 
</style> 

<!--[if gte ie 5.5000]>  
	<style media="screen" type="text/css">  
		@import url("ieOnly .css"); 
	</style>  
<![endif]-->   

Finally! And now hopefully, you will never have to solve this problem either!

Comments ( 7 )

  1. Chris Parker

    Somewhat relevant... or not

    So i take it your having some success using XSLT? Were still in the market for a good content management system for our office. Its a toss up between buying off the shelf or writing our own. Theres a pretty good one called Serena Collage that we came across this morning.

    You were interested in how sites look on a mac the last time we spoke. Ive posted a mini-review of Mozilla's Camino on my site that you may find interesting:

    http://www.cmpdesigns.com/sitenews.asp
    04 March 2005 at 15:49
  2. Phil Baines

    Yeah, we use XSLT quite a bit now. Separate data and structure...keeps things nice. It's good for the CMS, because clients can edit the main content, which is stored in a database and called in XML format. But they can't mess up the design of the site too much.

    You still have to give them some style guides though...otherwise they end up dropping ultra large images into their content and such like.

    The next step for our CMS is to introduce page history, so that pages can be rolled back to earlier version if there is a mistake, and to put some sort of editorial control over updates, so that each update have to be given the 'ok' by a trained member of staff, with a good understanding of the style guide.
    04 March 2005 at 16:05
  3. Eivind

    Why different stylesheets?

    Since I don't know how your system work's I may have missed something, but why serve different stylesheets depending on browser? Those hack's needed for IE could easily be wroted into the same stylesheet.
    05 March 2005 at 13:39
  4. Phil Baines

    @Eivind

    Well, first, I am a bit of a perfectionist, and I don't want to make my initial code messy with hacks put in there to make IE work. Second, some IE hacks contain invalid code, placing this code in a hidden style sheet that only IE will see means that my valid code doesn't suffer for these hacks, and my website will still validate. The validator will still see valid code, and so will all other browsers not including IE.

    To me, it just seems like the best way of doing things. In the future, when we don't have to worry about IE Hacking (A long time off I agree), then these hacks wont be causing a problem - because they were put in a place that makes them easily removable and they can also be set only to apply to IE 6 and lower.
    07 March 2005 at 09:17
  5. Jennifer Grucza

    I ran into the same problem, but got around it with a CDATA section:

    <xsl:comment> <![CDATA]> </xsl:comment>
    14 March 2005 at 18:33
  6. Phil Baines

    Yeah, i did try that. but you can't use <xsl:value-of select="." /> within CDATA it seems.
    15 March 2005 at 10:38
  7. The Wolf

    XSL is great if you want to advance your hair loss :)

    Chris, I've been looking for systems like that, have not found any worh using mind you.

    For my simple projects I tend to use PHP5 and SimpleXML instead of using XSL.
    24 April 2005 at 12:28

Sorry, commenting has been disabled for a while. I am getting a stupid amount of comment spam, and need to find a new way of doing things.

I will not publish your email address, but I may use it to get in contact with you.

HTML tags and entities display as source; they do not render. To create a live link, simply type the URL (including http://).

Topics

  • I am more than willing to admit the big gaps in my knowledge on many topics, including web development. This is a topic I use to get help from the community, and my imaginary audience.
    Ask the Audience
  • Even though looking pretty is not the most important layer to a succesful web site (I don't think there is a Most Important), it does help!
    Graphic Design
  • Have you got a sense of?
    Humor
  • The glam, the sadness, the joy, the madness.
    Life of Findel
  • What ever doesn't fit
    Misc
  • I have a crappy camera, but I do try. A good artist always blames his tools.
    Photography
  • Here I chat about projects I am working on, going to work on, or finished. Some of them I will be happy to let you in on, others I will be ashamed of.
    Project Watch
  • Site news and updates
    Site Info
  • Keepin' an eye on the wwwubb
    Watching The Web
  • This topic will be used for talking about all manner of project management issues relating to Wed Development (Wubb Development). Where we may talk about how to code in the Wubb Development topic, we will be more likely to talk about giving quotes, or planning projects, in this topic.
    Working with Wubbs
  • All things Web Development - Web standards, Hacks, Graphics, and more.
    Wubb Development