Note that there are some explanatory texts on larger screens.

plurals
  1. POReference to undeclared namespace prefix when parsing MSXML
    text
    copied!<p>How do I solve the </p> <pre><code>Reference to undeclared namespace prefix: '%s' </code></pre> <p>problem with Microsoft's msxml implementation?</p> <hr> <p>I'm using an XML feed from a government web-site that contains values i need to parse. The xml contains namespaces:</p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt; &lt;rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:cb="http://www.cbwiki.net/wiki/index.php/Specification_1.1" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3c.org/1999/02/22-rdf-syntax-ns#rdf.xsd"&gt; &lt;item rdf:about="http://www.bankofcanada.ca/stats/rates_rss/STATIC_IEXE0101.xml"&gt; &lt;cb:statistics&gt; &lt;cb:exchangeRate&gt; &lt;cb:value decimals="4"&gt;1.0351&lt;/cb:value&gt; &lt;cb:baseCurrency&gt;CAD&lt;/cb:baseCurrency&gt; &lt;cb:targetCurrency&gt;USD&lt;/cb:targetCurrency&gt; &lt;cb:rateType&gt;Bank of Canada noon rate&lt;/cb:rateType&gt; &lt;cb:observationPeriod frequency="daily"&gt;2011-05-09T12:15:00-04:00&lt;/cb:observationPeriod&gt; &lt;/cb:exchangeRate&gt; &lt;/cb:statistics&gt; &lt;/item&gt; &lt;/rdf:RDF&gt; </code></pre> <p>Running the XPath query:</p> <pre class="lang-xml prettyprint-override"><code>/rdf:RDF/item/cb:statistics/cb:exchangeRate/cb:targetCurrency </code></pre> <p>fails with the error:</p> <pre class="lang-xml prettyprint-override"><code>Reference to undeclared namespace prefix: 'rdf' </code></pre> <hr> <p><strong>Edit</strong>:</p> <p>If i edit the original XML to remove all use of namespaces:</p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt; &lt;rdf&gt; &lt;item&gt; &lt;statistics&gt; &lt;exchangeRate&gt; &lt;value decimals="4"&gt;1.0351&lt;/value&gt; &lt;baseCurrency&gt;CAD&lt;/baseCurrency&gt; &lt;targetCurrency&gt;USD&lt;/targetCurrency&gt; &lt;rateType&gt;Bank of Canada noon rate&lt;/rateType&gt; &lt;observationPeriod frequency="daily"&gt;2011-05-09T12:15:00-04:00&lt;/observationPeriod&gt; &lt;/exchangeRate&gt; &lt;/statistics&gt; &lt;/item&gt; &lt;/rdf&gt; </code></pre> <p>The query <code>/rdf/item/statistics/exchangeRate/baseCurrency</code> doesn't fail, and returns nodes:</p> <pre class="lang-xml prettyprint-override"><code>&lt;baseCurrency&gt;CAD&lt;/baseCurrency&gt; </code></pre> <p>How do i get Microsoft XML to work with namespaces?</p> <hr> <h2>Edit 2</h2> <p>i've tried adding <strong>SelectionNamespaces</strong> to the DOMDocument object:</p> <pre class="lang-xml prettyprint-override"><code>doc.setProperty('SelectionNamespaces', 'xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cb="http://www.cbwiki.net/wiki/index.php/Specification_1.1"'); </code></pre> <p>Now the xpath query doesn't fail, but it also returns no nodes:</p> <pre class="lang-xml prettyprint-override"><code>nodes = doc.selectNodes('/rdf:RDF/item/cb:statistics/cb:exchangeRate/cb:targetCurrency'); </code></pre> <h2>See also</h2> <ul> <li><a href="https://stackoverflow.com/questions/2811053/undeclared-reference-to-namespace-prefix-error">“undeclared reference to namespace prefix ” error</a></li> <li><a href="https://stackoverflow.com/questions/3555461/xmlreader-how-to-handle-undeclared-namespace">XMLReader - How to handle undeclared namespace</a></li> <li><a href="http://support.microsoft.com/kb/280457" rel="nofollow noreferrer">PRB: Specifying Fully Qualified Element Names in XPath Queries</a></li> <li><a href="http://www.vbmonster.com/Uwe/Forum.aspx/vb/2981/XPath-not-working-properly" rel="nofollow noreferrer">XPath not working properly.</a></li> </ul>
 

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