CDATA Escaping

337 views
Skip to first unread message

nr

unread,
Sep 30, 2008, 5:15:30 PM9/30/08
to KML Developer Support - Advanced Support for KML
think the KML example below shows a bug, please correct me if I'm
wrong so I can change my HTML. I am using Google Earth 4.3.7284.
I would expect the pop-up to say "X - X (< 5 z) . ", instead it says
"X - X (".
If I change the < to a &gt; it works as expected. Do I need to escape
within CDATA? I thought the point of CDATA was to eliminate the need
to escape.
NR
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
<name>KmlFile</name>
<Style id="sn_ylw-pushpin">
</Style>
<Style id="sh_ylw-pushpin">
<IconStyle>
<scale>1.2</scale>
</IconStyle>
</Style>
<StyleMap id="msn_ylw-pushpin">
<Pair>
<key>normal</key>
<styleUrl>#sn_ylw-pushpin</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#sh_ylw-pushpin</styleUrl>
</Pair>
</StyleMap>
<Placemark>
<name>ASDF</name>
<description> <![CDATA[X - X (< 5 z) .
<br><br><br>]]></description>
<styleUrl>#msn_ylw-pushpin</styleUrl>
<Point>

<coordinates>-116.9590990412384,46.41677724148703,50</coordinates>
</Point>
</Placemark>
</Document>
</kml>

ManoM

unread,
Sep 30, 2008, 5:57:56 PM9/30/08
to KML Developer Support - Advanced Support for KML
Hi nr,

Actually, this is the right behavior. Earth treats anything inside the
CDATA section as HTML. Since you would have to escape a < in HTML
(otherwise HTML thinks you're starting a tag like <p>), you have to
escape it in KML as well.

Mano

nr

unread,
Sep 30, 2008, 6:41:25 PM9/30/08
to KML Developer Support - Advanced Support for KML
Am I misunderstanding this description from the kml tutorial?

"If you want to write standard HTML inside a <description> tag, you
can put it inside a CDATA tag. If you don't, the angle brackets need
to be written as entity references to prevent Google Earth from
parsing the HTML incorrectly (for example, the symbol > is written as
&gt; and the symbol < is written as &lt;). This is a standard feature
of XML and is not unique to Google Earth."

I understand the second sentence to mean, If I don't use CDATA then I
have to escape the brackets. Are you saying that whether I use the
CDATA or not, I have to escape my description?

NR

barryhunter [KML Guru]

unread,
Oct 1, 2008, 5:40:27 AM10/1/08
to KML Developer Support - Advanced Support for KML
With CDATA you don't need to encode the < used in html tags. But just
like with HTML if you want a < to show to the user you do need* to
encode it. If you didnt use CDATA you would need to double encode the
< used in the text!


* (or rather you should, many browsers are quite lenient and let you
get away with it)

nr

unread,
Oct 1, 2008, 12:35:27 PM10/1/08
to KML Developer Support - Advanced Support for KML
Thanks for the explanation, that makes sense. The stuff inside CDATA
is interpreted as HTML, so < needs to be escaped.

NR

nr

unread,
Oct 1, 2008, 1:24:37 PM10/1/08
to KML Developer Support - Advanced Support for KML
Have a look at this behaviour:

Snippet 1 (works as expected):


<Placemark>
<description><![CDATA[X - X ( &lt; Y) ]]></description>
<Point>
<coordinates>-114.21422,52.694878,50</coordinates>
</Point>
</Placemark>

Snippet 2 (doesn't work as expected):


<Placemark>
<description><![CDATA[<br>X - X ( &lt; Y) ]]></description>
<Point>
<coordinates>-114.21422,52.694878,50</coordinates>
</Point>
</Placemark>


The only difference between these two is the added <br> within the
second CDATA. Any explanation for this?

barryhunter [KML Guru]

unread,
Oct 1, 2008, 4:11:06 PM10/1/08
to KML Developer Support - Advanced Support for KML

barryhunter [KML Guru]

unread,
Oct 1, 2008, 4:18:22 PM10/1/08
to KML Developer Support - Advanced Support for KML
btw, added it as an issue here:
http://code.google.com/p/kml-samples/issues/detail?id=196

On Oct 1, 9:11 pm, barryhunter [KML Guru] wrote:
> deja vo (how do you spell that?
>
> See:http://groups.google.com/group/kml-support-getting-started/browse_thr...

nr

unread,
Oct 1, 2008, 5:21:12 PM10/1/08
to KML Developer Support - Advanced Support for KML
Deja Vu :-)

Thanks for the help.

NR
Reply all
Reply to author
Forward
0 new messages