Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Adding CDATA encapsulation or processing instructions around XHTML when generated using JavaScript E4X

104 views
Skip to first unread message

jadon-usenet

unread,
Sep 8, 2006, 7:48:23 AM9/8/06
to
I use the following JavaScript to create a JavaScript entry in an XHTML
page:

XML.ignoreComments = false;
script =
<xml><!--
signup = new Array();
signup.onMouseClick = function(e)
{
alert(e);
}
document.getElementById("delete-elem0").onclick =
signup.onMouseClick;
--></xml>.toString();
page.body.script = script;
page.body.script.@language = "javascript";
page.body.script.@type = "text/javascript;e4x=1";

The result is:

<script language="javascript" type="text/javascript;e4x=1">signup =
new Array();
signup.onMouseClick = function(e)
{
alert(e);
}
document.getElementById("delete-elem0").onclick =
signup.onMouseClick;</script>

I'm trying to figure out why the "<!--" "-->" pair was removed and how
i can insert a "<![CDATA[" "]]>" pair around the JavaScript so that I
can include more XML within the embedded script. When I try, the "<"
in "<![CDATA[" is replaced with "&lt;". I guess that makes some sense,
but I hope there must be some way to inject that string pair into my
XML.

Martin Honnen

unread,
Sep 8, 2006, 8:29:50 AM9/8/06
to
jadon-usenet wrote:

> I use the following JavaScript to create a JavaScript entry in an XHTML
> page:
>
> XML.ignoreComments = false;
> script =
> <xml><!--
> signup = new Array();
> signup.onMouseClick = function(e)
> {
> alert(e);
> }
> document.getElementById("delete-elem0").onclick =
> signup.onMouseClick;
> --></xml>.toString();

If you want to create XML with E4X XML literals and then convert that a
string with XML markup then you should call toXMLString() on the XML
object and not toString(). I am however not sure what you want to
achieve with the above construct.
Which script engine are you using with E4X, Spidermonkey or Rhino?

> page.body.script = script;
> page.body.script.@language = "javascript";
> page.body.script.@type = "text/javascript;e4x=1";
>
> The result is:
>
> <script language="javascript" type="text/javascript;e4x=1">signup =
> new Array();
> signup.onMouseClick = function(e)
> {
> alert(e);
> }
> document.getElementById("delete-elem0").onclick =
> signup.onMouseClick;</script>

You nee to explain what kind of object page is and how exactly you get
the "result" you show.

> I'm trying to figure out why the "<!--" "-->" pair was removed and how
> i can insert a "<![CDATA[" "]]>" pair around the JavaScript so that I
> can include more XML within the embedded script. When I try, the "<"
> in "<![CDATA[" is replaced with "&lt;".

E4X can parse CDATA sections in XML markup presented as a string e.g.
var xmlObject = new XML('<text><![CDATA[Kibo & Xibo]]></text>');
or an XML literal e.g.
var xmlObject2 = <text><![CDATA[Kibo & Xibo]]></text>;
but its data model does not make any difference between CDATA sections
and normal text nodes so in the above examples the XML object of node
kind 'element' with the name 'text' has as its sole child an XML object
of node kind 'text'.
If you serialize an XML object back to a string with toXMLString() then
it does not create CDATA section as far as I know, it simply escapes
characters as needed e.g.
xmlObject.toXMLString()
yields
<text>Kibo &amp; Xibo</text>

So you can use CDATA sections when creating XML objects with E4X but if
you serialize back then you won't get them but the result is well-formed
by escaping characters as needed instead.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Marcello Bastéa-Forte

unread,
Sep 8, 2006, 12:02:10 PM9/8/06
to
Martin Honnen wrote:

> If you want to create XML with E4X XML literals and then convert that a
> string with XML markup then you should call toXMLString() on the XML
> object and not toString(). I am however not sure what you want to
> achieve with the above construct.

Is there anywhere that explains the difference between toString and
toXMLString?

Marcello

Jeff Walden

