How can I parse a SPARQL result set

433 views
Skip to first unread message

Jim Amsden

unread,
Jul 12, 2019, 3:09:08 PM7/12/19
to rdflib-dev
I'm using REST to sent a SPARQL query to an endpoint and get back a SPARQL result set:
```
<?xml version="1.0"?>
  <head>
    <variable name="validate"/>
    <variable name="cr"/>
    <variable name="tc"/>
    <variable name="req"/>
  </head>
  <results>
    <result>
      <binding name="validate">
      </binding>
      <binding name="cr">
      </binding>
      <binding name="tc">
      </binding>
    </result>
...
```
How can I use rdflib (or something more appropriate to parse this into something I can iterate on? 

I tried using an XMLResult, but I'm not clear what its constructor expects its source object to be - something that can be read? In any case, it doesn't accept a string. 

I could just use XML, but I was hoping to eventually find an easy way to convert this result set into a DataFrame.


Nicholas Car

unread,
Jul 12, 2019, 11:42:17 PM7/12/19
to rdfli...@googlegroups.com
Have you seen the SPARQLWrapper module in the rdflib family: https://github.com/rdflib/SPARQLWrapper? 

The first example in the README iterates over a SELECT.

Nick

— 
Nicholas Car
Data Systems Architect
SURROUND Australia
0477 560 177
--
http://github.com/RDFLib
---
You received this message because you are subscribed to the Google Groups "rdflib-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rdflib-dev+...@googlegroups.com.
To post to this group, send email to rdfli...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rdflib-dev/c3f1b728-57cf-43cc-a324-5f600a89b592%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sergio Fernández

unread,
Jul 14, 2019, 12:16:54 AM7/14/19
to rdfli...@googlegroups.com
In Python it's much easier to parse the response if you request the results in JSON format. But you don't need to go that low level, SPARQLWrapper takes care of that: https://rdflib.github.io/sparqlwrapper


Jim Amsden

unread,
Jul 16, 2019, 9:28:34 AM7/16/19
to rdflib-dev
The SPARQL endpoint I'm using is a little fussy about the POST its willing to accept, so I need more low level HTTP access. I'm thinking I should just parse the XML SPARQL result set and convert to a pandas DataFrame. 

Marijane White

unread,
Jul 16, 2019, 2:10:48 PM7/16/19
to rdfli...@googlegroups.com

You might try https://github.com/lawlesst/sparql-dataframe, which was recently updated to handle POST requests.

 

 

Marijane White, M.S.L.I.S.

Data Librarian, Assistant Professor

Oregon Health & Science University Library

 

Phone: 503.494.3484

Email: whi...@ohsu.edu

ORCiD: https://orcid.org/0000-0001-5059-4132

--

http://github.com/RDFLib
---
You received this message because you are subscribed to the Google Groups "rdflib-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rdflib-dev+...@googlegroups.com.
To post to this group, send email to rdfli...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages