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?
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/
>> 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?
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.
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