Search
Web Braintrove
Site Navigation
Home
Products
Tags
Levels
Dates
Authors
5 MOST RECENT
Locate the Design Checker Task Pane in InfoPath 2010
Locate Conditional Formatting and Custom Validation in InfoPath 2010
Conditionally Display Different Values In a Single Expression Box
Create a Button With a Blank Label
Create Shared Rules
5 MOST POPULAR
Access a Method in a Master Page with Code-Behind
Pass Query String Parameters to an ASP.NET Xml Control
Prevent Namespace Prefixes from Being Copied to the Output
Create Groups in a Drop-Down List Box
Configure IIS 7 for Custom Error Pages
5 MOST FORGOTTEN
Install Windows 7 Beta onto a Virtual Machine
Group Dates by Year, Month and Day
Configure Lab Color Mode Defaults for Braintrove Articles
Create a Monochromatic Dream Text Effect Using Styles
Combine Multiple Sums Into a Single Function
Books
XSLT and XPATH: A Guide to XML Transformations
Teach Yourself Visually HTML
Web Design with XML: Generating Webpages with XML, CSS, XSLT and Format
The Guru's Guide to SQL Server Stored Procedures, XML, and HTML
HTML and XHTML Pocket Reference
Magazines
Inside Web Design
MSDN Magazine
Net Developers Journal
Visual Studio Magazine
Practical Web Design
Microsoft MVP
This article has been viewed 672 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.