Pregunta

with rdf triples like this:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE rdf:RDF [<!ENTITY rdf 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'>
<!ENTITY rdfs 'http://www.w3.org/2000/01/rdf-schema#'>
<!ENTITY xsd 'http://www.w3.org/2001/XMLSchema#'>]>
<rdf:RDF xmlns:xsd="http://www.w3.org/2001/XMLSchema#" 
         xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
         xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:dnr="http://www.dotnetrdf.org/configuration#"
         xml:base="http://www.example.org/"
         xmlns:nss="http://www.example.org/startTime"
     xmlns:nse="http://www.example.org/endTime">

  <rdf:Description rdf:about="Fadi">
    <ns0:eat xmlns:ns0="http://example.org/">Apple</ns0:eat>
    <nss:startTime>00:01:38</nss:startTime>
    <nse:endTime>00:01:39</nse:endTime>
  </rdf:Description>

  <rdf:Description rdf:about="I">
    <ns1:love xmlns:ns1="http://example.org/">You</ns1:love>
    <nss:startTime>00:05:35</nss:startTime>
    <nse:endTime>00:06:39</nse:endTime>
  </rdf:Description>
</rdf:RDF>

how can i query about subject or object?

my code:

Graph myGraph = new Graph();
FileLoader.Load(myGraph, "C:\\Users\\hasoOn\\Desktop\\tt.rdf");                

TripleStore store = new TripleStore();
store.Add(myGraph);

SparqlParameterizedString queryString = new SparqlParameterizedString();
queryString.CommandText = "PREFIX ex: <http://example.org/> SELECT * WHERE { { ex:fadi ?p ?o } UNION { GRAPH ?g { ?s ?p ?o } } } ";
SparqlQueryParser parser = new SparqlQueryParser();
SparqlQuery query = parser.ParseFromString(queryString.CommandText);
SparqlResultSet results = (SparqlResultSet)store.ExecuteQuery(query);

if (results is SparqlResultSet)
{
    SparqlResultSet rset = (SparqlResultSet)results;
    foreach (SparqlResult result in rset)
    {
        Console.WriteLine(result);   
    }
} 

with my code below i get all answers from the query. can anyone help me to get for just subject = fadi?

¿Fue útil?

Solución

The second part of your union is responsible for your result : you query for all triples from all the graphs in your store since no variable is bound. The first part actually returns nothing.

You have two solutions here:

1) you add your graph in the default dataset with:

myGraph.baseUri = null

Before adding the store.add(myGraph) statement Then the query SELECT * { ex:Fadi ?p ?o } should be enough

2) you change the query to :

Select * from _yourGraphUriHere { ex:Fadi ?p ?o}

That should get you what you want.

By the way, remember that Uris are case sensitive so ex:fadi is not what you want to query on but ex:Fadi

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top