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:
http://unaffiliated-design.com/freelance/kenh98/test/widget.php

The XML can be found here:
http://unaffiliated-design.com/freelance/kenh98/wiki/index.php?title=Special:ExportRDF/France

<?php

	$url = "http://unaffiliated-design.com/freelance/kenh98/wiki/index.php?title=Special:ExportRDF/France";
	$rdf = new RDF($url);
	$rdf->getRDFElement();
	
	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);
			var_dump($this->xml_obj);
		} 
		
		public function getXMLObject(){
			return $this->xml_obj;
		}
		
		public function getRDFElement(){
			$ns_owl = $this->xml_obj->children('http://www.w3.org/2002/07/owl#');
			foreach($ns_owl->ObjectProperty as $ObjectProperty){
				$ns_rdfs = $ObjectProperty->children('http://www.w3.org/2000/01/rdf-schema#');
				print "Label: ".$ns_rdfs->label;
			}
		}
		
	}
?>

I’d appreciate any help you could offer.

Thanks.

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”?>
<!DOCTYPE rdf:RDF[
<!ENTITY rdf ‘http://…’>
<!ENTITY rdfs ‘http://…’>
<!ENTITY owl ‘http://…’>
<!ENTITY swivt ‘http://…’>
<!ENTITY wiki ‘http://…’>
<!ENTITY property ‘http://…’>
<!ENTITY wikiurl ‘http://…’>
]>

<rdf:RDF
xmlns:rdf=“&rdf;”
xmlns:rdfs=“&rdfs;”
xmlns:owl =“&owl;”
xmlns:swivt=“&swivt;”
xmlns:wiki=“&wiki;”
xmlns:property=“&property;”>

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 = 'http://unaffiliated-design.com/freelance/kenh98/wiki/index.php?title=Special:ExportRDF/France';
$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()).