From: Rob V. <rv...@vd...> - 2011-05-10 10:09:35
|
Hi Jens There is no way to add a Namespace to the parser, it must be defined in the query but you can do this without lots of nasty string concatenation by using the SparqlParameterizedString class. Essentially this has a Namespaces property that can be used to build a namespace map and automatically inserts the appropriate prefix declarations into the querystring when it is passed to the parser. SparqlParameterizedString queryString = new SparqlParameterizedString(); //Define as many namespaces as you want - note that the Namespace Map for a Parameterized //String will be empty by default so you typically need to define rdf namespace as well queryString.Namespaces.AddNamespace("rdf", new Uri(RdfSpecsHelper.RdfType)); queryString.Namespaces.AddNamespace("foaf", new Uri("http://xmlns.com/foaf/0.1/")); //Use CommandText to set the actual query text queryString.CommandText = "SELECT ?image WHERE {?image rdf:type foaf:Image}"; //Then parse the query from a string as usual SparqlQueryParser parser = new SparqlQueryParser(); SparqlQuery query = parser.ParseFromString(queryString); //Finally go ahead and process the query however you wish Hope this helps, if you have any further questions please let me know Regards, Rob Vesse ---------------------------------------- From: "Jens Panneel" <jen...@gm...> Sent: 10 May 2011 10:52 To: dot...@li... Subject: SPAM-LOW: [dotNetRDF-Support] NamespaceMap on parsetime Hi I am doing the folowing: http://pastebin.com/rfTC0fZu /* * Getting an error from ParseFromString, saying: * The Namespace URI for the given Prefix 'foaf' is not known by the in-scope NamespaceMapper * * My question how can i add a Namespace before the parsing? * I don't like the idea of manualy concatenating a lot of prefix strings to build my queries. * */ ISparqlQueryProcessor processor = new RemoteQueryProcessor(new SparqlRemoteEndpoint(new Uri("http://localhost:11025/sparql/query"))); SparqlQueryParser sparqlparser = new SparqlQueryParser(); SparqlQuery query = sparqlparser.ParseFromString("SELECT ?image WHERE {?image rdf:type foaf:Image}"); query.NamespaceMap.AddNamespace("foaf", new Uri("http://xmlns.com/foaf/0.1/")); Object results = processor.ProcessQuery(query); So is there a way to add a namespace to the parser? Thanks JPanneel |