2

I transform a XHTML dokument to another XML-Dokument with XSLT. In the XHTML-Input-Dokument there are several IE-conditional-comments, like this one:

<!--[if lte IE 7]>
<link rel='stylesheet' href='ie.css' type='text/css' />
<![endif]-->

But while the transformation they get lost... Even if I try only to do an identity-copy:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" version="1.0" encoding="UTF-8" />

    <xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

I only get the link-element without the conditional comment around it.

How can I copy the XHTML-Dokument with the conditional comment?

bceo
  • 319
  • 1
  • 3
  • 5
  • 1
    *I only get the link-element without the conditional comment around it.* - I doubt that. To XSLT, the *whole thing* is a comment, it would not copy only part of it. – Tomalak Oct 23 '11 at 14:27
  • Your error lies elsewhere and not in the code you posted. @Tomalak has right. – FailedDev Oct 23 '11 at 14:34

1 Answers1

2

Even if I try only to do an identity-copy:

...

I only get the link-element without the conditional comment around it.

If this is true, which I doubt, then you are using a very buggy XSLT processor. No compliant XSLT processor will strip out a comment and produce the comment text -- without having the appropriate XSLT instructions (within a template matching comment()).

Of course, I couldn't reproduce this "problem" having tried 6-7 different XSLT processors with this transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

When applied on this XML document (the comment, wrapped in a single top element to become a well-formed XML document):

<html>
<!--[if lte IE 7]> <link rel='stylesheet' href='ie.css' type='text/css' /> <![endif]-->
</html>

the result is exactly the same XML document:

<html>
  <!--[if lte IE 7]> <link rel='stylesheet' href='ie.css' type='text/css' /> <![endif]-->
</html>

Having said that, to generate such a "comment" is a little bit more tricky -- here is a demo how to do this:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" omit-xml-declaration="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
     <html>
       <xsl:text disable-output-escaping="yes">&#xA;&lt;!--[if lte IE 7]> </xsl:text>
       <link rel='stylesheet' href='ie.css' type='text/css' />
       <xsl:text disable-output-escaping="yes"> &lt;![endif]-->&#xA;</xsl:text>
     </html>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied to any XML document (not used in our example), the wanted, correct output is produced:

<html>
<!--[if lte IE 7]> <link rel="stylesheet" href="ie.css" type="text/css"/> <![endif]-->
</html>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431