@JonB said in QDocument has not expected behavior when creating elements:
And in this case the setAttributeNode() docs show it returns the attribute node it is replacing, or a dummy node, and that's what you setValue() `on.
Qt's stuff does work and as documented so not unexpected, just not to the chaining expectations you have have from, say, jQuery or whichever XML DOMs.
You are right the QDomElement::setAttributeNode() does indeed not return a copy but a null attribute.
Manual on QDomNode::insertAfter(...) states:
Returns a new reference to newChild on success or a null node on failure.
The DOM specification disallow inserting attribute nodes, but due to historical reasons QDom accept them nevertheless.
My first interaction was the PHP XML Dom setAttribute(...) where the manual says:
The new DOMAttr or false if an error occurred.
Where I'm used to chain functions.
Looking to the Java implementation, which I never worked with, does the same as Qt's implementation.
Chaining makes it easy because local variables are superfluous in that case and for me it is more readable.
Conclusion: My assumption was apparently incorrect.