jxon icon indicating copy to clipboard operation
jxon copied to clipboard

Support namespace prefixes

Open nrenner opened this issue 4 years ago • 1 comments

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 createElement and 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 appendChild is now done before recursing down (plugging together top down instead of bottom up).
  • Relying on getting xmlns attributes handled automatically with createElementNS doesn't seem to work for all cases. So they are added explicitly now with setAttributeNS and their namespace http://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 the xmlns attributes to already exist in the document.

Resources

nrenner avatar Mar 04 '21 18:03 nrenner

I also added namespace support for prefixed attributes.

nrenner avatar Mar 05 '21 12:03 nrenner