Converters
From n² wiki
Contents |
[edit] Download
svn co http://n2.talis.com/svn/playground/kwijibo/converters/
[edit] Intro
Converters is a PHP library of classes for converting RDF/XML and SPARQL/XML into PHP data structures. RDF/XML can be converted into an associative array structure following the RDF/JSON Specification (and filtered, modified, and reserialised as RDF/XML and Turtle), SPARQL is converted into an associative array similar to the SPARQL JSON serialisation specification.
[edit] API Overview
RDFXML
->value() // an RDF/XML string
->to_triples()
->to_resources() //needs namespaces array
Triples
->value() // an array of triples, as returned by the ARC RDF/XML parser
->to_resources()
ResourcesArray
->value() // an associative array like the RDF/JSON structure
->to_rdfxml()
->to_turtle()
->to_resource($uri) // creates and returns a ResourceArray object of the
->filter_by_language($lang) //returns resources with only literals of the same, or unspecified, language
->filter_by_object_value($value) // returns only resources with a given object value.
ResourceArray
->value() // an associative array like the RDF/JSON structure, containing only one resource
->property($property_qname) // returns the value of the given property of this resource.
->get_title() //returns the foaf:name, dc:title, or rdfs:label of the resource
SPARQL_XML
->value() // an XML document string
->to_boolean() //returns a boolean from an ASK query result document
->to_array() // returns an associative array from a select query result
URI
->value() // a URI string
->to_qname() //needs namespaces array
->to_term() // returns the part of the uri after the last # or /
->to_namespace() returns the part of the URI up to and including the last # or /
Qname
->value() // a qname, eg: 'rdf:type'
->to_uri() //needs namespaces array
->to_eRDF_value() // replaces the first : for a - .
[edit] Usage:
$namespaces = array ( 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'sioc' => 'http://rdfs.org/sioc/ns#', 'dc' => 'http://purl.org/dc/elements/1.1/', 'terms' => 'http://purl.org/dc/terms/', 'foaf' => 'http://xmlns.com/foaf/spec/', 'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#', 'rss' => 'http://purl.org/rss/1.0/', 'owl' => 'http://www.w3.org/2002/07/owl#', 'rel' => 'http://purl.org/vocab/relationship/', 'ibis' => 'http://dannyayers.com/2003/12/ibis.rdfs#', 'doap' => 'http://usefulinc.com/ns/doap#', 'frbr' => 'http://purl.org/vocab/frbr/core#', );
You can optionally pass in an array of namespace prefixes when you initiate the Convertor class. Alternatively (or as well), you can supply namespaces as a second parameter to methods that can (or need to) use prefixes (such as to_qname())
The RDFXML converter needs the ARC RDF/XML parser, available from: the Semsol website
$arc2_location = 'lib/arc2/ARC2.php'; //default path
$Converter = new Converter($namespaces, $arc2_location);
$ResourcesObject = $Converter->RDFXML->to_resources($rdfxml_string);
$resources_assoc_array = $ResourcesObject->value();
$resources_assoc_array == array(
'http://example.org/test/JohnSmith'=>
array(
'http://xmlns.com/foaf/spec/name' =>
array(
array('type'=>'literal','value'=>'John Smith')
),
'http://xmlns.com/foaf/spec/mbox' => array(
array('type'=>'uri','value'=>'mailto:john@example.org')
),
'http://www.w3.org/2000/01/rdf-schema#label' => array(
array('type'=>'literal','value'=>'Label','lang'=>'en-gb')
),
),
); // true
$ResourceObject = $ResourcesObject->to_resource('http://example.org/test/JohnSmith');
$ResourcesObject->get_title() == 'John Smith'; // true
$Convert->URI->to_qname('http://xmlns.com/foaf/spec/name')->value() == 'foaf:name'; //true
$Convert->URI->to_term('http://xmlns.com/foaf/spec/name') == 'name'; //true

