Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You've already answered the question, but it's worth understanding why this is.</p> <p>The namespace that an element is in cannot be determined solely by the namespace prefix. To find what namespace an element named <code>t:foo</code> is in, you have to search up the ancestor-or-self axis until you find the nearest node that defines the namespace for <code>t:</code>. For instance:</p> <pre><code>&lt;t:one xmlns:t="ns-one"&gt; &lt;t:one&gt; &lt;t:two xmlns:t="ns-two"&gt; &lt;t:two/&gt; &lt;/t:two&gt; &lt;/t:one&gt; &lt;/t:one&gt; </code></pre> <p>In that document, every element whose name is <code>one</code> is in the <code>ns-one</code> namespace, and every element whose name is <code>two</code> is in the <code>ns-two</code> namespace. You can tell that the deepest element in that document is in <code>ns-two</code> not because <code>t:</code> intrinsically means <code>ns-two</code>, but because if you search up the ancestor-or-self axis, the first element that you hit with an <code>xmlns:t</code> attribute on it - its parent - tells you the namespace.</p> <p>Given that, which nodes should the XPath expression <code>//t:*</code> match? It's impossible to say, because what namespace <code>t:</code> is mapped to changes throughout the document.</p> <p>Also, namespace prefixes are temporary, but namespaces are permanent. If you know that <code>one</code> is in <code>ns-one</code>, you really, truly don't care whether its prefix is <code>t:</code> or <code>x:</code> or if it has no prefix at all and just an <code>xmlns</code> attribute.</p> <p>When you're querying an XML document with XPath, you need a way of specifying what namespace a given element is in. And that's what <code>SelectionNamespaces</code> in a DOMDocument, or a namespace manager in C#, or whatever are for: they tell you what namespaces the prefixes in your XPath queries represent. So if I've set the prefix <code>a:</code> to <code>ns-one</code>, the XPath <code>//a:one</code> will find me all of the elements named <code>one</code> in the <code>ns-one</code> namespace, irrespective of what the actual prefix they're using in the document I'm searching is.</p> <p>This is a little counterintuitive when you're first learning it, but really, it's the only way that makes any sense at all.</p>
 

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