Posted by: phillipnb | May 10, 2011

PHP and XML Namespace

Can we use two elements of the same name and having the same attributes in the same xml document? How are we going to identify these two elements uniquely?. The answer to this lies in the term called ‘name space’. Using xml namespace allows to avoid conflict when we need to use two elements of the same name storing different data. These name space data can be accessed using SimpleXML. SimpleXML library has methods that can be used to get all namespaces used in a document. Let us take the help of an example to demonstrate this.

Example 1:

<?xml version="1.0"?>
<library xmlns:lib="http://mysite.local/library"
	<book meta:isbn="8126526468">
		<title>Beginning Drupal</title>
		<author>Jacob Redding</author>
		<pub:publisher>Wiley Eastern</pub:publisher>
$library = new SimpleXMLElement('namespace1.xml',null,true);
$namespaces = $library->getDocNamespaces();
foreach ($namespaces as $key => $value) {
    echo "{$key} => {$value}\n";

In the above example, we load the xml file and using that SimpleXMLElement instance and then we call the getDocNameSpace method. The getDocNamesspaces() will return all the names spaces in that file as an associative array.

Example 2:
In this example we will first traverse through all the nodes of a xml file and display the contents of those child elements that does not belong to a namespace. Then we will traverse through the nodes using a particular node and its children based on a name space. Finally, we get all the namespaces in the xml file and then display the children belonging to a particular namespace. Here is the xml file and the php code to demonstrate what we were talking about.

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:ab="" xmlns:cd=""
    <title>Drupal 7</title>
    <description>Drupal Module Development</description>
    <cd:publisher>Shroff Publishers</cd:publisher>
    <ef:publisher>ef Shroff Publishers</ef:publisher>
    <ef:author>ef Butcher</ef:author>
$xml = new SimpleXmlElement("doc1.xml",null,true);
foreach ($xml->books->book as $entry) {
	echo "\n".$entry->title;
	echo "\n".$entry->description;
	echo "\n".$entry->isbn;
	echo "\n";

	$cd = $entry->children('');
	echo "\n".$cd->publisher;
	echo "\n".$cd->author;
	echo "\n";

	$namespaces = $entry->getNameSpaces(true);
	$cd = $entry->children($namespaces['cd']);
	echo "\n".$cd->publisher;
	echo "\n".$cd->author;

	$namespaces = $entry->getNameSpaces(true);
	$cd = $entry->children($namespaces['ef']);
	echo "\n".$cd->publisher;
	echo "\n".$cd->author;

In this example, we can see that the element ‘author’ belongs to a particular name space and we access this element first by using the uri and then by using the prefix. As the element is tied to a namespace, you have the freedom to use the same element name in a different namespace. This can be seen by the use of the element in namespaces ‘cd’and ‘ef’. More details on the rules regarding namespaces can be seen here.

So, till next time it is Happy PHPing.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: