Skip to content

Instantly share code, notes, and snippets.

@eerohele
Created July 24, 2012 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eerohele/3169736 to your computer and use it in GitHub Desktop.
Save eerohele/3169736 to your computer and use it in GitHub Desktop.
An XSLT stylesheet for adding Paul Irish's conditional IE classes to an XHTML document
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" encoding="utf-8" indent="no" omit-xml-declaration="no"/>
<xsl:variable name="newline"><xsl:text>
</xsl:text></xsl:variable>
<xsl:template match="xhtml:html">
<xsl:value-of select="$newline"/>
<xsl:text disable-output-escaping="yes">
&lt;!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
</xsl:text>
<xsl:value-of select="$newline"/>
<xsl:comment>paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/</xsl:comment>
<xsl:value-of select="$newline"/>
<xsl:comment>[if lt IE 7]&gt; &lt;html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="<xsl:value-of select="@lang"/>"&gt; &lt;![endif]</xsl:comment>
<xsl:value-of select="$newline"/>
<xsl:comment>[if IE 7]&gt; &lt;html class="no-js lt-ie9 lt-ie8" lang="<xsl:value-of select="@lang"/>"&gt; &lt;![endif]</xsl:comment>
<xsl:value-of select="$newline"/>
<xsl:comment>[if IE 8]&gt; &lt;html class="no-js lt-ie9" lang="<xsl:value-of select="@lang"/>"&gt; &lt;![endif]</xsl:comment>
<xsl:value-of select="$newline"/>
<xsl:text disable-output-escaping="yes">&lt;!--[if gt IE 8]&gt;&lt;!--&gt;</xsl:text>
<html class="no-js">
<xsl:apply-templates select="@*"/>
<xsl:text disable-output-escaping="yes">&lt;!--&lt;![endif]--&gt;</xsl:text>
<xsl:value-of select="concat($newline, $newline)"/>
<xsl:apply-templates select="*"/>
</html>
</xsl:template>
<xsl:template match="xhtml:script">
<xsl:variable name="begin-cdata">
<xsl:text>
//&lt;![CDATA[</xsl:text>
</xsl:variable>
<xsl:variable name="end-cdata">
<xsl:text>
//]]&gt;
</xsl:text>
</xsl:variable>
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:value-of select="$begin-cdata" disable-output-escaping="yes"/>
<xsl:value-of select="."/>
<xsl:value-of select="$end-cdata" disable-output-escaping="yes"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment