Can't create PREFIX hierarchies with SPARQL

I need to represent the hierarchy in my url like this:

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

I want to define PREFIX and use 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 result in an error in ARQ with the following error:

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

Should I represent a hierarchy in my url like this, or is the use of SPARQL and PREFIX limited to one level?

+3


source to share


3 answers


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 partial strings in the URI.

+6


source


4.1.1.1 Prefix names

The PREFIX keyword associates a prefix label with an IRI. The name prefix is ​​the prefix label and the local part, separated by a colon ":". the prefix name maps to the IRI by concatenating the associated IRI with the prefix and local part. The prefix label or local part can be empty. Note that SPARQL local names allow leading. There are no local XML names. SPARQL local names also allow non-alphanumeric characters allowed in IRIs with the escapes backslash character (for example, ns: id \ = 123). SPARQL local names have more syntactic restrictions than CURIE.

Let's check the grammar to see if \

one of the intended non-alphanumeric characters is:

19.8 Grammar

& hellip;

[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  ::=   '\' ( '_' | '~' | '.' | '-' | '!' | '$' | '&' | "'" | '(' | ')' | '*' | '+' | ',' | ';' | '=' | '/' | '?'

      

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

Of course, we must be able to:



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

Thus, you should be able to write:

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

      

+5


source


I think the problem is that the SPARQL / RDF value clause is that it is a WWW extension and is based on the URI / URL principle, which is usually seen as using / as a namespace delimiter so you don't expect to need do something special to handle the url

You seem to be concatenating URIs with prefix names, prefix names are a convenience mechanism that allows you to write URIs in more compact forms for readability. Thus, they cannot represent all possible URIs without escaping for things that would otherwise be ambiguous in grammar.

As Joshua Taylor's answer shows, the downside to this (in your opinion) is that you cannot use relative URIs as-is with prefixed names, which is just a syntax constraint for SPARQL and other RDF serializations that use similar syntax.

However, we can of course write relative URIs without using any special characters by using a directive instead BASE

, eg.

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

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

      

Note that in this case, we have to enclose the relative URI in < >

, since we are using a URI and not a prefix name. If the URI is relative, the SPARQL processor will resolve it against the declared one BASE

, giving you the full URI you want without using any escape characters.

This is perhaps giving you what you think is more natural?

+3


source







All Articles