You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在SPARQL查询中转义引号?

SPARQL查询中URI内双引号的转义方案

你的问题根源在于URI中包含双引号,SPARQL对尖括号包裹的URI和字符串字面量的转义规则不同,之前尝试的字符串转义方式并不适用。这里提供两种可行的解决方法:

方法1:使用IRI()函数包裹URI

用单引号将包含双引号的URI括起来,再传入IRI()函数,内部双引号无需额外转义:

SELECT * WHERE { 
  IRI('http://dbpedia.org/resource/Andre_"Dede"_Sabino') rdfs:label ?lbl . 
  ?sub a foaf:Person . 
  ?sub rdfs:label ?lbl . 
  OPTIONAL {?sub dbo:birthDate ?birthDate .} 
  OPTIONAL {?sub dbo:birthPlace ?birthPlace . ?birthPlace a dbo:Location } 
  OPTIONAL {?sub dbo:birthPlace ?birthPlace2 .} 
  ?sub dbo:abstract ?abstract . 
  FILTER(langMatches(lang(?lbl), "en")) 
  FILTER(langMatches(lang(?abstract ), "en")) 
} LIMIT 10

方法2:对双引号进行URL百分号编码

把URI中的双引号"替换为URL编码值%22,直接用尖括号包裹:

SELECT * WHERE { 
  <http://dbpedia.org/resource/Andre_%22Dede%22_Sabino> rdfs:label ?lbl . 
  ?sub a foaf:Person . 
  ?sub rdfs:label ?lbl . 
  OPTIONAL {?sub dbo:birthDate ?birthDate .} 
  OPTIONAL {?sub dbo:birthPlace ?birthPlace . ?birthPlace a dbo:Location } 
  OPTIONAL {?sub dbo:birthPlace ?birthPlace2 .} 
  ?sub dbo:abstract ?abstract . 
  FILTER(langMatches(lang(?lbl), "en")) 
  FILTER(langMatches(lang(?abstract ), "en")) 
} LIMIT 10

为什么之前的方法无效?

  • ""和\"是字符串字面量的转义规则,但尖括号包裹的URI遵循URL编码规范,不适用这类转义。
  • 直接替换为单引号会修改原URI的内容,导致指向错误的资源,因此无法得到正确结果。

内容的提问来源于stack exchange,提问作者Rob Sedgwick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 21:10:02