PROBLEM DESCRIPTION
Code that was working up to recent CI build is now failing when querying xml attributes from an xml node.
STEPS TO REPRODUCE
1. Create new mobile project
2. Paste below code into the app.js
3. Run
ACTUAL RESULTS
[INFO] Attributes: null
EXPECTED RESULTS
[INFO] Attributes: [object TiDOMNamedNodeMap] [INFO] Value: appcelerator
CODE
var str = "<test name='appcelerator'/>";
var xml = Ti.XML.parseString(str);
Ti.API.info("Attributes: "+xml.attributes);
if (xml.attributes && (xml.attributes.length > 0)) {
Ti.API.info(" Value: "+xml.attributes.item(0).nodeValue);
}
win = Ti.UI.createWindow();
win.open();
WORKAROUND
A workaround was provided, and rejected from the customer.
var str = "<test name='appcelerator'/>"
var xml = Ti.XML.parseString(str);
var node = xml.documentElement;
//Ti.API.info("Attributes: "+xml.attributes);
if (node.attributes && (node.attributes.length > 0)) {
Ti.API.info(" Value: "+node.attributes.item(0).nodeValue);
};
win = Ti.UI.createWindow();
win.open();
This change is to conform to the DOM LEVEL 2 spec. See the description of "Attr": http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-637646024 And the IDL definition for "Document": http://www.w3.org/TR/DOM-Level-2-Core/core.html#i-Document In particular: * A Document is not an Element * Document provides the
documentElement
accessor for the element representing the root node of the document (the document object's first child) In the event that it was expected that you could directly access Element information off of a Document, this would require that we implement all Element methods on a Document (to conform to expected behavior, being able to retrieve Attr nodes off an object implies it is an Element, and that it should be interacted with as one). This violates the IDL specification by re-typing Document as a subtype of Element. The "workaround" is actually the correct way to interact with an XML tree.Additionally, see the behavior definition for
Node.attributes
in this section of the spec: http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-1950641247 The property is defined, but MUST (according to spec) returnnull
for non-Element nodes.Closing based on Stevens comments