title: XPath in SimpleXML for default namespaces without needing prefixes
body: <p>I have an XML document that has a default namespace attached to it, eg</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;foo xmlns="http://www.example.com/ns/1.0"&gt;
  ...
  &lt;/foo&gt;
  </code></pre>
  
  <p>In reality this is a complex XML document that conforms to a complex schema.  My job is to parse out some data from it.  To aid me, I have a spreadsheet of XPath.  The XPath is rather deeply nested, eg</p>
  
  <pre><code>level1/level2/level3[@foo="bar"]/level4[@foo="bar"]/level5/level6[2]
  </code></pre>
  
  <p>The person who generate the XPath is an expert in the schema, so I am going with the assumption that I can't simplify it, or use object traversal shortcuts.</p>
  
  <p>I am using <a href="http://us3.php.net/manual/en/book.simplexml.php" rel="noreferrer">SimpleXML</a> to parse everything out.  My problem has to do with how the default namespace gets handled. </p>
  
  <p>Since there is a default namespace on the root element, I can't just do</p>
  
  <pre><code>$xml = simplexml_load_file($somepath);
  $node = $xml-&gt;xpath('level1/level2/level3[@foo="bar"]/level4[@foo="bar"]/level5/level6[2]');
  </code></pre>
  
  <p>I have to <a href="http://us3.php.net/manual/en/simplexmlelement.registerxpathnamespace.php" rel="noreferrer">register the namespace</a>, assign it to a prefix, and then use the prefix in my XPath, eg</p>
  
  <pre><code>$xml = simplexml_load_file($somepath);
  $xml-&gt;registerXPathNamespace('myns', 'http://www.example.com/ns/1.0');
  $node = $xml-&gt;xpath('myns:level1/myns:level2/myns:level3[@foo="bar"]/myns:level4[@foo="bar"]/myns:level5/myns:level6[2]');
  </code></pre>
  
  <p>Adding the prefixes isn't going to be manageable in the long run.</p>
  
  <p>Is there a proper way to handle default namespaces without needing to using prefixes with XPath?  </p>
  
  <p>Using an empty prefix doesn't work (<code>$xml-&gt;registerXPathNamespace('', 'http://www.example.com/ns/1.0');</code>).  I can string out the default namespace, eg</p>
  
  <pre><code>$xml = file_get_contents($somepath);
  $xml = str_replace('xmlns="http://www.example.com/ns/1.0"', '', $xml);
  $xml = simplexml_load_string($xml);
  </code></pre>
  
  <p>but that is skirting the issue.</p>