title: How to query the distance between two things with SPARQL
body: <p>I created a little metro map with RDF/XML and wonder, how to query the distance between two stops. I'm very new to SPARQL and don't know how to start. </p>
  
  <p>"Distance" means, that I want to know, how many stations are between the two ones. Later, I want to calculate the duration, but that's another point.</p>
  
  <p>Thats my first approach:</p>
  
  <pre><code>PREFIX rdf: &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt;
  PREFIX rdfs: &lt;http://www.w3.org/2000/01/rdf-schema#&gt;
  PREFIX ex: &lt;http://example.com&gt;
  
  SELECT (count(?mid) as ?distance) WHERE {
    &lt;http://example.com/StopD&gt; ex:via* ?mid .
    ?mid ex:via+ &lt;http://example.com/StopC&gt; .
  }
  </code></pre>
  
  <p>I think, that my query doesn't work because I'm using blank nodes? Doesn't work means, that I don't get the number of graphs that are between the two stops (like StopA and StopB). I have something like this in my mind: <a href="https://web.archive.org/web/20160629220103/http://answers.semanticweb.com:80/questions/3491/how-can-i-calculate-the-length-of-a-path-between-2-graph-nodes-in-sparql" rel="nofollow noreferrer">http://answers.semanticweb.com/questions/3491/how-can-i-calculate-the-length-of-a-path-between-2-graph-nodes-in-sparql/24609</a></p>
  
  <p>Thats a sketch of my map. The numbers beside the lines represents the travel duration between two stations:</p>
  
  <p><a href="http://gopeter.de/misc/semantic-metro-map.JPG" rel="nofollow noreferrer">semantic-metro-map.JPG</a></p>
  
  <p>My RDF code describe each station and its neighbour stops with available lines and travel duration. At the first look it looks quite redundant, but I want to include one-direction routes (e.g. for buses) in the future, so I think it's ok for the first try.</p>
  
  <p><strong>RDF (download the file here: <a href="http://gopeter.de/misc/metro.rdf" rel="nofollow noreferrer">http://gopeter.de/misc/metro.rdf</a>)</strong></p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;rdf:RDF 
      xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
      xmlns:ex="http://example.com/"&gt;
  
      &lt;rdf:Description rdf:about="http://example.com/StopA"&gt;
  
          &lt;ex:via rdf:parseType="Resource"&gt;
              &lt;ex:Stop rdf:resource="http://example.com/StopB" /&gt;
              &lt;ex:Line rdf:resource="http://example.com/Line1" /&gt;     
              &lt;ex:Duration&gt;2&lt;/ex:Duration&gt;            
          &lt;/ex:via&gt;
  
          &lt;ex:via rdf:parseType="Resource"&gt;
              &lt;ex:Stop rdf:resource="http://example.com/StopB" /&gt;
              &lt;ex:Line rdf:resource="http://example.com/Line2" /&gt;     
              &lt;ex:Duration&gt;7&lt;/ex:Duration&gt;            
          &lt;/ex:via&gt;       
  
          &lt;ex:via rdf:parseType="Resource"&gt;
              &lt;ex:Stop rdf:resource="http://example.com/StopD" /&gt;
              &lt;ex:Line rdf:resource="http://example.com/Line4" /&gt;     
              &lt;ex:Duration&gt;2&lt;/ex:Duration&gt;            
          &lt;/ex:via&gt;               
  
          &lt;ex:via rdf:parseType="Resource"&gt;
              &lt;ex:Stop rdf:resource="http://example.com/StopD" /&gt;
              &lt;ex:Line rdf:resource="http://example.com/Line2" /&gt;     
              &lt;ex:Duration&gt;6&lt;/ex:Duration&gt;            
          &lt;/ex:via&gt;                       
  
          &lt;ex:via rdf:parseType="Resource"&gt;
              &lt;ex:Stop rdf:resource="http://example.com/StopE" /&gt;
              &lt;ex:Line rdf:resource="http://example.com/Line1" /&gt;     
              &lt;ex:Duration&gt;1&lt;/ex:Duration&gt;            
          &lt;/ex:via&gt;                               
  
          &lt;ex:via rdf:parseType="Resource"&gt;
              &lt;ex:Stop rdf:resource="http://example.com/StopF" /&gt;
              &lt;ex:Line rdf:resource="http://example.com/Line4" /&gt;     
              &lt;ex:Duration&gt;3&lt;/ex:Duration&gt;            
          &lt;/ex:via&gt;                                       
  
      &lt;/rdf:Description&gt;
  
      &lt;rdf:Description rdf:about="http://example.com/StopB"&gt;
  
          &lt;ex:via rdf:parseType="Resource"&gt;
              &lt;ex:Stop rdf:resource="http://example.com/StopA" /&gt;
              &lt;ex:Line rdf:resource="http://example.com/Line1" /&gt;     
              &lt;ex:Duration&gt;2&lt;/ex:Duration&gt;            
          &lt;/ex:via&gt;
  
          &lt;ex:via rdf:parseType="Resource"&gt;
              &lt;ex:Stop rdf:resource="http://example.com/StopA" /&gt;
              &lt;ex:Line rdf:resource="http://example.com/Line2" /&gt;     
              &lt;ex:Duration&gt;7&lt;/ex:Duration&gt;            
          &lt;/ex:via&gt;       
  
          &lt;ex:via rdf:parseType="Resource"&gt;
              &lt;ex:Stop rdf:resource="http://example.com/StopC" /&gt;
              &lt;ex:Line rdf:resource="http://example.com/Line2" /&gt;     
              &lt;ex:Duration&gt;10&lt;/ex:Duration&gt;           
          &lt;/ex:via&gt;               
  
          &lt;ex:via rdf:parseType="Resource"&gt;
              &lt;ex:Stop rdf:resource="http://example.com/StopF" /&gt;
              &lt;ex:Line rdf:resource="http://example.com/Line3" /&gt;     
              &lt;ex:Duration&gt;2&lt;/ex:Duration&gt;            
          &lt;/ex:via&gt;                       
  
      &lt;/rdf:Description&gt;      
  
      &lt;rdf:Description rdf:about="http://example.com/StopC"&gt;
  
          &lt;ex:via rdf:parseType="Resource"&gt;
              &lt;ex:Stop rdf:resource="http://example.com/StopB" /&gt;
              &lt;ex:Line rdf:resource="http://example.com/Line2" /&gt;     
              &lt;ex:Duration&gt;10&lt;/ex:Duration&gt;           
          &lt;/ex:via&gt;
  
      &lt;/rdf:Description&gt;  
  
      &lt;rdf:Description rdf:about="http://example.com/StopD"&gt;
  
          &lt;ex:via rdf:parseType="Resource"&gt;
              &lt;ex:Stop rdf:resource="http://example.com/StopA" /&gt;
              &lt;ex:Line rdf:resource="http://example.com/Line2" /&gt;     
              &lt;ex:Duration&gt;6&lt;/ex:Duration&gt;            
          &lt;/ex:via&gt;
  
          &lt;ex:via rdf:parseType="Resource"&gt;
              &lt;ex:Stop rdf:resource="http://example.com/StopA" /&gt;
              &lt;ex:Line rdf:resource="http://example.com/Line4" /&gt;     
              &lt;ex:Duration&gt;2&lt;/ex:Duration&gt;            
          &lt;/ex:via&gt;       
  
          &lt;ex:via rdf:parseType="Resource"&gt;
              &lt;ex:Stop rdf:resource="http://example.com/StopF" /&gt;
              &lt;ex:Line rdf:resource="http://example.com/Line3" /&gt;     
              &lt;ex:Duration&gt;2&lt;/ex:Duration&gt;            
          &lt;/ex:via&gt;               
  
      &lt;/rdf:Description&gt;      
  
      &lt;rdf:Description rdf:about="http://example.com/StopE"&gt;
  
          &lt;ex:via rdf:parseType="Resource"&gt;
              &lt;ex:Stop rdf:resource="http://example.com/StopA" /&gt;
              &lt;ex:Line rdf:resource="http://example.com/Line1" /&gt;     
              &lt;ex:Duration&gt;1&lt;/ex:Duration&gt;            
          &lt;/ex:via&gt;
  
      &lt;/rdf:Description&gt;  
  
      &lt;rdf:Description rdf:about="http://example.com/StopF"&gt;
  
          &lt;ex:via rdf:parseType="Resource"&gt;
              &lt;ex:Stop rdf:resource="http://example.com/StopA" /&gt;
              &lt;ex:Line rdf:resource="http://example.com/Line4" /&gt;     
              &lt;ex:Duration&gt;3&lt;/ex:Duration&gt;            
          &lt;/ex:via&gt;
  
          &lt;ex:via rdf:parseType="Resource"&gt;
              &lt;ex:Stop rdf:resource="http://example.com/StopB" /&gt;
              &lt;ex:Line rdf:resource="http://example.com/Line3" /&gt;     
              &lt;ex:Duration&gt;2&lt;/ex:Duration&gt;            
          &lt;/ex:via&gt;
  
          &lt;ex:via rdf:parseType="Resource"&gt;
              &lt;ex:Stop rdf:resource="http://example.com/StopD" /&gt;
              &lt;ex:Line rdf:resource="http://example.com/Line3" /&gt;     
              &lt;ex:Duration&gt;2&lt;/ex:Duration&gt;            
          &lt;/ex:via&gt;               
  
      &lt;/rdf:Description&gt;      
  
  &lt;/rdf:RDF&gt;
  </code></pre>