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

Discussion in 'XML' started by Vincent Lefevre, Jun 17, 2006.

  1. 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?

    --
    Vincent Lefèvre <> - Web: <http://www.vinc17.org/>
    100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
    Work: CR INRIA - computer arithmetic / SPACES project at LORIA
     
    Vincent Lefevre, Jun 17, 2006
    #1
    1. Advertising

  2. * 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.
    --
    Björn Höhrmann · mailto: · http://bjoern.hoehrmann.de
    Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
    68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
     
    Bjoern Hoehrmann, Jun 17, 2006
    #2
    1. Advertising

  3. In article <>,
    Bjoern Hoehrmann <> wrote:

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

    --
    Vincent Lefèvre <> - Web: <http://www.vinc17.org/>
    100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
    Work: CR INRIA - computer arithmetic / SPACES project at LORIA
     
    Vincent Lefevre, Jun 17, 2006
    #3
  4. 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...

    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Jun 21, 2006
    #4
  5. * 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.
    --
    Björn Höhrmann · mailto: · http://bjoern.hoehrmann.de
    Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
    68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
     
    Bjoern Hoehrmann, Jun 21, 2006
    #5
  6. Vincent Lefevre

    Andy Dingley Guest

    Re: How to generate /*<![CDATA[*/ for XHTML style element?

    Bjoern Hoehrmann wrote:

    > 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.
     
    Andy Dingley, Jun 21, 2006
    #6
  7. Bjoern Hoehrmann wrote:
    > 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.

    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Jun 22, 2006
    #7
  8. Vincent Lefevre

    Andy Dingley Guest

    Re: How to generate /*<![CDATA[*/ for XHTML style element?

    Joe Kesselman wrote:

    > 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.
     
    Andy Dingley, Jun 22, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Davison
    Replies:
    1
    Views:
    636
    Hal Rosser
    Jul 7, 2004
  2. Ralf Schneider

    XHTML: <![CDATA[..script..]]>

    Ralf Schneider, Aug 9, 2003, in forum: HTML
    Replies:
    4
    Views:
    599
  3. Dave Matthews

    Detecting CDATA sections with XSLT

    Dave Matthews, Jun 18, 2004, in forum: XML
    Replies:
    2
    Views:
    743
    Dave Matthews
    Jun 19, 2004
  4. Replies:
    3
    Views:
    799
    Joe Kesselman
    Mar 6, 2006
  5. Usha2009
    Replies:
    0
    Views:
    1,174
    Usha2009
    Dec 20, 2009
Loading...

Share This Page