Search
Web Braintrove
Site Navigation
Home
Products
Tags
Levels
Dates
Authors
5 MOST RECENT
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
Preview and Open Forms after Upgrading to Internet Explorer 8
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
Install Windows 7 Beta onto a Virtual Machine
Configure Lab Color Mode Defaults for Braintrove Articles
Permanently Change the Default Styles for New Word Documents
Enable Themes in Windows Server
Use Logical Operators in Place of Conditional Expressions
Books
Web Design with XML: Generating Webpages with XML, CSS, XSLT and Format
How to Do Everything with HTML
HTML Utopia: Designing Without Tables Using CSS
XSLT: Professional Developer's Guide
Easy HTML-DB Oracle Application Express : Create Dynamic Web Pages with OAE
Magazines
Inside Web Development
.net
Asp.net Professional
Soa Web Services Journal
Visual Basic & Net Journal
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.