note
dakkar
<h3>And now a small lesson on XML namespaces</h3>
<p><font size="-1">no, seriously</font></p>
<p>In namespace-aware XML documents, an element name is a <em>qualified name</em> (<em>qname</em>), composed by a <em>prefix</em> and a <em>local name</em>, separated by a colon (<tt>:</tt>). The prefix is bound to a URI via a namespace declaration. Example:</p>
<code>
<first>
<ns:second>
<my:third xmlns:my="someURI">
<fourth xmlns="otherURI">
<fifth/>
</fourth>
</my:third>
</ns:second>
</first>
</code>
<p>Let's read that. The element named <tt>first</tt> has a <em>local name</em> of <tt>first</tt>, and belongs to no namespaces. The element named <tt>ns:second</tt> is <strong>wrong</strong>, since namespace-aware parsers
require the prefix to be declared, and <tt>ns</tt> is not. <tt>my:third</tt> belongs to the <tt>someURI</tt> namespace, which is <em>locally bound to the <tt>my</tt> prefix</em>. <tt>fourth</tt> belongs to the (locally default) namespace <tt>otherURI</tt>, as does <tt>fifth</tt>.</p>
<p>Hope this is clear enough...</p>
<h3>Your problem</h3>
<p>XPath has some problems with namespaces, namely that an XPath expression is interpreted in the <em>context element</em> (which in the case of your program is the invocant of <tt>findnodes</tt>). So the prefixes are resolved using the namespace declarations visible from that node. This forces you to know the <em>prefixes</em> used in the document, instead of the URIs, which creates the problems I said earlier (prefixes are not unique, URIs are).</p>
<p><font size="+1">Anyway</font>, your problem is much easier: <tt>$tree</tt> is a <tt>XML::LibXML::Document</tt>, which has no knwoledge of namespaces, since they are declared (at the earliest) on the document element. This is why the second form works. BTW, in the previous examples (disregarding the <tt>ns:second</tt> element), if you did:
<code>
$docElem->findnodes('//my:third');
</code>
It wouldn't work, since the <tt>my</tt> prefix is not defined on the document element...</p>
<pre>--
dakkar - Mobilis in mobile
</pre>
242028
242028