[英]SPARQL Query for searching all properties of one type to match a string
我正在嘗試構建一個SPARQL查詢來搜索類型的所有屬性。 例如,我想搜索Person類型( http://topbraid.org/examples/kennedys#Person )並返回其屬性與輸入字符串“ken”匹配的Person實例。 這是我正在使用的當前查詢:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ui: <http://uispin.org/ui#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT *
WHERE {
GRAPH <http://topbraid.org/examples/kennedys> {
?subject a <http://topbraid.org/examples/kennedys#Person>;
?property ?value .
FILTER EXISTS {
?subject ?anyProperty ?anyValue .
FILTER (isLiteral(?anyValue) && regex(xsd:string(?anyValue), "1956", "i")) .
} .
}
}
以上查詢返回給我:
這在匹配屬性birthYear並返回3個實例時是正確的。 但是當我搜索“男性”,並希望返回與男性匹配的所有Person實例時,我沒有達到我的預期。
詢問
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ui: <http://uispin.org/ui#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT *
WHERE {
GRAPH <http://topbraid.org/examples/kennedys> {
?subject a <http://topbraid.org/examples/kennedys#Person>;
?property ?value .
FILTER EXISTS {
?subject ?anyProperty ?anyValue .
FILTER (isLiteral(?anyValue) && regex(xsd:string(?anyValue), "male", "i")) .
} .
}
}
我的查詢有問題嗎?
你沒有做錯任何事。 第二個結果令人驚訝的是什么? 你正在做一個不區分大小寫的正則表達式匹配,並且有三重奏
:SydneyLawford :middleName "Maleia"
"Maleia"
肯定與"male"
相匹配(因為比賽不區分大小寫)。 如果您希望該值具有完全字符串"male"
,請嘗試稍微不同的filter
:
FILTER (isLiteral(?anyValue) && lcase(str(?anyValue)) = "male")
所以這是最后的查詢。 我把它分成兩部分。 第一個將搜索Person類型的所有屬性值。 第二部分獲取值中對象的rdfs:標簽。 所以這個例子就是財產價值http://topbraid.org/examples/kennedys#female會有一個標簽為女性。 該查詢將與該匹配。 這兩個結果將在最后結合。
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ui: <http://uispin.org/ui#>
SELECT distinct ?subject
WHERE {
{
GRAPH <http://topbraid.org/examples/kennedys> {
?subject a <http://topbraid.org/examples/kennedys#Person> .
?subject ?property ?value .
FILTER (isLiteral(?value) && regex(str(?value), "1956", "i")) .
}
}
UNION
{
GRAPH <http://topbraid.org/examples/kennedys> {
?subject a <http://topbraid.org/examples/kennedys#Person> .
?subject ?property ?value .
?property rdfs:label ?propertyName .
?value rdfs:label ?valueName .
FILTER regex(?valueName, "female") .
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.