Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Interesting question, as it seems simple enough and yet it's hard to correctly express the query. <a href="https://stackoverflow.com/a/9771503/1260887">William Greenly's answer</a> does not provide what you want, although he is perfectly right in his explanations and rightly use a <a href="http://www.w3.org/TR/sparql11-query/#propertypaths" rel="nofollow noreferrer">property path</a>. To be able to properly ask the right query that answers your question, it must be assumed that all lists are well formed (they only have one first element, one rest and they end with a nil).</p> <p>The problem in your query is that it will count all members of all lists in the dataset. You need something to relate the <code>rdf:first</code> to only the element of one list.</p> <p>If you have a URI identifying the list your are interested in, you can the following:</p> <pre><code>PREFIX rdf: &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt; PREFIX ex: &lt;http://www.example.org/#&gt; SELECT (COUNT(?member) AS ?count) WHERE { ex:uriOfTheList rdf:rest*/rdf:first ?member } </code></pre> <p>But often, lists are not identified by a URI. In this case, it is possible to identify certain lists by using other properties. For instance, imagine you have a <code>ex:listOfAuthors</code> property, you can do:</p> <pre><code>PREFIX rdf: &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt; PREFIX ex: &lt;http://www.example.org/#&gt; SELECT (COUNT(?member) AS ?count) WHERE { ex:publication ex:listOfAuthors ?list . ?list rdf:rest*/rdf:first ?member . } </code></pre> <p>Note that if you simply do:</p> <pre><code>PREFIX rdf: &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt; SELECT (COUNT(?member) AS ?count) WHERE { ?list rdf:rest*/rdf:first ?member . } </code></pre> <p>you'll add up all the sizes of lists <strong>and sublists</strong>. Now things get complicated if you don't have predicates to which you can attach the list and you don't have a URI and maybe you want to get the count for all lists, per list. There is one way that should work:</p> <pre><code>PREFIX rdf: &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt; SELECT (COUNT(?c) AS ?count) WHERE { ?thing !rdf:rest ?list . ?list rdf:rest*/rdf:first ?member . } </code></pre> <p>What this is saying is that we want to find something that connects to a list, but not with the predicate <code>rdf:rest</code>. In principle, only the start of a list is connected via a predicate to some other entity, if the entity is not a list itself and the predicate is not <code>rdf:rest</code>. Moreover, lists are normally always connected somehow to other entities, as there would be no point in describing a list independently of aything else.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload