Hi,
I have a SPARQL Query that I am executing through dotNetRDF. The query is given as below:
Object results = store.ExecuteQuery("PREFIX pre:<http://www.owl-ontologies.com/LRBrain_Simple.owl#>" + "SELECT ?name ?Characteristics ?Behaviour " +
"WHERE { ?Person pre:hasCharacteristics ?Characteristics. ?Person pre:name ?name. ?Characteristics pre:showsBehaviour ?Behaviour} " +
"ORDER BY ?name");
When I display the result in Console using the following:
SparqlResultSet rset = (SparqlResultSet)results;
foreach (SparqlResult result in rset)
{
Console.WriteLine(result.ToString());
}
The result is displayed as follows:
?Characteristics = http://www.owl-ontologies.com/LRBrain_Simple.owl#Intutive , ?Behaviour = http://www.owl-ontologies.com/LRBrain_Simple.owl#RightBrained , ?name = A^^http://www.w3.org/2001/XMLSchema#string
?Characteristics = http://www.owl-ontologies.com/LRBrain_Simple.owl#Linear , ?Behaviour = http://www.owl-ontologies.com/LRBrain_Simple.owl#LeftBrained , ?name = A^^http://www.w3.org/2001/XMLSchema#string
I want to format this output as, say, a tabular format like:
Name | Characteristics | Behaviour
A Intutive RightBrained
A Linear LeftBrained
or atleast like:
Name = A
Characteristics = Intutive
Behaviour = RightBrained
Name = A
Characteristics = Linear
Behaviour = LeftBrained
I went through the link: How can you format SPARQL Query Results in dotnetrdf? and tried working out the solution provided, but the line if (r.HasVariable(var) && r[var] != null)
shows an error as there is no HasVariable function for SparqlResult.
So I modified the code, leaving out the r.HasVariable and the following code:
SparqlResultSet rset = (SparqlResultSet)results;
foreach (SparqlResult r in rset)
{
foreach (String var in r.Variables)
{
if (r[var] != null)
{
INode n = r[var];
String output;
switch (n.NodeType)
{
case NodeType.Literal:
output = ((ILiteralNode)n).Value;
Console.Write(output);
break;
case NodeType.Uri:
output = ((IUriNode)n).ToString();
Console.Write(output);
break;
default:
output = n.ToString();
Console.Write(output);
break;
}
}
Console.WriteLine();
}
}
The above code gives me the answer as:
http://www.owl-ontologies.com/LRBrain_Simple.owl#Intutive
http://www.owl-ontologies.com/LRBrain_Simple.owl#RightBrained
A
http://www.owl-ontologies.com/LRBrain_Simple.owl#Linear
http://www.owl-ontologies.com/LRBrain_Simple.owl#LeftBrained
A
How can I split only the word Intutive from http://www.owl-ontologies.com/LRBrain_Simple.owl#Intutive and also format it in the way mentioned above?
asked
14 Jan, 01:21
George Abraham
249●1●7
accept rate:
16%
Oops, a typo on my part,
HasVariable()should have beenHasValue()- will go and correct the other answer@Rob Vesse: Ok...It happens :)