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

text to XML object (E4X)

4 views
Skip to first unread message

hluz

unread,
Nov 2, 2005, 4:35:58 AM11/2/05
to
Hi everyone.
I'm having a problem with creating a XML object from a document received
via XMLHTTPRequest.
The following code:

var x = new XML(responseText)

gives javascript error: xml is a reserved identifier.
If I remove the <?xml version="1.0"?> line from responseText before
using it, it works fine.

Any ideas?

Martin Honnen

unread,
Nov 2, 2005, 1:06:54 PM11/2/05
to

hluz wrote:

It is by design as the authors of the E4X specification wanted it, the
specification for
new XML(string)
says to do the following (13.4.2):
Let x = ToXML(string)
and ToXML(string) is specified as follows (10.3.1):
Parse the concatenation of
<parent xmlns=", defaultNamespace, ">", string, "</parent>"
So that way a conforming implementation has to parse e.g.
<parent xmlns=""><?xml version="1.0"?><root /></parent>
and that is not well-formed and gives a parse error.
Thus if you want to parse a string with XML markup make sure you throw
the XML declaration away before passing the string to new XML().

A regular expression to match the XML declaration is e.g.
var xmlDeclaration = /^<\?xml version[^>]+?>/;
so you could try removing it e.g.
new XML(responseText.replace(xmlDeclaration, ''))

--

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

hluz

unread,
Nov 2, 2005, 8:36:16 PM11/2/05
to
Martin Honnen wrote:
> hluz wrote:

>> var x = new XML(responseText)
>>
>> gives javascript error: xml is a reserved identifier.
>> If I remove the <?xml version="1.0"?> line from responseText before
>> using it, it works fine.
>
>
> It is by design as the authors of the E4X specification wanted it, the
> specification for
> new XML(string)
> says to do the following (13.4.2):
> Let x = ToXML(string)
> and ToXML(string) is specified as follows (10.3.1):
> Parse the concatenation of
> <parent xmlns=", defaultNamespace, ">", string, "</parent>"
> So that way a conforming implementation has to parse e.g.
> <parent xmlns=""><?xml version="1.0"?><root /></parent>
> and that is not well-formed and gives a parse error.
> Thus if you want to parse a string with XML markup make sure you throw
> the XML declaration away before passing the string to new XML().
>
> A regular expression to match the XML declaration is e.g.
> var xmlDeclaration = /^<\?xml version[^>]+?>/;
> so you could try removing it e.g.
> new XML(responseText.replace(xmlDeclaration, ''))
>

Thanks for your reply Martin. Yes, removing it works fine. Just one
question: Shouldn't the xml declaration be considered a Processing
Instruction and as such be ingnored when property
XML.ignoreProcessingInstructions = true?

Martin Honnen

unread,
Nov 3, 2005, 9:35:21 AM11/3/05
to

hluz wrote:

> Just one
> question: Shouldn't the xml declaration be considered a Processing
> Instruction and as such be ingnored when property
> XML.ignoreProcessingInstructions = true?

No, the XML markup is parsed as normal, only when the XML object is
built after successfully parsing the markup then processing instructions
are not included with the above setting.

Jonas Sicking

unread,
Nov 3, 2005, 8:05:18 PM11/3/05
to
> Thanks for your reply Martin. Yes, removing it works fine. Just one
> question: Shouldn't the xml declaration be considered a Processing
> Instruction and as such be ingnored when property
> XML.ignoreProcessingInstructions = true?

What happens is actually exactly that. We try to parse <?xml as a
processing instruction, however since the name 'xml' is reserved you get
a parsing error.

In fact, the xml-declaration appearing at the top of a file is not a
processing instruction. If you look at a DOM created by mozilla you'll
notice that the xml-declaration does not show up as a PI. The xml spec
explicitly states this.

/ Sicking

0 new messages