Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is XmlNamespaceManager necessary?
    primarykey
    data
    text
    <p>I've come up kinda dry as to <strong>why</strong> -- at least in the .Net Framework -- it is necessary to use an <code>XmlNamespaceManager</code> in order to handle namespaces (or the rather clunky and verbose <code>[local-name()=...</code> XPath predicate/function/whatever) when performing XPath queries. I <strong>do</strong> understand why namespaces are necessary or at least beneficial, but <strong>why</strong> is it so complex?</p> <p>In order to query a simple XML Document (no namespaces)...</p> <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt; &lt;rootNode&gt; &lt;nodeName&gt;Some Text Here&lt;/nodeName&gt; &lt;/rootNode&gt; </code></pre> <p>...one can use something like <code>doc.SelectSingleNode("//nodeName")</code> (which would match <code>&lt;nodeName&gt;Some Text Here&lt;/nodeName&gt;</code>)</p> <p><em>Mystery #1</em>: <strong>My first annoyance</strong> -- If I understand correctly -- is that merely adding a namespace reference to the parent/root tag (whether used as part of a child node tag or not) like so:</p> <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt; &lt;rootNode xmlns="http://someplace.org"&gt; &lt;nodeName&gt;Some Text Here&lt;/nodeName&gt; &lt;/rootNode&gt; </code></pre> <p>...requires several extra lines of code to get the same result:</p> <pre><code>Dim nsmgr As New XmlNamespaceManager(doc.NameTable) nsmgr.AddNamespace("ab", "http://s+omeplace.org") Dim desiredNode As XmlNode = doc.SelectSingleNode("//ab:nodeName", nsmgr) </code></pre> <p>...essentially dreaming up a non-existent prefix ("<code>ab</code>") to find a node that doesn't even use a prefix. <em>How does this make sense?</em> What is wrong (conceptually) with <code>doc.SelectSingleNode("//nodeName")</code>?</p> <p><em>Mystery #2</em>: So, say you've got an XML document that uses prefixes:</p> <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt; &lt;rootNode xmlns:cde="http://someplace.org" xmlns:feg="http://otherplace.net"&gt; &lt;cde:nodeName&gt;Some Text Here&lt;/cde:nodeName&gt; &lt;feg:nodeName&gt;Some Other Value&lt;/feg:nodeName&gt; &lt;feg:otherName&gt;Yet Another Value&lt;/feg:otherName&gt; &lt;/rootNode&gt; </code></pre> <p>... If I understand correctly, you would have to add both namespaces to the <code>XmlNamespaceManager</code>, in order to make a query for a single node...</p> <pre><code>Dim nsmgr As New XmlNamespaceManager(doc.NameTable) nsmgr.AddNamespace("cde", "http://someplace.org") nsmgr.AddNamespace("feg", "http://otherplace.net") Dim desiredNode As XmlNode = doc.SelectSingleNode("//feg:nodeName", nsmgr) </code></pre> <p>... Why, in this case, do I need (conceptually) a namespace manager? </p> <p><em><strong></em>**REDACTED into comments below**</strong></p> <p><strong>Edit Added:</strong> My revised and refined question is based upon the apparent redundancy of the XmlNamespaceManager in what I believe to be the majority of cases and the use of the namespace manager to specify a mapping of prefix to URI:</p> <p>When the direct mapping of the namespace prefix ("cde") to the namespace URI ("http://someplace.org") is explicitly stated in the source document: </p> <pre><code>...&lt;rootNode xmlns:cde="http://someplace.org"... </code></pre> <p>what is the conceptual need for a programmer to recreate that mapping before making a query?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload