0

我喜欢列出所有以某个字母开头的药物来填充自动填充文本框。这里是代码

public string[] GetCompletionList(string prefixText)
{
    string rdfDat = AppDomain.CurrentDomain.BaseDirectory + "DrugRDF.rdf";

        List<string> list = new List<string>();
        TripleStore store = new TripleStore();
        Graph rdf = new Graph();
        FileLoader.Load(rdf, rdfDat, new RdfXmlParser());
        store.Add(rdf);
        string tmp = "^" + prefixText;
        string sparqlQuery = "PREFIX  mojLek: <http://www.example.org/mojLek#>"
            + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>"
            + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
            + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
            + "SELECT  ?x"
            + "WHERE {?h mojLek:ime ?x ."
            + "FILTER regex(str(?x),"+tmp+")            }";


        SparqlQueryParser sparqlParser = new SparqlQueryParser();
        SparqlQuery query = sparqlParser.ParseFromString(sparqlQuery);
        Object results = store.ExecuteQuery(query);
        if (results is SparqlResultSet)
        {
            SparqlResultSet r = results as SparqlResultSet;

            foreach (SparqlResult res in r)
            {

                list.Add(res["x"].ToString().ToLower());
            }
        }

        return list.ToArray();


}

但是,如果我用例如 A 尝试它,已经有以 AI 开头的情侣得到了这个错误

VDS.RDF.Parsing.RdfParseException: [Line 1 Column 263] The value &#39;A&#39; is not valid as a QName


at VDS.RDF.Parsing.Tokens.SparqlTokeniser.TryGetQNameOrKeyword()

在 VDS.RDF.Parsing.Tokens.SparqlTokeniser.GetNextToken() 在 VDS.RDF.Parsing.Tokens.TokenQueue.InitialiseBuffer() 在 VDS.RDF.Parsing.SparqlQueryParser.ParseInternal(SparqlQueryParserContext context) 在 VDS.RDF.Parsing.SparqlQueryParser .ParseInternal(TextReader input) at VDS.RDF.Parsing.SparqlQueryParser.ParseFromString(String queryString) at SuggestWebService.GetCompletionList(String prefixText) in d:\Suggest\App_Code\SuggestWebService.cs:line 57

4

2 回答 2

3

在查询字符串中添加换行符以使错误消息更好。

没有 SPARQL 报价

regex(str(?x),"+tmp+")

尝试:

regex(str(?x),'"+tmp+"')

它将单引号放入 SPARQL。小心tmp.

于 2012-08-03T09:43:02.567 回答
0

我以这种方式更改了我的代码,所以它对我有用

        string tmp="^"+prefixText;
        var query = "PREFIX  mojLek: <http://www.example.org/mojLek#>"
            + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>"
            + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
            + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
            + "PREFIX fn: <http://www.w3.org/2005/xpath-functions#>"
            + "SELECT  ?x ?h"
            + "WHERE {?h mojLek:ime ?x ."
            + "FILTER regex(?x,\""+tmp+"\")"
                +"}";
于 2012-08-03T09:42:25.003 回答