unread,
Sep 8, 2006, 5:43:38 PM9/8/06
to
Marcello Bastéa-Forte wrote:
> Is there anywhere that explains the difference between toString and
> toXMLString?

The specification is really the best reference, unfortunately. It's hard to give a good description of exactly how the two differ without going there, because the spec definition is the only real way to explain it.

Jeff

--
Rediscover the Web!
http://snurl.com/get_firefox

Reclaim Your Inbox!
http://snurl.com/get_thunderbird

Martin Honnen

unread,
Sep 9, 2006, 8:33:12 AM9/9/06
to
Jeff Walden wrote:

> Marcello Bastéa-Forte wrote:
>
>> Is there anywhere that explains the difference between toString and
>> toXMLString?
>
>
> The specification is really the best reference, unfortunately.

If you don't want to start with the specification then looking at
<http://www.faqts.com/knowledge_base/index.phtml/fid/1784>
where I tried to document all methods in an informal way with some
simple examples might be a start.

Marcello Bastéa-Forte

unread,
Sep 10, 2006, 2:02:24 AM9/10/06
to
Interesting! I actually found that listing in my searches for
information on E4X (compilation here:
http://marcello.cellosoft.com/projects/alt/js-e4x.html), and I may have
seen your description for that method, but it seems to be for a deep XML
structure, toString simply calls toXMLString.

I have looked at the specification but a lot of it seemed overly
verbose, but I suppose I can look again.

The main thing that bothers me on E4X is all the method syntax
(children(), text(), etc.) they should be properties, not methods!
(Especially since JavaScript supports getters/setters for properties.)
That's a side note anyhow...

It would be nice to see an 'official' api documentation (for using E4X,
not implementing it) along the lines of Adobe's, but with the little
more detail on each of the methods like you have done. Perhaps on
developer.mozilla.org (although I don't know what's involved on
contributing to that wiki).

Marcello

eric.pr...@gmail.com

unread,
Sep 10, 2006, 3:10:00 PM9/10/06
to
Marcello Bastéa-Forte wrote:
>[...]

>
> The main thing that bothers me on E4X is all the method syntax
> (children(), text(), etc.) they should be properties, not methods!
> (Especially since JavaScript supports getters/setters for properties.)
> That's a side note anyhow...

E4X deliberately defines these as methods so programs can work
with documents that contain valid element names that collide, such
as this case

js> var doc = <doc>
<text>just some text</text>
mixed content
</doc>
js> doc.text
just some text
js> doc.text()
mixed content

I suppose the E4X spec could have defined a set of reserved keywords,
leaving programmers to reference elements contained in tags using
those names with the alternate notation, but it doesn't. As in

js> doc['text']
just some text

- Eric

>
>[...]

Marcello Bastéa-Forte

unread,
Sep 10, 2006, 3:36:21 PM9/10/06
to
I guess that makes sense, but wouldn't that break the functional aspect
of JavaScript? If doc.text() is a function call, doc.text should be a
Function object, right?

Anyway, I've just been using the extra syntax and avoiding the weird
function calls when possible (such as xml.* instead of xml.children()).
Granted, that's probably slower/worse, but I am still just playing around.

Marcello

igor.b...@gmail.com

unread,
Sep 11, 2006, 6:57:56 AM9/11/06
to

Marcello Bastéa-Forte wrote:
> I guess that makes sense, but wouldn't that break the functional aspect
> of JavaScript? If doc.text() is a function call, doc.text should be a
> Function object, right?

No, E4X effectively introduced separated name spaces for methods and
properties. That is, "doc.text" means "get the value of the property
text from doc", while "doc.text()" means "get the function named text
from doc and call it using doc as this". For ordinary objects property
and function value are the same, but for XML ones they entirely
different.

If you would like to get the function object behind doc.text(), you
have to use SpiderMonkey extension doc.function::text. In fact, you can
assume that doc.text(), doc['text']() are just syntax sugar for
doc.function::text() or doc.function::['text'].

Regards, Igor

0 new messages