Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with XML namespaces in JavaScript (DOM)

I am building a tree (for SOAP) using DOM. I would like read the following info at a certain node:

  • Is an XML namespace already "imported" into the document (with xmlns:blah="http://...) - knowing the http://... part.
  • What moniker (in the above example blah) used.

Is there any way other than the manual: to walk chain of ancestors and iterate on attribute nodes, find any starting with xmlns: checking the value and if match return the rest of the attribute name?

like image 644
vbence Avatar asked May 29 '26 09:05

vbence


1 Answers

Aside the usual methods such as document.getElementsByTagName, DOM offers their namespaced versions: document.getElementsByTagNameNS

Such methods take the namespace URL as their first argument.

document.getElementsByTagNameNS('http://...', 'abc');

By the way, using the regular methods, the elements might be available as…

document.getElementsByTagName('xmlns\\:abc');

This works for me in case of a HTML DOM even without "importing" any namespace.

Update:

The method OP was looking for is document.lookupPrefix('http://...')

like image 106
J. K. Avatar answered May 31 '26 22:05

J. K.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!