Parsing XML Namespaces - RDF

I’m attempting to parse an RDF XML file. The file utilizes namespaces and I’ve been trying to follow this example:
SimpleXML and namespaces » SitePoint

However, I must be missunderstaning something because the script below is not working. My first goal is to just print out a list of all the labels within the object properties.

You can view the script as it is currently executing at the location:

The XML can be found here:


	$url = "";
	$rdf = new RDF($url);
	class RDF{
		protected $rdf_url;
		protected $xml_obj;
	    public function __construct($rdf_url) {
			$this->rdf_url = $rdf_url;
			$this->xml_obj = simplexml_load_file($this->rdf_url);
		public function getXMLObject(){
			return $this->xml_obj;
		public function getRDFElement(){
			$ns_owl = $this->xml_obj->children('');
			foreach($ns_owl->ObjectProperty as $ObjectProperty){
				$ns_rdfs = $ObjectProperty->children('');
				print "Label: ".$ns_rdfs->label;

I’d appreciate any help you could offer.


I actually just figured this out, i’ll post a working copy in a while. In the meantime, I have one small thing bothering me still that I can’t seem to solve:

I’m trying to get the uri contained in the root nodes xmlns:property attribute.

if(!$xml_obj = simplexml_load_file(url)){
print "Error reading XML File";
print (string)$xml_obj->attributes('xmlns',true)->property;

Where the xml looks like:

<?xml version=“1.0” encoding=“UTF-8”?>
<!ENTITY rdf ‘http://…’>
<!ENTITY rdfs ‘http://…’>
<!ENTITY owl ‘http://…’>
<!ENTITY swivt ‘http://…’>
<!ENTITY wiki ‘http://…’>
<!ENTITY property ‘http://…’>
<!ENTITY wikiurl ‘http://…’>

xmlns:owl =“&owl;”

My ultimate goal is to get the url contained in the property attribute.
Thanks for the help.

The xmlns:* attributes are special in XML, in that they declare namespaces for the element. In your case you’re wanting to get the URL associated with the property namespace (it is used where you see <property:Blah />).

Since it is namespace information that you need, the process is different from “normal” attributes. Here’s an example:

$url = '';
$xml = new SimpleXMLElement($url, LIBXML_NOENT, true);
$namespaces = $xml->getDocNamespaces();
echo $namespaces['property'];

You’ll see that getDocNamespaces() (docs) was used to get the namespaces (as prefix => uri pairs in an array) for the document.

Since the value of the namespace is an entity (&property;) the snippet also uses the LIBXML_NOENT option (docs) to make the URL be substituted when loading the XML (you could use simplexml_load_file() and pass in that option, I just prefer new SimpleXMLElement()).