Kazzi.NET

CDATA and XHTML Validation

15:16 19/04/2007, in Coding

Recently I had to deal with some validation issues. When your code can not be validated due to some strange characters, or whatever things have to be there, we use CDATA mark to make it ignored by the parser.

The term CDATA (/ˈsi.deɪ.tə/), meaning character data, is used for distinct, but related purposes in the markup languages SGML and XML. The term indicates that a certain portion of the document is general character data, rather than non-character data or character data with a more specific, limited structure.)

http://en.wikipedia.org/wiki/CDATA

For example, if your JavaScript source contains many characters like the greater than (>), less than (<) etc., you have to put it in CDATA bounds to make it validated.

<script type="text/javascript">
<![CDATA[
... your code
]]>
</script>

Some JavaScript compiler will report error on the opening mark <![CDATA[, so just put the double forward slashes in front of them, they will be ignored too. Like this:

<script type="text/javascript">
//<![CDATA[
... your code
//]]>
</script>

Fair enough, huh? You can apply the trick with another language or style, like <!--<![CDATA[--> and <!--]]>--> for HTML, /*<![CDATA[*/ and /*]]>*/ for CSS.


5 Comments »

  1. Viet
    1

    XHTML is not that universal. It causes lots of validation problems. XHTML 1.0 Strict treat the attribute “target” of anchors <A> as invalid. Excessive enthusiasm makes web design less fun!

    Fri 11/Apr/2008 @ 10:10 AM
  2. Nick
    2

    Thanks for this, couldn’t quite remember how to do it!

    Viet, target attribute is invalidate for a reason afaik, it can totally mess up a users experience when used incorrectly (which is the case, in most cases)

    Mon 20/Apr/2009 @ 9:14 PM
  3. Elyza
    3

    Thanks

    Thu 26/Nov/2009 @ 6:53 PM
  4. Louis Carabini
    4

    In HTML their contents are defined as CDATA meaning everything from the start of the contents to the next occurrence of the closing token in this case is considered character data that isnt parsed as markup. However in XHTML their contents are defined as PCDATA meaning the contents are parsed as markup comment directives are hidden ampersands are treated as the beginning of character references marked sections are recognized etc.

    Sun 20/Mar/2011 @ 10:55 AM
  5. Ryan
    5

    Can you use CDATA in an XSL document? I remember vaguely that you could.

    Tue 12/Feb/2013 @ 9:40 AM

Leave a comment

 

RSS Comments RSS

62 queries. 2.258 seconds. Powered by WordPress