Can't PREFIX hierarchies with SPARQL

一笑奈何 提交于 2019-12-06 08:52:40

问题


I have a need to represent a hierarchy in my URL's like this:

http://www.me.org/
-----------------root1/
-----------------------level1/
------------------------------level2/etc

I want to define PREFIX's and used them in a SPARQL query like this:

PREFIX root1: <http://www.me.org/root1/>

select * where {
    ?s ?p root1:level1/level2/etc .
} limit 100

This will fail in ARQ with the following error:

Encountered " "/" "/ "" at line 10, column 43.
Was expecting one of:
    "values" ...
    "graph" ...
...

Should I be able to represent a hierarchy in my URL's like this or is SPARQL and PREFIX use limited to a single level ?


回答1:


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.

Let's check the grammar to see whether \is one of the supposed non-alphanumeric characters:

19.8 Grammar

[169]     PN_LOCAL      ::=   (PN_CHARS_U | ':' | [0-9] | PLX ) ((PN_CHARS | '.' | ':' | PLX)* (PN_CHARS | ':' | PLX) )?
[170]     PLX           ::=   PERCENT | PN_LOCAL_ESC
[171]     PERCENT       ::=   '%' HEX HEX
[172]     HEX           ::=   [0-9] | [A-F] | [a-f]
[173]     PN_LOCAL_ESC  ::=   '\' ( '_' | '~' | '.' | '-' | '!' | '$' | '&' | "'" | '(' | ')' | '*' | '+' | ',' | ';' | '=' | '/' | '?'

| '#' | '@' | '%' )

Sure enough, we should be able to via:

PN_LOCAL &rightarrow; PLX &rightarrow; PN_LOCAL_ESC &rightarrow; '\' '/'

Thus you should be able to write:

?s ?p root1:level1\/level2\/element .



回答2:


You can use relative URIs in prefix declarations:

BASE <http://example/>
PREFIX root1:  <root1/>
PREFIX level1: <root1/level1/>
PREFIX level2: <root1/level1/level2/>

SELECT * {
  ?s ?p  level2:etc .
}

which avoids some of the overhead of repeating part-strings in URIs.




回答3:


I think the problem is that SPARQL/RDF value proposition is that it is an extension of the WWW and builds on the URI/URL principle which generally is seen as using / as a namespace separator so you would not expect to have to do something special to process a URL

You seem to be conflating URIs with Prefixed Names, prefixed names are a convenience mechanism designed to allow you to write URIs down in more compact forms for human readability. As such they can't represent every possible URI without using escape characters for things that would otherwise be ambiguous in the grammar.

As Joshua Taylor's answer demonstrates the downside of this (in your opinion) is that you can't use relative URIs as-is with prefixed names which is merely a syntax limitation of SPARQL and other RDF serialisations that use similar syntactic constructs.

However we can naturally write down relative URIs without recourse to any special characters if we instead use a BASE directive e.g.

BASE <http://www.me.org/root1/>

SELECT *
WHERE
{
    ?s ?p <level1/level2/etc> .
}
LIMIT 100

Note in this case we have to enclose the relative URI in < > as we are using a URI rather than a prefixed name. Where the URI is relative a SPARQL processor will resolve it against the declared BASE giving you the desired full URI without needing to use any escape characters.

This perhaps gives you something you consider a more natural fit?



来源:https://stackoverflow.com/questions/25547212/cant-prefix-hierarchies-with-sparql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!