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?

4

1 回答 1

2

即使我只尝试进行身份复制:

...

我只得到没有条件注释的链接元素。

如果这是真的,我对此表示怀疑,那么您使用的是一个非常有问题的 XSLT 处理器。没有兼容的 XSLT 处理器会去掉注释并生成注释文本——没有适当的 XSLT 指令(在模板匹配内comment())。

当然,我无法通过这种转换尝试 6-7 个不同的 XSLT 处理器来重现这个“问题”:

<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>

当应用于此 XML 文档(注释,包装在单个顶部元素中以成为格式良好的 XML 文档)时:

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

结果是完全相同的 XML 文档

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

话虽如此,生成这样的“评论”有点棘手——这里有一个演示如何做到这一点:

<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>

当此转换应用于任何 XML 文档(在我们的示例中未使用)时,将生成所需的正确输出

<html>
<!--[if lte IE 7]> <link rel="stylesheet" href="ie.css" type="text/css"/> <![endif]-->
</html>
于 2011-10-23T15:08:02.530 回答