I’ve had a section of my JavaScript code that’s been working fine for a while now to grab values from incoming xml comms, but it has suddenly stopped reporting any found elements by certain tags. I did some looking and it seems that only tags that are numbers get affected by this, once I switch back to letters everything works great. Is there some reason why I can’t use just numbers?
Javascript Code:
xmlhttp.onreadystatechange=function()
{
xmlDoc=xmlhttp.responseXML;
x=xmlDoc.getElementsByTaName("VAR");
alert(x.length); // <- reports 0 when numbers are used as tags
var dataBack = [];
for (j=0;j<x.length;j++) {
dataBack[j] = x[0].getElementsByTagName(x[j])[0].childNodes[0].nodeValue);
}
useXMLdata(dataBack);
}
I have tried adding a single letter to my number tags and it magically starts to work. I’m using this to actually catch the tags, but I’m curious why I can’t use only numbers.
Elements which are only numbers (e.g.
<8>Some content</8>) do not appear to be valid XML.The XML specification is pretty turgid, but section 3.1 defines start tag naming. A start tag name must begin with a
NameStartCharcharacter and then continue with any number ofNameCharcharacters.NameStartCharappears to be a subset ofNameCharwhich does not include the digits 0-9 among other things. Therefore a valid tag name cannot begin with a numeric digit.I doubt your XML will validate if it contains XML elements such as
<8>Something</8>. Whether that is the reason that your JavaScript fails I have no idea, but it’s reason enough not to structure your XML in that way.Edit
Try plugging the following XML into a validator:
This gives the error Invalid element name for the
<1xyz>tag. Even SO doesn’t like it hence the lack of syntax highlighting!