When using a default namespaced xml an xpath-expression need to look a bit different than when the xml uses namespace prefixes. Lets have a look at this namespaced XML:
<?xml version="1.0"?>
<bk:bookstore xmlns:bk="urn:xmlns:25hoursaday-com:bookstore">
<bk:book>
<bk:title>Lord of the Rings</bk:title>
<bk:author>J.R.R. Tolkien</bk:author>
</bk:book>
</bk:bookstore>
The xpath used to get the title of the book would look like: /bk:bookstore/bk:book/bk:title
Now lets look at this default namespaced XML, it has no prefixes so we need to take another aproach to get the title of the book this time:
<?xml version="1.0"?>
<bookstore xmlns="urn:xmlns:25hoursaday-com:bookstore">
<book>
<title>Lord of the Rings</title>
<author>J.R.R. Tolkien</author>
</book>
</bookstore>
This time the xpath would look like: _/[local-name()=’bookstore’]/[local-name()=’book’]/*[local-name()=’title’] _The xpath matches any namespace and looks for the local name bookstore and after that book and title.
PHP Example
The following code shows how to use it, and the code including the xml files can be downloaded from github.
<?php
$xml = simplexml_load_file('namespaced.xml');
$result = $xml->xpath('/bk:bookstore/bk:book/bk:title');
echo $result[0];
echo "<br />";
$xml = simplexml_load_file('default-namespaced.xml');
$result_from_default_namespace = $xml->xpath("/*[local-name()='bookstore']/*[local-name()='book']/*[local-name()='title']");
echo $result_from_default_namespace[0];