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"
	 xmlns:meta="http://mysite.local/meta-lib"
	 xmlns:pub="http://mysite.local/publisher"
	 xmlns:local="http://mysite.local/local"
	 xmlns:alpha="http://mysite.local/alpha">
	<book meta:isbn="8126526468">
		<title>Beginning Drupal</title>
		<author>Jacob Redding</author>
		<pub:publisher>Wiley Eastern</pub:publisher>
	</book>
</library>
$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="http://somedomain1.com/ver/1.1/" xmlns:cd="http://somedomain2.com/ver/1.2"
xmlns:ef="http://somedomain3.com/ver/1.2">
<books>
  <book>
    <title>Drupal 7</title>
    <description>Drupal Module Development</description>
    <ab:isbn>9350232996</ab:isbn>
    <cd:publisher>Shroff Publishers</cd:publisher>
    <cd:author>Butcher</cd:author>
    <cd:date>12-01-2011</cd:date>
    <ef:publisher>ef Shroff Publishers</ef:publisher>
    <ef:author>ef Butcher</ef:author>
    <ef:date>12-01-2011</ef:date>
  </book>
</books>
</rss>
$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('http://somedomain2.com/ver/1.2');
	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.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Categories

%d bloggers like this: