If you run a SPARQL query, you get your output in dotnetrdf as

?a= "some text", ?b="some more text", ?c="some text, with a comma!"

How can I format this result to display properly on a website so it gets rid of the variables declared in the sparql query and shows text only like this

some text
some more text
some text, with a comma!

I was thinking of splitting the text at a comma and using substring to get rid of the variables but my text itself contains comma so I cannot use that method. Is there any other way to do it?

asked 28 Feb '12, 08:16

Semantic_Developer's gravatar image

Semantic_Dev...
17914
accept rate: 0%

How about getting back the results in a standard output format (e.g., XML or JSON) and transforming it to what you want?

http://www.w3.org/TR/rdf-sparql-XMLres/

http://www.w3.org/TR/rdf-sparql-json-res/

(28 Feb '12, 09:51) Signified ♦ Signified's gravatar image

You can format SPARQL results in several ways, the way you have shown is just calling the ToString() method on a result which is the crudest way. I'll address your question directly and give you some information on other ways to format results.

A useful thing to know about accessing SPARQL results, firstly you can use the standard indexer syntax to access specific variables on a result e.g.

//Assuming I have a result set in the variable rest
foreach (SparqlResult r in rset)
{
  if (r.HasVariable("var"))
  {
    INode n = rset["var"];
  }
}

Which will give you either an INode instance from which you can get the Node type and access various node type specific fields or an error if the given variable is not present - note the use of the HasVariable("var") method to check that a variable is present before you try to retrieve it.

Your specific question

Assuming you want raw text ignoring any datatype/language present and any other formatting niceties you could do the following:

foreach (SparqlResult r in rset)
{
  foreach (String var in r.Variables)
  {
    if (r.HasBoundValue(var))
    {
      INode n = r[var];
      String output;
      switch (n.NodeType)
      {
         case NodeType.Literal:
           //For literals you only want the lexical value only so need to cast to
           //literal and access the Value property
           output = ((ILiteralNode)n).Value;
           break;
         default:
           //For any other node we just want the string form so
           //ToString() is sufficient
           output = n.ToString();
           break;
      }
    }
  }
}

Other Ways to format SPARQL Results

1 - Manually

Simply iterator over the SparqlResultSet object and add some rendering logic yourself e.g.

foreach (SparqlResult r in rset)
{
  //Take action to format result here
}

Sounds like you are doing this currently and just calling r.ToString()

2 - Format with a ISparqlResultsWriter

There are a variety of implementations of ISparqlResultsWriter which take a SparqlResultSet and output it all at once in formats such as SPARQL XML, SPARQL JSON, SPARQL CSV, SPARQL TSV and HTML.

e.g.

SparqlXmlWriter writer = new SparqlXmlWriter();
writer.Save(rset, "example.srx");

3 - Format with a INodeFormatter

The INodeFormatter is an interface designed for providing reusable stateless logic for the formatting of nodes from any source. You can use any of the pre-existing implementations or implement your own either from scratch or deriving from an existing implementation.

Usage is very simple e.g.

NTriplesFormatter formatter = new NTriplesFormatter();
foreach (SparqlResult r in rset)
{
  Console.WriteLine(r.ToString(formatter);
}

This will still print the variables but you could avoid that by doing the following:

NTriplesFormatter formatter = new NTriplesFormatter();
foreach (SparqlResult r in rset)
{
  foreach (String var in r.Variables)
  {
    if (r.HasBoundValue(var))
    {
      Console.WriteLine(r[var].ToString(formatter);
    }
  }
}
permanent link

answered 28 Feb '12, 15:36

Rob%20Vesse's gravatar image

Rob Vesse ♦
14.0k1715
accept rate: 29%

edited 14 Jan '13, 08:02

@Rob Vesse: When I try the above mentioned code, it is giving me an error saying,

'VDS.RDF.Query.SparqlResult' does not contain a definition for 'HasVariable' and no extension method 'HasVariable' accepting a first argument of type 'VDS.RDF.Query.SparqlResult' could be found...

Do I need to add any special packages or references..??

(09 Jan '13, 13:21) George Abraham George%20Abraham's gravatar image

Was my mistake, should be HasValue() not HasVariable()

(14 Jan '13, 08:01) Rob Vesse ♦ Rob%20Vesse's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×1,328
×80

question asked: 28 Feb '12, 08:16

question was seen: 1,831 times

last updated: 14 Jan '13, 08:02