sp wrote:
[snip]
<?xml version="1.0" encoding="utf-8"?>
<elts>
<elt id="1" class="c1">content1</elt>
In HTML, an ID can't start with a number
In general, that is true of any application of SGML, and it is also true
of XML and its applications.
In XML, this is a violation of the ID validity constraint. A conforming,
non-validating parser may continue to process the rest of the document,
but it should be corrected, nevertheless.
The following is based on how I would /expect/ the Gecko engine to
behave. I say this because I've not used its XML DOM, so corrections are
not only welcome, but may be necessary.
[snip]
In order for the getElementById method to find an element, it must know
what attributes are identifiers. It doesn't do this by name, but by
type: ID. As you haven't provided a document type, it has no way of
knowing what attributes are of what type. That said, the Gecko engine
doesn't implement a validating parser, therefore it wouldn't actually
fetch the DTD, even if you included a declaration.
There are two options.
1) Define an internal subset for your document.
2) Avoid the getElementById method and walk the document tree.
The former would produce a document that may look something like:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE elts [
<!ELEMENT elts (elt)* >
<!ELEMENT elt (#PCDATA) >
<!ATTLIST elt
id ID #IMPLIED
class NMTOKENS #IMPLIED]>
<elts>
<elt id="a" class="c1">content1</elt>
<elt id="b" class="c1">content2</elt>
</elts>
[snip]
xmlData.firstChild.childNodes[0].className returns null also?
Gecko browsers add text nodes to keep whitespace in the source.
Even if Rob wasn't right about white space nodes, the className property
will be undefined for elements, anyway. The className property, like all
other shortcut attribute properties, is defined for the HTML DOM. As an
XML document should only expose the Core module, you would need to use
methods such as getAttribute to retrieve the attribute value.
[snip]
Mike