Search
Web Braintrove
Site Navigation
Home
Products
Tags
Levels
Dates
Authors
5 MOST RECENT
Create Shared Rules
Perform a Case-Insensitive String Comparison without Using Translate()
Force a Field Value to a Boolean Using Rules
Quickly Crop to a Shadow
Sum Repeating Time Values
5 MOST POPULAR
Pass Query String Parameters to an ASP.NET Xml Control
Access a Method in a Master Page with Code-Behind
Prevent Namespace Prefixes from Being Copied to the Output
Create Groups in a Drop-Down List Box
Change the Default Action of the Power Button on the Windows Vista Start Menu
5 MOST FORGOTTEN
Recover a Dark Picture with a Heavy Color Cast
Hide Deleted Items in the Source Control Explorer
Prevent Windows Vista from Requesting Your Permission to Continue
Create a Variable with a Unique Sorted Node-Set
Get the Oldest or Newest Date Value of a List Node
Books
Sams Teach Yourself Web Publishing with HTML & XHTML in 21 Days
Dynamic HTML: The Definitive Reference
XSLT: Programmer's Reference
New Perspectives on HTML and XHTML, Comprehensive
creative html design.2
Magazines
.net: The Internet Magazine
Inside Visual Basic
Practical Web Design
Inside Web Development
SQL Server Magazine
Microsoft MVP
This article has been viewed 595 times.

Embed HTML Script in XSLT

Written by Gregory Scot Collins
Thursday, 11 May 2006, 6:48 AM
This article has been tested to work with the following products and versions. No guarantee of compatibility, with or without modification, is offered for products or versions other than those listed.
When transforming to an HTML document that includes script you might encounter an error because code typically contains characters considered illegal to XML parsers (see Table 1). These characters must either be manually escaped or contained within a CDATA section. All content within a CDATA section is ignored by the parser, meaning it will be interpreted strictly as text and not as markup, thus automatically escaping the characters listed in rows 1-3 of Table 1.
The characters in the first two rows of Table 1 are strictly illegal in XML, meaning that they must be escaped either manually by substitution with their associated entity reference, or by inclusion within a CDATA section. This is because the parser interprets the < character as the start of a new element, and the & character as the start of a character escape sequence. While it is not required that the remaining three characters be escaped, it is considered a best practice to do so.

How to use a CDATA section

A CDATA section begins with <![CDATA[ and ends with ]]>. Listing 1 is an example of how to correctly use a CDATA section to output a block of JavaScript code from a transform:

Output markup within a CDATA section

Since CDATA sections are used to escape blocks of text containing characters that would otherwise be recognized as markup, if follows that you cannot output XML values directly within the CDATA section as you would elsewhere. For example, an <xsl:value-of/> tag would be interpreted as text, thus frustrating your intentions. To accomplish this you must explicitly close the CDATA section, include your markup, and then reopen the CDATA section, as shown in Listing 2:
Bookmark this Article
StumbleUpon  Stumble It!
Digg  Digg It!
del.icio.us  del.icio.us
List of Figures
Table 1 - The five predefined XML entity references.
Listing 1 - Use a CDATA section when outputting blocks of code.
Listing 2 - Interupt the CDATA section to output markup.
Article Tags
Great Deals
TigerDirect Exclusive Deals, Limited Time Offers, Act Now And Save!
Find all current special offers on Adobe products.
Try SugarSync Free!
Join WebHost4Life.com
TigerDirect
Computers4SURE (4SURE.com - An Office Depot Co.)
Copyright © 2006-2010 Braintrove. All rights reserved. Braintrove, braintrove.com, and the Braintrove logo are trademarks of Gregory Scot Collins in the United States and/or other countries. The names of actual companies and products mentioned herein may be the trademarks of their respective owners. Any rights not expressly granted herein are reserved.