[xsl] Re: XSL Java (&amp;lt; &lt; <) problem

Subject: [xsl] Re: XSL Java (&amp;lt; &lt; <) problem
From: yguaba@xxxxxxxxxxxx
Date: Tue, 06 Jan 2004 21:00:12 -0200
On 6 Jan 2004 at 16:15, Robert A. van Ginkel wrote:

> It seems <![CDATA[ does the same as the htmlspecialchar function in
> PHP (transfer html directionchars to harmless values) 

Not really. A CDATA section in an XML file doesn't actually *do* 
anything to the characters it contains, i.e., the text contained in a 
CDATA section is not transformed or rendered "harmless", as you 
suggest. The htmlspecialchars() and htmlentities() functions in PHP, 
on the other hand, substitutes HTML entities for "special characters" 
in a string (such as the ampersand and less-than symbols).

And just as you see the regular "&" and "<" characters, *not* the 
HTML entities in the underlying HTML code, when you view a web page 
with your browser, the JavaScript processor (which turns out to be 
the browser itself) does not see the XML entities (e.g. &lt;). When 
an XML document is transformed via an XSL stylesheet, the output will 
*NOT* contain the entities that you are so afraid of. Go ahead and 
try it!

> What i did (while waiting for response) turn around every Java
> expresion ie. 1 < 0 ? becoming 0 > 1 ? this worked. Now I tried the
> <![CDATA[ variation and it gave: function xmld(x,l) { var r="",c;
> for(i=0;i&lt;l;i++) { c = xmldate.charAt(x+i); r+=(c!="0" ||
> r!="")?c:""; } return r; } so this isn't java. (&lt;) 

No, this definitely isn't Java. JavaScript perhaps, but not Java. 
Let's have a look at the JavaScript code that you shared with us 
above. I'll space and indent it for the sake of clarity.

function xmld (x, l) 
{
	var r="", c;
	for( i=0; i &lt; l; i++ )
	{
		c = xmldate.charAt(x+i);
		r += (c != "0" || r != "") ? c : "";
	}
	return r;
}

I suppose your complaint is that you got "&lt;" rather than a "<" 
inside the "for" statement. Are you sure that you did not use the 
entity "&lt;" inside the CDATA section in your XML file? I think you 
were advised to type the regular "<" character inside the CDATA 
section, because there (and only there) it is harmless and will not 
be mistaken for the start of an element. In other words, if you're 
going to use a CDATA section, do NOT use entities.

> Isn't there a xml/xsl tag that returns < at the the front end?

What do you mean?

> PS. I use xml with xsl now in a php processor(this is a better processor
> than the one of the ie explorer + I get XHTML source back) instead of
> using the <?xml-stylesheet type="text/xsl" href="example.xsl"?> directive
> in the xml file.

No problem. Try this:

(a) Create the file x.xml with the contents between the dotted lines 
below:

---------------------

<?xml version="1.0" encoding="UTF-8"?>
<root>
	<elem>document.write("&lt;&gt;&amp;\&quot;");</elem>
</root>

---------------------

(b) Create the file x.xsl with the contents between the dotted lines 
below below and save it to the same directory as x.xml:

---------------------

<?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";>
<xsl:output method="html" indent="yes" encoding="iso-8859-1"/>
<xsl:template match="/">
	<html>
		<head>
			<script language="javascript">
			function test()
			{
			<xsl:value-of select="."/>
			}
			</script>
		</head>
		<body>
			<script language="javascript">
			test();
			</script>
		</body>
	</html>
</xsl:template>
</xsl:stylesheet>

---------------------

(c) Create the file x.php with the contents below and save it to the 
same directory as the two previous files:

---------------------

<?php
$xh = xslt_create();
$result = xslt_process($xh, 'file://'.getcwd().'/'.'x.xml',
							   'file://'.getcwd().'/'.'x.xsl');
xslt_free($xh);
print_r($result);
?>

---------------------

Now call up the PHP file (x.php) in your browser 
(http://localhost/x.php, for example, but I'm sure you know this 
part). I'll bet you anything that you'll get exactly this printed on 
your screen:

<>&"

Look at the source of the web page and you'll see that there are no 
entities anywhere in sight. That will already have been made clear by 
the fact that the JavaScript code will run without any errors and 
produce the desired output.

Good luck,

Erik

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread