使用 SPARQL 的 URI( ) fn w/PREFIX & 包含斜杠的后缀?

Using SPARQL's URI( ) fn w/PREFIX & suffix containing slashes?

我想使用 PREFIX 来简化 URI 创建。欢迎任何帮助我建立一个关于 PREFIX 在 SPARQL 查询中正在做什么的心智模型的建议——它似乎不是一个简单的 key/value 替换。

这里有一些已经尝试过的例子。

工作

除了不使用 PREFIX.

SELECT * WHERE {
    BIND ( URI("http:://www.foo.com/bar/01/grik/234") as ?s ) # (a) works fine
    ?s a ?o .
    # Here (a) works as expected. I'm binding ?s to a specific URI
    # for testing because otherwise it runs too long to debug my query logic.
}
LIMIT 10

我失败了 PREFIX 次尝试

我的实际前缀 URI 片段更长,但这个例子展示了这个想法。

我想将上述 URI 的第一部分 http:://www.foo.com/bar/ 放在 PREFIX 中并使用 01/grik/234作为后缀。

此 return 的变体在 URI 组合上没有任何内容或错误:

PREFIX foo: <http:://www.foo.com/bar/>
SELECT * WHERE {
    # I'm just running run one of these BIND statements
    # at a time; listing all of them here for easier visual comparison.
    # BIND ( URI(foo:01/grik/234) as ?s )                   # (b) Lexical error. Encountered "/" after "grik"
    # BIND ( URI(foo:"01/grik/234") as ?s )                 # (c) Encountered " <STRING_LITERAL2> "/grik/234"\""
    # BIND ( URI(foo:URI("01/grik/234")) as ?s )            # (d) Encountered "/" after "01"
    # BIND ( URI(foo:ENCODE_FOR_URI("01/grik/234")) as ?s ) # (e) Encountered "/" after "01"
    # BIND( URI(foo:ENCODE_FOR_URI("01/grik/234")) as ?s )  # (f) WARN  URI <http:://www.foo.com/bar/ENCODE_FOR_URI> has no registered function factory
    ?s a ?o .
}
LIMIT 10

您正在尝试使用前缀名称形式的 IRI。 W3C SPARQL recommendation contains the following section

4.1.1.1 Prefixed Names

The PREFIX keyword associates a prefix label with an IRI. A prefixed name is a prefix label and a local part, separated by a colon ":". A prefixed name is mapped to an IRI by concatenating the IRI associated with the prefix and the local part. The prefix label or the local part may be empty. Note that SPARQL local names allow leading digits while XML local names do not. SPARQL local names also allow the non-alphanumeric characters allowed in IRIs via backslash character escapes (e.g. ns:id\=123). SPARQL local names have more syntactic restrictions than CURIEs.

鉴于/是一个非字母数字字符,这里最重要的部分是

SPARQL local names also allow the non-alphanumeric characters allowed in IRIs via backslash character escapes (e.g. ns:id\=123).

长话短说,您的查询应该是

PREFIX foo: <http:://www.foo.com/bar/>

SELECT * WHERE {  
    BIND ( URI(foo:01\/grik\/234) as ?s )
    ?s a ?o .
}
LIMIT 10