views:

310

answers:

1

I am using php's simple xml and xpath to parse an rdf xml file and am struggling to get a list of all the rdf:about values.

Any advice?

+2  A: 

There seems to be an issue when using SimpleXml with namespaced attributes prior to PHP5.3. Basically, anything with a : will be dropped when converted to an object property of a SimpleXml element. The following will do, but feels hackish to me:

$rdf = str_replace('rdf:about', 'rdf_about', $rdf);  
$rdf = new SimpleXMLElement($rdf);
foreach($rdf->xpath('//@rdf_about') as $node) {
  echo $node, PHP_EOL;
}

See here:

You could use DOM instead of SimpleXml:

$dom = new DomDocument;
$dom->loadXml($rdf);
$xph = new DOMXPath($dom);
$xph->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
foreach($xph->query('//@rdf:about') as $attribute) {
    echo $attribute->value, PHP_EOL;
}

But, I suggest using a dedicated library for this over SimpleXml or DOM:

And here's a blog post about the parsers:

Gordon
Using Dom has proved much more successful - thanks. I am using an arc triplestore and have looked into using its parser but all i want are the elements of a few ontologies and the arc parses seems to want to just provide a list or triples.
David
@David Haven't worked with ARC yet, so I can't tell much about it. You could ask http://twitter.com/bengee if it's possible though.
Gordon
I might do but I have it working now using Dom intead of simple xml. Thansk for all your help.
David