我正在尝试从land注册API(http://landregistry.data.gov.uk/app/hpi/qonsole)获取一些数据并使用SPARQL查询。我是sparql查询的新手,不知道在我的param.query对象中传递什么以及如何传递所需的前缀。
//myCode
var req = {
method: 'GET',
url: 'http://landregistry.data.gov.uk/landregistry/query',
headers: { 'Content-type' : 'application/x-www-form-urlencoded',
'Accept' : 'application/sparql-results+json' },
params: {
query :"select ?paon ?saon ?street ?town ?county ?postcode ?amount ?date where {?addr lrcommon:postcode "PL6 8RU"} limit 10",
format: "json"
}
};
console.log(req)
// SparQL Query
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix sr: <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/>
prefix lrhpi: <http://landregistry.data.gov.uk/def/hpi/>
prefix lrppi: <http://landregistry.data.gov.uk/def/ppi/>
prefix skos: <http://www.w3.org/2004/02/skos/core#>
prefix lrcommon: <http://landregistry.data.gov.uk/def/common/>
#-- Returns the Price Paid data from the default graph for each transaction record having
#-- an address with the given postcode.
#-- The postcode to query is set in the line - ?address_instance common:postcode "PL6 8RU"^^xsd:string .
SELECT ?paon ?saon ?street ?town ?county ?postcode ?amount ?date
WHERE
{
?transx lrppi:pricePaid ?amount ;
lrppi:transactionDate ?date ;
lrppi:propertyAddress ?addr.
?addr lrcommon:postcode "PL6 8RU"^^xsd:string.
?addr lrcommon:postcode ?postcode.
OPTIONAL {?addr lrcommon:county ?county}
OPTIONAL {?addr lrcommon:paon ?paon}
OPTIONAL {?addr lrcommon:saon ?saon}
OPTIONAL {?addr lrcommon:street ?street}
OPTIONAL {?addr lrcommon:town ?town}
}
ORDER BY ?amount
答案 0 :(得分:4)
你到了一半。您可以将Angular的$http
服务与您开发的请求对象一起使用(假设您正确填充了查询字符串)。 $http
服务返回一个承诺,您可以使用可用的快捷方法(.success
和.error
)来解析该承诺。对于这些,您可以指定一个回调函数,该函数在响应准备就绪时异步调用。例如:
$http({
url: 'http://landregistry.data.gov.uk/landregistry/query',
headers: { 'Content-type' : 'application/x-www-form-urlencoded',
'Accept' : 'application/sparql-results+json' },
method: "GET",
params: {
query : "select * where {?s a ?o} limit 10",
format: "json"
}
})
.success(function(data, status, headers, config) {
// callback called asynchronously when the response is available
$scope.results = data.results.bindings;
})
.error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status
});
这是一个有效的plunker,它执行上述查询并显示结果。
答案 1 :(得分:2)
我认为,您可以使用此答案:How do I POST urlencoded form data with $http in AngularJS?来形成您的请求,并在数据中提供查询:
data: {query: "Put your SPARQL Query here"}
SPARQL查询就是&#34; // SparQL Query&#34;在您的商家信息中,只是JavaScript字符串中的多行文字。 (Creating multiline strings in JavaScript用于JavaScript中的多行字符串)。例如,如果我将它放入文本文件yourquery.txt中,我可以使用curl获取结果:
curl --data-urlencode "query@yourquery.txt" http://landregistry.data.gov.uk/landregistry/query
它返回了很好的json:
{
"head": {
"vars": [ "paon" , "saon" , "street" , "town" , "county" , "postcode" , "amount" , "date" ]
} ,
"results": {
"bindings": [
{
"paon": { "datatype": "http://www.w3.org/2001/XMLSchema#string" , "type": "typed-literal" , "value": "58" } ,
"street": { "datatype": "http://www.w3.org/2001/XMLSchema#string" , "type": "typed-literal" , "value": "PATTINSON DRIVE" } ,
"town": { "datatype": "http://www.w3.org/2001/XMLSchema#string" , "type": "typed-literal" , "value": "PLYMOUTH" } ,
"county": { "datatype": "http://www.w3.org/2001/XMLSchema#string" , "type": "typed-literal" , "value": "CITY OF PLYMOUTH" } ,
"postcode": { "datatype": "http://www.w3.org/2001/XMLSchema#string" , "type": "typed-literal" , "value": "PL6 8RU" } ,
"amount": { "datatype": "http://www.w3.org/2001/XMLSchema#integer" , "type": "typed-literal" , "value": "56000" } ,
"date": { "datatype": "http://www.w3.org/2001/XMLSchema#date" , "type": "typed-literal" , "value": "1999-11-08" }
} ,
...
}
NB。我正在使用POST,而不是GET - 这不是必需的。
注:
?addr lrcommon:postcode "PL6 8RU"^^xsd:string.
?addr lrcommon:postcode ?postcode.
这不太好。最好使用VALUES或BIND:
BIND ("PL6 8RU"^^xsd:string AS ?postcode)
?addr lrcommon:postcode ?postcode.
因为这是大概想要的。有关详细信息,请参阅SPARQL Query language specs。
如果需要在查询中使用JavaScript变量,则应该转义该值并使用连接:
'BIND ("' + escapedPostcodeVar + '"^^xsd:string AS ?postcode)'
有关逃生的内容和方法的详细信息,请参阅documentation。
供参考,使用SPARQL查询:
var myquery = 'prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n\
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n\
prefix owl: <http://www.w3.org/2002/07/owl#> \n\
prefix xsd: <http://www.w3.org/2001/XMLSchema#> \n\
prefix sr: <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/> \n\
prefix lrhpi: <http://landregistry.data.gov.uk/def/hpi/> \n\
prefix lrppi: <http://landregistry.data.gov.uk/def/ppi/> \n\
prefix skos: <http://www.w3.org/2004/02/skos/core#> \n\
prefix lrcommon: <http://landregistry.data.gov.uk/def/common/> \n\
\n\
SELECT ?paon ?saon ?street ?town ?county ?postcode ?amount ?date \n\
WHERE \n\
{ \n\
BIND ("PL6 8RU"^^xsd:string AS ?postcode) \n\
\n\
?addr lrcommon:postcode ?postcode. \n\
\n\
?transx lrppi:pricePaid ?amount ; \n\
lrppi:transactionDate ?date ; \n\
lrppi:propertyAddress ?addr. \n\
\n\
OPTIONAL {?addr lrcommon:county ?county} \n\
OPTIONAL {?addr lrcommon:paon ?paon} \n\
OPTIONAL {?addr lrcommon:saon ?saon} \n\
OPTIONAL {?addr lrcommon:street ?street} \n\
OPTIONAL {?addr lrcommon:town ?town} \n\
} \n\
ORDER BY ?amount \n\
LIMIT 10'
然后在$ http:
params: {query: myquery}