如何从 sparql 结果中提取数据属性值?

How to extract dataproperty value from sparql results?

我正在使用 jena 在 ontology 上触发查询。当我 运行 查询以获取分配给对象 Student1

的数据 属性 的值时,我得到的结果如下
"Ashutosh"^^http://www.w3.org/2001/XMLSchema#string is not a URI node

想要结果如下怎么办

"Ashutosh" or `Ashutosh`

这是我的 java 代码。如果我想要上面给出的答案怎么办?

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());
}   
 } 
 } 

我想你想做的事情可以通过做这样的事情来实现:

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

这将为您提供不带引号的文字。喜欢

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

如果您想要文字的纯 词法 形式,您可以使用此:

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

这将 return 只有没有数据类型的节点的文字。 (但要注意它只有在 Node 确实是文字时才有效)