Home > Articles > Web Services > XML

  • Print
  • + Share This
From the author of

CDATA Sections

The SVG example in Listing 2 exposed us to a new XML component, the Document Type Declaration, and we reviewed how DTDs are used to define document structure and entities. Let’s close out our first step toward XML mastery by discussing an XML component, the CDATA section, that comes into play when trying to include text with characters that will upset an XML parser. I discussed how entity references can be used to substitute for problematic characters such as <, >, and &, but CDATA takes that assistance one step further—by saying, in effect, "Parser, don’t try to decode any of this. Just leave it as is." Thus, if your text contains a lot of < or & characters (as program code often does), your text can be moved inside a CDATA section.

A CDATA section starts with <![CDATA[ and ends with ]]>. Listing 4 shows an XML document containing some JavaScript placed in a CDATA section.

Listing 4 An XML CDATA section illustrating how code can be delivered within an XML document.

    var SVGDoc;
    var groups = new Array();
    var last_group;

    *  init
    *  Find this SVG’s document element
    *  Define members of each group by ID
    function init(e) {
      SVGDoc = e.getTarget().getOwnerDocument();
      append_group(1, 4, 6); // group 0
      append_group(5, 4, 3); // group 1
      append_group(2, 3);  // group 2

In the example in Listing 4, all the code inside the CDATA section is ignored by the parser, despite the brackets and & symbols. The CDATA section is quite monolithic, with only a few rules:

  • A CDATA section cannot contain the string "]]>"; therefore, nested CDATA sections are not allowed.
  • No spaces or line breaks are allowed inside the ]]> string.
  • + Share This
  • 🔖 Save To Your Account