如何生成SPARQL查询提取RDF中publisher的@type值
用SPARQL从JSON-LD数据中提取第一个publisher的@type值
需求说明
给定提供的JSON-LD格式RDF数据,生成SPARQL查询,找到第一个出现的publisher块并提取其@type值,预期结果为Organization。
SPARQL查询语句
PREFIX schema: <https://schema.org/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT ?publisherTypeLabel WHERE { ?resource schema:publisher ?publisher . ?publisher rdf:type ?publisherType . BIND(STRAFTER(STR(?publisherType), STR(schema:)) AS ?publisherTypeLabel) } ORDER BY ?resource LIMIT 1
查询解释
- 前缀定义:
schema:对应schema.org的命名空间,rdf:对应RDF核心标准命名空间,简化查询中的URI书写。 - 匹配资源:
?resource schema:publisher ?publisher匹配所有包含发布者信息的资源(对应JSON-LD@graph里带有publisher字段的条目)。 - 获取类型:
?publisher rdf:type ?publisherType获取发布者资源的类型(对应JSON-LD中publisher块的@type)。 - 提取类型名称:
BIND函数从类型的完整URI中提取本地名称(例如从https://schema.org/Organization提取出Organization)。 - 取第一个结果:
ORDER BY ?resource确保按资源在数据中的出现顺序排序,LIMIT 1只返回第一个匹配的结果。
查询结果
执行上述查询后,返回的?publisherTypeLabel值为Organization,符合预期。
内容的提问来源于stack exchange,提问作者Henley Wing Chiu
相关产品推荐
相关产品推荐

