-
Notifications
You must be signed in to change notification settings - Fork 0
UserGuide Result Formatting
Home > User Guide > Advanced SPARQL > Result Formatting
When you make a SPARQL query as detailed on the Querying with SPARQL you typically get back a SparqlResultSet object. While you can trivially dump results to strings using the ToString() method this doesn't give you a particularly pretty output, this page details various methods by which you can format results for display.
dotNetRDF supports a powerful Formatting API which is discussed in general elsewhere, this can be leveraged for the purposes of formatting results. Since values in each column of a SparqlResult are just normal INode values you can use any of the existing INodeFormatter implementations for this.
For example:
//Assume we already have our query results in the variable results
//Create a formatter
INodeFormatter formatter = new TurtleFormatter();
foreach (SparqlResult result in results)
{
Console.WriteLine(result.ToString(formatter));
}The above will print out results using Turtle formatting for the terms, exact formatting will vary depending on the formatter used and arguments passed to the constructor.
Sometimes you may prefer to extract the actual values from the nodes as strings e.g.
//Assume we have our results in the variable results
foreach (SparqlResult result in results)
{
INode n;
String data;
if (result.TryGetValue("var", out n))
{
switch (n.NodeType)
{
case NodeType.Uri:
data = ((IUriNode)n).Uri.AbsoluteUri;
break;
case NodeType.Blank:
data = ((IBlankNode)n).Label;
break;
case NodeType.Literal:
//You may want to inspect the DataType and Language properties and generate
//a different string here
data = ((ILiteralNode)n).Value;
break;
default:
throw new RdfOutputException("Unexpected Node Type");
}
}
else
{
data = String.Empty;
}
//Do what you want with the extracted string
}- Getting Started
- Library Overview
- Hello World
- Reading RDF
- Writing RDF
- Working With Graphs
- Typed Values and Lists
- Working with Triple Stores
- Building SPARQL
- Querying with SPARQL
- Updating with SPARQL
- Exceptions
- Equality and Comparison
- Event Model
- Utility Methods
- Extension Methods
- Using the Namespace Mapper
- Storage API
- Advanced SPARQL
- Ontology API
- Inference and Reasoning
- ASP.NET Integration
- Global Options
- Formatting API
-
Configuration API
- Graphs
- Triple Stores
- Object Factories
- Readers and Writers
- SPARQL Endpoints
- Query Processors
- Update Processors
- Protocol Processors
- SPARQL Datasets
- SPARQL Expression Factories
- SPARQL Operators
- SPARQL Optimisers
- Full Text Query
- Reasoners
- Storage Providers
- User Groups
- Permissions
- Users
- Static Options
- Proxy Servers
- Handlers API
- JSON-LD API
- Tools