How to extract dataproperty value from sparql results?

I am using jena to fire query on ontology. I am getting results as follows when I run the query to get the value of data property assigned to object Student1

"Ashutosh"^^http://www.w3.org/2001/XMLSchema#string is not a URI node

What should I do if I want result as follows

"Ashutosh" or `Ashutosh`

This is my java code. What should I do if I want answer as given above?

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.util.FileManager;


public class abc {
static String avg;
static String sq="Charlie";
static String tq="'";
static String data;


public static void main(String[] args) {

    // TODO Auto-generated method stub

sparqltest();}


static void sparqltest()
{

    FileManager.get().addLocatorClassLoader(abc.class.getClassLoader());
    Model model= FileManager.get().loadModel("C:/Users/avg/workspacejena32/Jena/bin/ashu.rdf");
    //System.out.println("Connected...");
    String queryString="prefix avg:<http://www.semanticweb.org/avg/ontologies/2016/3/untitled-ontology-14#>" +
                       "prefix rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
                       "SELECT * WHERE {" +
                       "avg:Student1 avg:Name ?x . filter isLiteral(?x)" +
                       "}";
    //System.out.println("Connected...");


    Query query= QueryFactory.create(queryString);
    QueryExecution qexec=QueryExecutionFactory.create(query, model);

 try {
    ResultSet rs = qexec.execSelect();
    for ( ; rs.hasNext() ; )
    {
    QuerySolution soln = rs.nextSolution() ;
    RDFNode a = soln.get("x") ;
    data = a.asNode().getLocalName();


} }


    catch(Exception e)
{
    System.out.println(e.getMessage());
}   
 } 
 } 

Upvotes: 1

Views: 780

Answers (1)

oole
oole

Reputation: 352

I think what you want to do can be achieved by doing something like:

String aS = a.asNode().getLiteral();

This will give you the Literal without quotations. Like

"Ashutosh"^^http://www.w3.org/2001/rdf-schema#Literal

If you want the pure lexical form of the Literal you want to use this:

String aS = a.asNode().getLiteralLexicalForm();

This will return only the Literal of the Node without the datatype. (But beware it only works if the Node really is a literal)

Upvotes: 2

Related Questions