473,789 Members | 2,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generating CDATA sections...?

Here's my problem:

<xsl:template match="/category">
....
<script
type="text/javascript">
&lt;![CDATA[
if (GBrowserIsComp atible())
{
var map = new GMap(document.g etElementById(" mapdiv"));
map.addControl( new GSmallMapContro l());
map.addControl( new GMapTypeControl ());
map.centerAndZo om(new GPoint( -4, 55), 8);

<xsl:apply-templates select="//story"/>

}
else
{
alert( "We're sorry, but your browser is not supported by
Google Maps");
}
]]&gt;
</script>
....
</xsl:template>

<!-- DO NOT reformat this. The interaction between CDATA, JavaScript
and XSL is horrible! -->
<xsl:template match="story">
<xsl:variable name="marker">m arker<xsl:value-of
select="positio n()"/></xsl:variable>
<xsl:variable name="url">"/pres/story/article_<xsl:va lue-of
select='normali ze-space(@article) '/>.html"</xsl:variable>
<xsl:variable name="title">"< xsl:value-of
select="normali ze-space(title)"/>"</xsl:variable>
<xsl:variable name="created"> "<xsl:apply-templates
select='created '/>"</xsl:variable>
<xsl:apply-templates select="locatio n"/>
var <xsl:value-of select="$marker "/> =
createMarker(po int, <xsl:value-of select="$url"/>,
<xsl:value-of select='$title'/>,
<xsl:value-of select="$create d"/>);

map.addOverlay( <xsl:value-of select="$marker "/>);

</xsl:template>

<xsl:template match="location ">
point = new GPoint( <xsl:value-of select="@longit ude"/>,
<xsl:value-of select="@latitu de"/>);
</xsl:template>

The data is here:
<URL:http://www.weft.co.uk/withmapsdemo/primitive?categ ory=9>

So what's the problem?

Well, if you run it form the command line, e.g.

-[simon]-> xsltproc pres-news-map.xsl primitive.xml

It works OK and you get this:
<URL:http://www.weft.co.uk/withmapsdemo/demo.html>
(yes, there's a minor JavaScript error, but I'm not too bothered about
that just now); the critical bit is formatted

point = new GPoint( -3.9488888888888 9, 54.948888888888 9);

var marker1 =
createMarker(po int, "/pres/story/article_64.html ",
"Gareth's off to a Land Down Under", "Friday, 13 January, 2006");

map.addOverlay( marker1);

But if you run it server-side, as I usually do, you get back in the DOM
tree not one big CDATA node, but a series of Text nodes, even if you do

doc.getDocument Element().norma lize();

(which I do). Because it's Text and not CDATA, XML syntax characters in
it get entified on printing, and you get:

point = new GPoint(
-3.9488888888888 9
,
54.948888888888 9
);
var
marker1
=
createMarker(po int,
&quot;/pres/story/article_64.html &quot;
,
&quot;Gareth 's off to a Land Down Under&quot;
,
&quot;Friday , 13 January, 2006&quot;
);

map.addOverlay(
marker1
);

which the JavaScript engines simply don't recognise as JavaScript at all.

Finally, if you run the transform the XML client side (which you will do
if you go to <http://www.weft.co.uk/withmapsdemo/newsmap> with a browser
which sends either text/xml or application/xml in its Accepts header),
you get (or at least I do, with Firefox) absolutely nothing at all.

Any ideas about what I've got wrong, and how I can fix this?

--
si***@jasmine.o rg.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/

;; this is not a .sig
Mar 11 '06 #1
10 2352
Simon Brooke wrote:
Here's my problem:

<xsl:template match="/category">
...
<script
type="text/javascript">
&lt;![CDATA[


A CDATA section is not for JavaScript, but for the XSLT transformer to
indicate that the following block should be ignored.

That knowing, the syntax should be as follows:

<script type="text/javascript">
// <![CDATA[
...
// ]]>
</script>
JW
Mar 11 '06 #2
Janwillem Borleffs wrote:
A CDATA section is not for JavaScript, but for the XSLT transformer to
indicate that the following block should be ignored.


Quick reminder: CDATA sections are only (and I stress ONLY!) an
alternative to escaping single characters, for the convenience of humans
doing cut-and-paste of textual data into an XML document. XML tools
generally do not consider the difference between
<a><![CDATA[<b>]]></a>
and
<a>&lt;b></a>
to be meaningful. In particular, XSLT treats the two as identical.
--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Mar 11 '06 #3
Slight correction: XSLT treats the as identical on input.

On output, you can use the cdata-section-elements directive of
xsl:output to identify elements whose text content should be wrapped in
CDATA sections. Note the word "should" -- depending on how the output of
the stylesheet is handled, that distinction may not be preserved. Also
note that this is an all-instances-of-this-kind-ofelement directive; you
can't do it on a case by case basis.

Also note that since it does this for all text children of those
elements, it does *not* address the case of wanting some of the text
outside the CDATA.
Mar 11 '06 #4
Simon Brooke wrote:
Here's my problem:
Um, are you trying to run XSLT+javascript in a single step?

I think you need to chain them, with the XSLT coming first.
If it all needs to be done on the fly, you've got a big
performance hit anyway ...

You can certainly run XSLT in a filter - e.g. mod_transform.
Not sure about javascript: that's customarily delegated to
browsers (or avoided altogether:-) in my world.
But if you run it server-side, as I usually do,


Can your javascript processor be chained?

--
Nick Kew
Mar 11 '06 #5
in message <qv************ @asgard.webthin g.com>, Nick Kew
('n***@asgard.w ebthing.com') wrote:
Simon Brooke wrote:
Here's my problem:
Um, are you trying to run XSLT+javascript in a single step?

I think you need to chain them, with the XSLT coming first.
If it all needs to be done on the fly, you've got a big
performance hit anyway ...


Yup. This all gets cached, so that repeated requests with the same query
part will get the result from the cache. It's only if the cache is stale
that I'll regenerate.
You can certainly run XSLT in a filter - e.g. mod_transform.
Yup, I already have my own component to do that:

http://www.weft.co.uk/library/jacqua...achedPage.html
Not sure about javascript: that's customarily delegated to
browsers (or avoided altogether:-) in my world.
> But if you run it server-side, as I usually do,


Can your javascript processor be chained?


The JavaScript gets processed client-side. I'm fairly sure, for the
Google Maps interface, that it has to - different JavaScript is chained
by Google for different clients. Obviously, I don't control what happens
if I hand off the XSL processing to the client, which is why I usually
do it server side.

--
si***@jasmine.o rg.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/
;;Drivers in the UK kill more people every single year than
;; Al Qaeda have ever killed in any single year.
Mar 11 '06 #6
in message <75************ *************** ***@comcast.com >, Joe Kesselman
('k************ *@comcast.net') wrote:
Janwillem Borleffs wrote:
A CDATA section is not for JavaScript, but for the XSLT transformer
to indicate that the following block should be ignored.


Quick reminder: CDATA sections are only (and I stress ONLY!) an
alternative to escaping single characters, for the convenience of
humans doing cut-and-paste of textual data into an XML document. XML
tools generally do not consider the difference between
<a><![CDATA[<b>]]></a>
and
<a>&lt;b></a>
to be meaningful. In particular, XSLT treats the two as identical.


We may be at cross purposes here, but it appears to me that this is
incorrect. Example: apply this stylesheet

<?xml version="1.0" encoding="UTF-8"?>
<xsl:styleshe et version="1.0">
<xsl:template match="foo">
<![CDATA[
<xsl:apply-templates select="bar"/>
]]>
</xsl:template>

<xsl:template match="bar">
bar <xsl:value-of select="positio n()"/> matched!
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>

to this XML:

<foo>
<bar>
Hello there!
</bar>
</foo>

yields:

<?xml version="1.0" encoding="UTF-8"?>

&lt;xsl:appl y-templates select="bar"/&gt;

in xsltproc, Xalan, and Saxon. However, replace '<![CDATA[' with
'&lt;![CDATA[' and ']]>' with ']]&gt;' and you get:

<?xml version="1.0" encoding="UTF-8"?>

&lt;![CDATA[

bar 1 matched!

Hello there!

]]&gt;

with xsltproc, Xalan, and Saxon. So unless all three are wrong,
XSL /does/ /not/ process the contents of a CDATA section, but /does/
process a section which will be rendered as a CDATA section when
serialised.

--
si***@jasmine.o rg.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/

;; I can't work yanks out......
;; Why do they frown upon sex yet relish violence?
;; Deep Fried Lettuce
Mar 11 '06 #7
in message <Ge************ ********@comcas t.com>, Joe Kesselman
('k************ *@comcast.net') wrote:
Slight correction: XSLT treats the as identical on input.

On output, you can use the cdata-section-elements directive of
xsl:output to identify elements whose text content should be wrapped in
CDATA sections.


Ah! Kwa-tcha! Thank you. 'cdata-section-elements="scrip t"' seems to be a
big step towards what I need; you need to use 'method="xml"' as well.
Fixing the serialiser seems to be the rest. I'm still finding that even
after using cdata-section-elements="scrip t" and using
Document.getDoc umentElement(). normalize() the DomImplementati on is still
passing the contents to me as a series of fragmentary text nodes rather
than as a single CDATA node, which means I escape the contents... Damn.
Still, I can make it work with this. Thanks.

--
si***@jasmine.o rg.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/

;; I'd rather live in sybar-space

Mar 11 '06 #8
In article <nj************ @gododdin.inter nal.jasmine.org .uk>,
Simon Brooke <si***@jasmine. org.uk> wrote:
XML
tools generally do not consider the difference between
<a><![CDATA[<b>]]></a>
and
<a>&lt;b></a>
to be meaningful. In particular, XSLT treats the two as identical.
We may be at cross purposes here,
You are :-)
but it appears to me that this is
incorrect. Example: apply this stylesheet
[...]
<![CDATA[
<xsl:apply-templates select="bar"/>
]]>
[...]
However, replace '<![CDATA[' with
'&lt;![CDATA[' and ']]>' with ']]&gt;'


That wasn't the transformation Joe specified. He was completely
removing the <![CDATA[ and ]]> and escaping the special characters
inside. The escaped equivalent is:

&lt;xsl:appl y-templates select="bar"/&gt;

To reiterate: as far as XML is concerned, CDATA sections are an
escaping mechamism that is equivalent to, but often more readable
than, character or entity references. Non-XML tools may treat them
differently, but this is dangerous since almost any XML processing
may change CDATA sections to other escaping.

-- Richard

Mar 11 '06 #9
Simon Brooke wrote:
Here's my problem:

<xsl:template match="/category">
...
<script
type="text/javascript">
&lt;![CDATA[


See the FAQ at http://xml.silmaril.ie/authors/cdata/

///Peter
Mar 11 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2424
by: Aidan | last post by:
The setup: a Sax parser in a servlet and a Java client (same machine) which uploads an XML document containing CDATA elements which hold base 64 encoded binary files. The servlet then SAX parses the client's data. If I comment out the parser call I can see that the transmition time of large data files is very low. My understanding is that a SAX parser should transmit the content of a CDATA string downstream without signiificant...
0
2060
by: Chris Waddingham | last post by:
I am experiencing 2 problems with CDATA sections. These are: 1. Expat appears to be collapsing adjacent linefeeds into one inside CDATA sections. 2. Expat (XML_CharacterDataHandler) returns the wrong len value for CDATA sections containing ']'. I would be grateful of any help you can offer. My XML application contains code like this:
10
42969
by: Jon Noring | last post by:
Out of curiosity, may a CDATA section appear within an attribute value with datatype CDATA? And if so, how about other attribute value datatypes which accept the XML markup characters? To me, the XML specification seems a little ambiguous on this, so I defer to the XML authorities. Refer to sections 2.4 and 2.7 (it all hinges on if CDATA attribute values are part of markup or not.) Thanks.
4
2017
by: Jake Barnes | last post by:
I'm reading over this page: http://wiki.script.aculo.us/scriptaculous/show/Usage I'm struck by this code example +++++++++++++++++++++++++++++++ 3. Use
11
6467
by: ericms | last post by:
Can anybody show me how to insert a CDATA section using XPathNavigator ? I have tried the follwing with no luck: XmlDocument docNav = new XmlDocument(); docNav.LoadXml(xmlString); XPathNavigator nav = docNav.CreateNavigator(); XmlDocument doc = new XmlDocument(); doc.LoadXml("<DocumentData></DocumentData>"); XmlElement elem = doc.CreateElement(currentNodeName);
2
5253
by: Steveino | last post by:
Hello, Just wondering if anyone could shed any light on this, it's probably me just being silly... I have a dataset that I've used to create an XmlDataDocument, in order to apply XSL. The XSL reformats the XML to another format (still XML, not HTML). This works fine, but whatever I do, I can't get the finally output section to have CDATA elements. I've added a cdata-section-elements tag into the xsl file, but it doesn't seem to have...
12
4014
by: Peter Michaux | last post by:
Hi, I am experimenting with some of the Ruby on Rails JavaScript generators and see something I haven't before. Maybe it is worthwhile? In the page below the script is enclosed in //<!]> Is this trick grounded in any real information about HTML vs XHTML? I
3
3170
by: yawnmoth | last post by:
How might I go about including a ]]in a cdata tag? Since ]]>'s normally close cdata's, ]]>, by itself, wouldn't work...
7
5271
by: Silfheed | last post by:
Heyas So first off I know that CDATA is generally hated and just shouldn't be done, but I'm simply required to parse it and spit it back out. Parsing is pretty easy with lxml, but it's the spitting back out that's giving me issues. The fact that lxml strips all the CDATA stuff off isnt really a big issue either, so long as I can create CDATA blocks later with <>&'s showing up instead of &lt;&gt;&amp; . I've scoured through the lxml docs, but...
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10410
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9984
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.