[thelist] ignore xml whitespace?
Andrew Clover
and-evolt at doxdesk.com
Sun Jun 27 00:26:37 CDT 2004
Diane Soini <dianesoini at earthlink.net> wrote:
> I've been struggling to use xml in javascript because of differences in
> how browsers see whitespace. When I call hasChildNodes() one browser
> counts whitespace as childNodes while another skips whitespace and
> counts elements as childNodes.
This is true regardless of whether the document was parsed as XML or
HTML. By my reading of the standards all browsers should preserve the
whitespace Text nodes in HTML and XHTML (at least in elements with Mixed
content), however IE typically does not.
In any case, your code will have to be aware of the possibility of
whitespace nodes and work around them appropriately. A DOM Level 2 Range
'NodeFilter' object would be one way of doing it but is not
well-supported on browsers. Similarly you could use your own function to
get the 'childElements' of a particular Node, eg:
function childElements(parentNode) {
var l= new Array();
for (var i= 0; i<parentNode.childNodes.length; i++)
if (parentNode.childNodes[i].nodeType==1)
l[l.length]= parentNode.childNodes[i];
return l;
}
Or you could even removeChild the whitespaces. But often, judicious use
of getElementsByTagName, the HTMLCollection-based properties, and just
skipping over text nodes may do the trick.
--
Andrew Clover
mailto:and at doxdesk.com
http://www.doxdesk.com/
More information about the thelist
mailing list