[xsl] Re: Un-cdata-section-elements

Subject: [xsl] Re: Un-cdata-section-elements
From: yguaba@xxxxxxxxxxxx
Date: Tue, 21 Mar 2006 10:05:40 -0300
Hello Alex,

I'm not sure I understand your problem. I assume you're trying to 
transform an XML document containing JavaScript code inside one or 
more CDATA sections into an HTML document to be parsed by browsers.

If this is the case, there's nothing to be escaped: you just take the 
contents of the CDATA section and output it as plain text between two 
SCRIPT tags.

Suppose, for example, you have an XML document that looks like this:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<ROOT>
	<JSCode>
		<![CDATA[
			function test()
			{
				alert("Hello world!");
			}
		]]>
	</JSCode>
</ROOT>

You can transform it into an HTML file with an XSLT stylesheet like 
this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://www.w3.org/1999/XSL/Transform 
http://www.w3.org/2004/11/schema-for-xslt20.xsd";>
	<xsl:output method="html" indent="yes" encoding="UTF-8"/>
	<xsl:template match="/">
		<html>
			<script language="JavaScript">
				<xsl:value-of select="/ROOT/JSCode" disable-output-
escaping="yes"/>
			</script>
		</html>
		<body onload="test();">
			<p>test page</p>
		</body>
	</xsl:template>
</xsl:stylesheet>

You can test the example above by putting the XML code in a file 
(say, test.xml) and the XSLT code in another file (test.xsl) in the 
same directory as the first. Then open the XML file in your browser 
and, presto, everything works as expected.

Hope this helps.

Best wishes,

Erik


On 21 Mar 2006 at 10:34, omprakash.v@xxxxxxxxxxxxx wrote:

> Is there any way to undo the CDATA section rules for outputting XHTML 1.0?
> I'm trying to create something like ;
> 
> <script type=3D"javascript">
>   // <![CDATA[
>    javascript here ...
>   // ]]>
> </script>
> 
> The processors I use (tested Sablotron and LibXSLT) seems to
> (properly) use the DTD and enforce the content of <script> as CDATA ;
> 
> <script type=3D"javascript">
>   <![CDATA[
>    javascript here ...
>   ]]>
> </script>
> 
> where there's no JavaScript comments '//' in front of their definition (I
> can fake the last one, but I'm stumped on how to create the first one).
> The XHTML 1.0 strict validation approves, but the JavaScript engines
> complain (it doesn't understand CDATA, of course)
> 
> Is there anyway to override this so I can invoke my own rules for this
> element, kinda like the opposite of output@cdata-section-elements? Or do I
> have to hack something ugly together with
> value-of@disable-output-escaping?


	

	
		
_______________________________________________________ 
Yahoo! doce lar. Faga do Yahoo! sua homepage. 
http://br.yahoo.com/homepageset.html 

Current Thread