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

如何将anyURI类型的CURIe字符串转换为真实CURIe或IRI?

解决方案

SPARQL实现

tarql:expandPrefixedName是TarQL专属函数,ARQ和GraphDB默认不支持,可通过字符串拆分+命名空间拼接的方式实现需求:

已知前缀对应IRI的场景

如果明确external前缀对应的IRI是https://example.org/external/,直接用字符串处理函数拆分CURIe并拼接:

PREFIX source: <https://example.org/source>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT ?s ?p ?expandedIRI
WHERE {
  ?s ?p ?o .
  FILTER(datatype(?o) = xsd:anyURI)
  # 拆分CURIe字符串为前缀和局部名
  BIND(STR(?o) AS ?curieStr)
  BIND(STRBEFORE(?curieStr, ":") AS ?prefix)
  BIND(STRAFTER(?curieStr, ":") AS ?localName)
  # 拼接生成目标IRI
  BIND(IRI(CONCAT("https://example.org/external/", ?localName)) AS ?expandedIRI)
}

动态处理命名空间的场景

如果需要适配多个前缀,或调用数据库已注册的命名空间映射,可使用ARQ和GraphDB都支持的namespace-uri-for-prefix扩展函数:

PREFIX source: <https://example.org/source>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT ?s ?p ?expandedIRI
WHERE {
  ?s ?p ?o .
  FILTER(datatype(?o) = xsd:anyURI)
  BIND(STR(?o) AS ?curieStr)
  BIND(STRBEFORE(?curieStr, ":") AS ?prefix)
  BIND(STRAFTER(?curieStr, ":") AS ?localName)
  # 动态获取前缀对应的命名空间IRI
  BIND(namespace-uri-for-prefix(?prefix) AS ?nsIRI)
  # 拼接成完整IRI(注意命名空间是否以/结尾,按需调整)
  BIND(IRI(CONCAT(STR(?nsIRI), "/", ?localName)) AS ?expandedIRI)
}

Python rdflib实现

利用rdflib的命名空间管理功能,可快速完成CURIe的展开:

from rdflib import Graph, Namespace, XSD

# 初始化图并加载数据
g = Graph()
g.parse(data="""
@prefix source: <https://example.org/source> .
@prefix external: <https://example.org/external> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

source:sample1 source:external_identifiers "external:0110680"^^xsd:anyURI .
""")

# 注册命名空间
EXTERNAL = Namespace("https://example.org/external/")
g.bind("external", EXTERNAL)

# 遍历并展开CURIe
for s, p, o in g:
    if isinstance(o, Literal) and o.datatype == XSD.anyURI:
        expanded_iri = g.namespace_manager.expand_curie(str(o))
        print(f"原三元组: {s} {p} {o}")
        print(f"展开后三元组: {s} {p} {expanded_iri}\n")

运行输出:

原三元组: https://example.org/source#sample1 https://example.org/source#external_identifiers "external:0110680"^^http://www.w3.org/2001/XMLSchema#anyURI
展开后三元组: https://example.org/source#sample1 https://example.org/source#external_identifiers https://example.org/external/0110680

内容的提问来源于stack exchange,提问作者Mark Miller

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 23:53:25