Skip to content Skip to sidebar Skip to footer

Using Creatensresolver With Multiple Namespaces Specified In The Xml

I have an xml string such as this: Copy

where the same namespace is used with different prefixes and of course you could have samples as e.g.

<p:fooxmlns:p="http://example.com/n1"><p:barxmlns:p="http://example.com/n2">...</p:bar></p:foo>

where the same prefix is bound to different namespace URIs on different elements.

So basically you need to know the namespace URI of the element node you are looking for, then you can set up a namespace resolver with any prefix your code can choose e.g.

return xmlDoc.evaluate("//pf:key", xmlDoc, function(prefix) { if (prefix === 'pf'return'http://schemas.datacontract.org/2004/07/System.Collections.Generic'; elsereturnnull; }, XPathResult.ANY_TYPE, null);

On the other hand with that simple path I would not use XPath and evaluate at all but rather go for return xmlDoc.getElementsByTagNameNS('http://schemas.datacontract.org/2004/07/System.Collections.Generic', 'b');.

As for finding namespace declarations in a DOM tree to build up a namespace resolver yourself based on prefixes, XPath can give some assistance itself by looking at the namespace axis e.g. //namespace::* but inside the browser Mozilla/Firefox never considered it necessary to support the namespace axis. Then there are some DOM Level 3 methods http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-lookupNamespacePrefix and http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-lookupNamespaceURI so you could write your own code walking the DOM tree and collecting namespace declarations but that does not help in the general case to solve the problems outlined in the two example samples I posted above.

Post a Comment for "Using Creatensresolver With Multiple Namespaces Specified In The Xml"