[XSLT] How to generate /*<![CDATA[*/ for XHTML style element?

V

Vincent Lefevre

I'd like to generate something like that:

<style type="text/css">/*<![CDATA[*/
body { }
*/]]></style>

I thought the following would work, but it doesn't (I tried with
xsltproc).

<?xml version="1.0"?>

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

<xsl:eek:utput method="xml"
encoding="iso-8859-1"
doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
indent="yes"/>

<xsl:template match="/">
<html>
<head>
<title>.</title>
<style type="text/css">
<xsl:text disable-output-escaping="yes">/*&lt;![CDATA[*/
body { }
/*]]&gt;*/</xsl:text>
</style>
</head>
<body><p>.</p></body>
</html>
</xsl:template>

</xsl:stylesheet>

Any idea?
 
B

Bjoern Hoehrmann

* Vincent Lefevre wrote in comp.text.xml:
I'd like to generate something like that:

<style type="text/css">/*<![CDATA[*/
body { }
*/]]></style>

This construct is useful only if the stylesheet contains "]]>", "&", or
"<" and you want both HTML tag soup and XML processors to successfully
process the document; if that is ever the case you should use external
style sheets instead, or, if possible, escape the characters using
\hhhhhh escapes instead.
 
V

Vincent Lefevre

Bjoern Hoehrmann said:
* Vincent Lefevre wrote in comp.text.xml:
I'd like to generate something like that:

<style type="text/css">/*<![CDATA[*/
body { }
*/]]></style>
This construct is useful only if the stylesheet contains "]]>", "&", or
"<" and you want both HTML tag soup and XML processors to successfully
process the document;

Or for better maintainability, e.g. if the styles are incomplete and
one wants to make sure not to forget to add this construct when the
styles get "&" or "<" characters in future modifications.
if that is ever the case you should use external style sheets
instead,

This is the best solution when the page is on a web server, but in
other cases, e.g. when one wants to distribute a HTML file by e-mail,
internal styles may be an easier solution.
or, if possible, escape the characters using \hhhhhh escapes
instead.

But this is less readable and also more prone to bugs when generating
styles from a programming language where \ is the escape character.
 
J

Joe Kesselman

You can tell XSLT that the text children of certain elements should be
escaped as CDATA Sections, via the cdata-section-elements attribute of
the xsl:eek:utput directive.

I agree that this is generally something you should avoid, but if the
customer/boss insists on it...
 
B

Bjoern Hoehrmann

* Joe Kesselman wrote in comp.text.xml:
You can tell XSLT that the text children of certain elements should be
escaped as CDATA Sections, via the cdata-section-elements attribute of
the xsl:eek:utput directive.

That would not work here as the CDATA section delimiters have to be
surrounded by /* ... */ comment delimiters.
 
A

Andy Dingley

Bjoern said:
That would not work here as the CDATA section delimiters have to be
surrounded by /* ... */ comment delimiters.

Why does <style> have to be a CDATA section anyway? (ignoring the
bogosity of the /* <![CDATA[ ... ]]> */ construction)

<script> has some arguments for it, in some particular circumstances,
but <style> doesn't.
 
J

Joe Kesselman

Bjoern said:
That would not work here as the CDATA section delimiters have to be
surrounded by /* ... */ comment delimiters.

In that case, assembling it out of characters -- and outputting through
a path which suppresses normal XML escaping -- may indeed be the best
you can do. Or you can perform non-XML-based postprocessing to replace
the <![CDATA[]]> delimiters with their Javascript-commented-out equivalents.

The better answer remains: Don't Do That. The whole point of XHTML is
that it is XML-based, and that *should* mean you don't have to kluge
using <![CDATA[]]> and comments to make it accept scripts. If that isn't
true, beat up the browser authors until they fix their code.
 
A

Andy Dingley

Joe said:
The better answer remains: Don't Do That. The whole point of XHTML is
that it is XML-based, and that *should* mean you don't have to kluge
using <![CDATA[]]> and comments to make it accept scripts.

It's no an XHTML problem, it's a problem for appendix C XHTML which is
being fed to SGML parsers and expected to work transparently as
_either_ XML or SGML, from the same document. The addition of the
comment markers is a particularly obsessive case. Hixie is obsessed
with this, no-one else really cares.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top