SPARQL查询求助:如何检索痛苦程度为‘not too disturbing’的人员
修正你的SPARQL查询问题
你的查询主要存在两个问题,下面是具体的修正方案和解释:
问题1:非法的URI格式
ocd:Not too disturbing包含空格,这违反了RDF资源URI的命名规范——URI中不允许出现空格。这类资源通常会用下划线或驼峰式命名,比如ocd:notTooDisturbing或ocd:Not_too_disturbing。
问题2:不必要的强制属性约束
当前查询要求?person必须同时具备hasInsight、hasThought、hasEmotion和hasDistressLevel四个属性,只要其中一个属性缺失,该人员就不会被返回结果。如果这些属性不是查询必需的条件,应该用OPTIONAL包裹。
修正后的查询(分两种场景)
场景1:痛苦程度是一个规范的无空格URI资源
如果not too disturbing对应的是一个符合URI规范的资源(比如ocd:notTooDisturbing),使用以下查询:
SELECT * WHERE { ?person ocd:hasDistressLevel ?severitycontrol. # 用OPTIONAL包裹非必需属性,避免过滤掉缺少这些数据的人员 OPTIONAL { ?person ocd:hasInsight ?insight. } OPTIONAL { ?person ocd:hasThought ?thought. } OPTIONAL { ?person ocd:hasEmotion ?emotion. } FILTER (?severitycontrol = ocd:notTooDisturbing) }
场景2:痛苦程度是资源的文本标签
如果not too disturbing是该资源的显示文本(即rdfs:label属性值),则需要匹配标签而非直接比较URI:
SELECT * WHERE { ?person ocd:hasDistressLevel ?severitycontrol. OPTIONAL { ?person ocd:hasInsight ?insight. } OPTIONAL { ?person ocd:hasThought ?thought. } OPTIONAL { ?person ocd:hasEmotion ?emotion. } ?severitycontrol rdfs:label "not too disturbing"@en. # 如果标签没有语言限制,可以去掉@en后缀 }
内容的提问来源于stack exchange,提问作者Areej
相关产品推荐
相关产品推荐

