jxon
jxon copied to clipboard
Support namespace prefixes
Fixes #52 by using lookupNamespaceURI and supporting changes.
Tested with current Chromium (89) and Firefox (86) on Ubuntu (not on IE or other browsers).
See updated demo that now has the locus: prefix in the XML string also.
Failing tests are in separate commits before fixes, so they can be checked out and seen failing (feel free to squash).
Includes the following changes:
- Refactoring of duplicate
createElementand namespace retrieval code into functions - For prefixed elements, the corresponding namespace is searched a) in the attributes of the current value, then b) in the parent element hierarchy by using
lookupNamespaceURI. - For the lookup to work, the element hierarchy already needs to exist when procesing a value. Therefore
appendChildis now done before recursing down (plugging together top down instead of bottom up). - Relying on getting
xmlnsattributes handled automatically withcreateElementNSdoesn't seem to work for all cases. So they are added explicitly now withsetAttributeNSand their namespacehttp://www.w3.org/2000/xmlns/, which is needed for lookup. This way, they can be set anywhere in the parent hierarchy of a prefixed element. - Adds attributes first when creating an object tree from XML. This is needed for a round trip
stringToJs->jsToString, as the the namespace lookup needs thexmlnsattributes to already exist in the document.
Resources
I also added namespace support for prefixed attributes.