getElementsByTagName + documentElement

M

Max

Hello everyone!

i would want to know if the getElementsByTagName() function starts to find
the elements from documentElement comprising the same documentElement.

XML example:
<?xml version="1.0"?>
<exslt:module xmlns:exslt="http://exslt.org/documentation"
xmlns="http://default-ns-outter" version="1" prefix="str" exslt:foo="bah" >
....
</exslt:module>

Javascript code:
var dom = new DOMImplementation();
....
var doc = dom.doc.getDocumentElement();
var nodeList = doc.getElementsByTagName("exslt:module");

Does getElementsByTagName() function returns the first element
"exslt:module"?

Has getElementsByTagNameNS('http://exslt.org/documentation', 'module')
function the same behavior?

And getElementById(): same or different?

Best regards,

Max
 
M

Martin Honnen

Max wrote:

Javascript code:
var dom = new DOMImplementation();
...
var doc = dom.doc.getDocumentElement();
var nodeList = doc.getElementsByTagName("exslt:module");

Does getElementsByTagName() function returns the first element
"exslt:module"?

I have no idea why you call a variable doc (like document?) if you then
assign an element to it. But your code calls getElementsByTagName on the
document element and that call will return all descendant elements of
the element it is called on but not the element itself.
You could simply do
var doc = dom.doc;
var nodeList = doc.getElementsByTagName("exslt:module");
that is call the getElementsByTagName method on the document node itself
and not on the document element.

If you have XML with namespaces then it is usually better to use
namespace aware methods like getElementsByTagNameNS. Of course that
method too when applied to the document node returns all matching
descendant elements in the document while applied to the document
element will return all matching descendant elements of the document
element.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top