Re: Help with Musicbrainz

> ====== SNIP ============
>
> <title>Error 400 Parse error:
> prefix foaf: &lt;http://xmlns.com/foaf/0.1/&gt;  prefix rdf:  &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns
>
> Encountered "&lt;" at line 1, column 57.
> Was expecting:
>    &lt;IRIref&gt; ...
>    </title>
> </head>

i believe your mistake is in the url encoding.  you're using the
HTML-style encoding where < becomes &lt;

actually, you can just leave '<' and '>' unencoded i think.  i'm using
python to make the query -

In [1]: import urllib

In [2]: query = '''prefix foaf: <http://xmlns.com/foaf/0.1/>  prefix
rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  SELECT ?type
WHERE { ?name foaf:name 'Screwtape' . ?name rdf:type ?type  }'''

In [3]: query_esc = urllib.quote(query)

In [4]: response =
urllib.urlopen('http://dbtune.org/musicbrainz/sparql?query='+query_esc)

In [5]: print response.read()
<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
 <head>
   <variable name="type"/>
 </head>
 <results>
   <result>
     <binding name="type">
       <uri>http://purl.org/ontology/mo/MusicArtist</uri>
     </binding>
   </result>
 </results>
</sparql>

notice i'm just using the handy urllib library to do the url escaping.
 which ever language you're using, there is surely a similar library.

also for completeness, here's what the percent encoded query looks like:

In [6]: print query_esc
prefix%20foaf%3A%20%3Chttp%3A//xmlns.com/foaf/0.1/%3E%20%20prefix%20rdf%3A%20%20%3Chttp%3A//www.w3.org/1999/02/22-rdf-syntax-ns%23%3E%20%20SELECT%20%3Ftype%20WHERE%20%7B%20%3Fname%20foaf%3Aname%20%27Screwtape%27%20.%20%3Fname%20rdf%3Atype%20%3Ftype%20%20%7D

I also recommend SPARQLWrapper for python for querying endpoints.
http://sparql-wrapper.sourceforge.net/

Hope this helps!

-kurt

Received on Tuesday, 25 August 2009 09:30:51 UTC